-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add CSV data parsing #402
Merged
Merged
Add CSV data parsing #402
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
cb1fc16
Add string trimming utility
hidmic 4d7bc1f
Add CSV data parsing support
hidmic 8de20ad
Use libcsv for minimal maintenance surface
hidmic 19da6f8
Revert "Add string trimming utility"
hidmic 395aee1
Document and please linters
hidmic 150bb26
Drop libcsv as a dependency
hidmic a925186
Move CSV data parsing to IO component
hidmic 3a9cbe2
Drop libcsv-dev from packages.apt
hidmic 8673ed4
Address peer review comments
hidmic aab2ff3
Merge branch 'main' into hidmic/csv-data
caguero 9a3c84d
Add missing documentation
hidmic e72604a
Relocate visibility attributes
hidmic f6e5a12
Add missing visibility attribute
hidmic 4008a99
Merge branch 'main' into hidmic/csv-data
hidmic be13770
Merge branch 'main' into hidmic/csv-data
hidmic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
load( | ||
"//gz_bazel:build_defs.bzl", | ||
"GZ_ROOT", | ||
"GZ_VISIBILITY", | ||
"generate_include_header", | ||
"gz_export_header", | ||
) | ||
|
||
package( | ||
default_visibility = GZ_VISIBILITY, | ||
features = [ | ||
"-parse_headers", | ||
"-layering_check", | ||
], | ||
) | ||
|
||
public_headers_no_gen = glob([ | ||
"include/gz/common/*.hh", | ||
]) | ||
|
||
sources = glob( | ||
["src/*.cc"], | ||
exclude = ["src/*_TEST.cc"], | ||
) | ||
|
||
test_sources = glob(["src/*_TEST.cc"]) | ||
|
||
gz_export_header( | ||
name = "include/gz/common/io/Export.hh", | ||
export_base = "GZ_COMMON_IO", | ||
lib_name = "gz-common-io", | ||
visibility = ["//visibility:private"], | ||
) | ||
|
||
generate_include_header( | ||
name = "iohh_genrule", | ||
out = "include/gz/common/io.hh", | ||
hdrs = public_headers_no_gen + [ | ||
"include/gz/common/io/Export.hh", | ||
], | ||
) | ||
|
||
public_headers = public_headers_no_gen + [ | ||
"include/gz/common/io/Export.hh", | ||
"include/gz/common/io.hh", | ||
] | ||
|
||
cc_library( | ||
name = "io", | ||
srcs = sources, | ||
hdrs = public_headers, | ||
includes = ["include"], | ||
deps = [ | ||
GZ_ROOT + "gz_common", | ||
GZ_ROOT + "gz_math", | ||
], | ||
) | ||
|
||
cc_binary( | ||
name = "libgz-common5-io.so", | ||
includes = ["include"], | ||
linkopts = ["-Wl,-soname,libgz-common5-io.so"], | ||
linkshared = True, | ||
deps = [":events"], | ||
) | ||
|
||
[cc_test( | ||
name = src.replace("/", "_").replace(".cc", "").replace("src_", ""), | ||
srcs = [src], | ||
deps = [ | ||
":io", | ||
GZ_ROOT + "gz_common/test:test_utils", | ||
"@gtest", | ||
"@gtest//:gtest_main", | ||
], | ||
) for src in test_sources] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add_subdirectory(gz) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add_subdirectory(common) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
gz_install_all_headers(COMPONENT io) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
/* | ||
* Copyright (C) 2022 Open Source Robotics Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
#ifndef GZ_COMMON_CSVSTREAMS_HH_ | ||
#define GZ_COMMON_CSVSTREAMS_HH_ | ||
|
||
#include <istream> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include <gz/common/io/Export.hh> | ||
|
||
#include <gz/utils/ImplPtr.hh> | ||
|
||
namespace gz | ||
{ | ||
namespace common | ||
{ | ||
/// \brief A CSV specification. | ||
struct GZ_COMMON_IO_VISIBLE CSVDialect | ||
{ | ||
/// Field delimiter character. | ||
char delimiter; | ||
|
||
/// Row termination character. | ||
char terminator; | ||
|
||
/// Field quoting character. | ||
char quote; | ||
|
||
/// CSV dialect as expected by Unix tools. | ||
static const CSVDialect Unix; | ||
}; | ||
|
||
/// \brief Check CSV dialects for equality. | ||
/// \param[in] _lhs Left-hand side CSV dialect. | ||
/// \param[in] _rhs Right-hand side CSV dialect. | ||
/// \return true if CSV dialects are equal, false otherwise. | ||
bool GZ_COMMON_IO_VISIBLE operator==(const CSVDialect &_lhs, | ||
const CSVDialect &_rhs); | ||
|
||
/// \brief A token in CSV data. | ||
/// | ||
/// Lexical specifications are typically dictated by a CSV dialect. | ||
struct CSVToken | ||
{ | ||
/// Token type. | ||
enum { | ||
TEXT = 0, ///< A pure text token (e.g. a letter). | ||
QUOTE, ///< A field quoting token (e.g. a double quote). | ||
DELIMITER, ///< A field delimiter token (e.g. a comma). | ||
TERMINATOR ///< A row termination token (e.g. a newline). | ||
} type; | ||
|
||
/// Token character. | ||
char character; | ||
}; | ||
|
||
/// \brief Extract a single token from an input stream of CSV data. | ||
/// | ||
/// If tokenization fails, the CSV data stream ``failbit`` will be set. | ||
/// | ||
/// \param[in] _stream A stream of CSV data to tokenize. | ||
/// \param[out] _token Output CSV token to extract into. | ||
/// \param[in] _dialect CSV data dialect. Defaults to the Unix dialect. | ||
/// \return same CSV data stream. | ||
GZ_COMMON_IO_VISIBLE std::istream &ExtractCSVToken( | ||
std::istream &_stream, CSVToken &_token, | ||
const CSVDialect &_dialect = CSVDialect::Unix); | ||
|
||
/// \brief Parse a single row from an input stream of CSV data. | ||
/// | ||
/// If parsing fails, the CSV data stream ``failbit`` will be set. | ||
/// | ||
/// \param[in] _stream CSV data stream to parse. | ||
/// \param[out] _row Output CSV row to parse into. | ||
/// \param[in] _dialect CSV data dialect. Defaults to the Unix dialect. | ||
/// \returns same CSV data stream. | ||
GZ_COMMON_IO_VISIBLE std::istream &ParseCSVRow( | ||
std::istream &_stream, std::vector<std::string> &_row, | ||
const CSVDialect &_dialect = CSVDialect::Unix); | ||
|
||
/// \brief A single-pass row iterator on an input stream of CSV data. | ||
/// | ||
/// Similar to std::istream_iterator, this iterator parses a stream of | ||
/// CSV data, one row at a time. \see `ParseCSVRow`. | ||
class GZ_COMMON_IO_VISIBLE CSVIStreamIterator | ||
{ | ||
public: using iterator_category = std::input_iterator_tag; | ||
public: using value_type = std::vector<std::string>; | ||
public: using difference_type = std::ptrdiff_t; | ||
public: using pointer = const value_type*; | ||
public: using reference = const value_type&; | ||
|
||
/// \brief Construct an end-of-stream iterator. | ||
public: CSVIStreamIterator(); | ||
|
||
/// \brief Construct an iterator over `_stream`. | ||
/// | ||
/// The first row will be read from the underlying stream to | ||
/// initialize the iterator. If there are parsing errors while | ||
/// reading, the underlying stream ``failbit`` will be set. | ||
/// | ||
/// \param[in] _stream A stream of CSV data to iterate. | ||
/// \param[in] _dialect CSV data dialect. Defaults to the Unix dialect. | ||
public: explicit CSVIStreamIterator( | ||
std::istream &_stream, | ||
const CSVDialect &_dialect = CSVDialect::Unix); | ||
|
||
/// \brief Read the next row from the underlying stream. | ||
/// | ||
/// If the read fails, the iterator becomes an end-of-stream iterator. | ||
/// If there are parsing errors while reading, the underlying stream | ||
/// ``failbit`` will be set. If the iterator already is an end-of-stream | ||
/// iterator, behavior is undefined. | ||
/// | ||
/// \return A reference to the iterator once modified. | ||
public: CSVIStreamIterator &operator++(); | ||
|
||
/// \brief Read the next row from the underlying stream. | ||
/// | ||
/// If the read fails, the iterator becomes an end-of-stream iterator. | ||
/// If there are parsing errors while reading, the underlying stream | ||
/// ``failbit`` will be set. If the iterator already is an end-of-stream | ||
/// iterator, behavior is undefined. | ||
/// | ||
/// \return A copy of the iterator before modification. Note that, | ||
/// while an iterator copy retains its state, the underlying stream | ||
/// may still be advanced. | ||
public: CSVIStreamIterator operator++(int); | ||
|
||
/// \brief Check for iterator equality. | ||
/// \param[in] _other Iterator to compare with. | ||
/// \return true if both iterators are end-of-stream iterators | ||
/// or if both iterator wrap the same stream and use the same dialect, | ||
/// false otherwise. | ||
public: bool operator==(const CSVIStreamIterator &_other) const; | ||
|
||
/// \brief Check for iterator inequality. | ||
/// \param[in] _other Iterator to compare with. | ||
/// \return true if both iterators are not equal, false otherwise. | ||
public: bool operator!=(const CSVIStreamIterator &_other) const; | ||
|
||
/// \brief Access current row. | ||
/// | ||
/// Behavior is undefined if the iterator is an end-of-stream iterator. | ||
/// | ||
/// \return reference to the current row. | ||
public: reference operator*() const; | ||
|
||
/// \brief Access current row. | ||
/// | ||
/// Behavior is undefined if the iterator is an end-of-stream iterator. | ||
/// | ||
/// \return pointer to the current row. | ||
public: pointer operator->() const; | ||
|
||
/// \brief Pointer to private data. | ||
private: GZ_UTILS_IMPL_PTR(dataPtr) | ||
}; | ||
} | ||
} | ||
|
||
#endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this parser work with Windows for example? If not I think we should mention it somewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@caguero I'll start by saying I didn't try this on Windows, BUT my understanding is that Windows will translate
\r\n
to\n
when reading a text file (see here and here) so this dialect should work just fine there too.