Vince's CSV Parser
csv_format.cpp
Go to the documentation of this file.
1 
5 #include <algorithm>
6 #include <set>
7 
8 #include "csv_format.hpp"
9 
10 namespace csv {
12  this->possible_delimiters = { delim };
13  this->assert_no_char_overlap();
14  return *this;
15  }
16 
17  CSV_INLINE CSVFormat& CSVFormat::delimiter(const std::vector<char> & delim) {
18  this->possible_delimiters = delim;
19  this->assert_no_char_overlap();
20  return *this;
21  }
22 
24  this->no_quote = false;
25  this->quote_char = quote;
26  this->assert_no_char_overlap();
27  return *this;
28  }
29 
30  CSV_INLINE CSVFormat& CSVFormat::trim(const std::vector<char> & chars) {
31  this->trim_chars = chars;
32  this->assert_no_char_overlap();
33  return *this;
34  }
35 
36  CSV_INLINE CSVFormat& CSVFormat::column_names(const std::vector<std::string>& names) {
37  this->col_names = names;
38  this->header = -1;
39  return *this;
40  }
41 
43  if (row < 0) this->variable_column_policy = VariableColumnPolicy::KEEP;
44 
45  this->header = row;
46  this->col_names = {};
47  return *this;
48  }
49 
50  CSV_INLINE void CSVFormat::assert_no_char_overlap()
51  {
52  auto delims = std::set<char>(
53  this->possible_delimiters.begin(), this->possible_delimiters.end()),
54  trims = std::set<char>(
55  this->trim_chars.begin(), this->trim_chars.end());
56 
57  // Stores intersection of possible delimiters and trim characters
58  std::vector<char> intersection = {};
59 
60  // Find which characters overlap, if any
61  std::set_intersection(
62  delims.begin(), delims.end(),
63  trims.begin(), trims.end(),
64  std::back_inserter(intersection));
65 
66  // Make sure quote character is not contained in possible delimiters
67  // or whitespace characters
68  if (delims.find(this->quote_char) != delims.end() ||
69  trims.find(this->quote_char) != trims.end()) {
70  intersection.push_back(this->quote_char);
71  }
72 
73  if (!intersection.empty()) {
74  std::string err_msg = "There should be no overlap between the quote character, "
75  "the set of possible delimiters "
76  "and the set of whitespace characters. Offending characters: ";
77 
78  // Create a pretty error message with the list of overlapping
79  // characters
80  for (size_t i = 0; i < intersection.size(); i++) {
81  err_msg += "'";
82  err_msg += intersection[i];
83  err_msg += "'";
84 
85  if (i + 1 < intersection.size())
86  err_msg += ", ";
87  }
88 
89  throw std::runtime_error(err_msg + '.');
90  }
91  }
92 }
Stores information about how to parse a CSV file.
Definition: csv_format.hpp:36
CSVFormat & column_names(const std::vector< std::string > &names)
Sets the column names.
Definition: csv_format.cpp:36
CSVFormat & trim(const std::vector< char > &ws)
Sets the whitespace characters to be trimmed.
Definition: csv_format.cpp:30
CSVFormat & delimiter(char delim)
Sets the delimiter of the CSV file.
Definition: csv_format.cpp:11
CSVFormat & header_row(int row)
Sets the header row.
Definition: csv_format.cpp:42
CSVFormat & quote(char quote)
Sets the quote character.
Definition: csv_format.cpp:23
#define CSV_INLINE
Helper macro which should be #defined as "inline" in the single header version.
Definition: common.hpp:26
Defines an object used to store CSV format settings.
The all encompassing namespace.