34 #include <boost/tokenizer.hpp> 35 #include <boost/locale.hpp> 36 #include <boost/algorithm/string.hpp> 38 #include <glib/gi18n.h> 41 GncCsvTokenizer::set_separators(
const std::string& separators)
43 m_sep_str = separators;
47 int GncCsvTokenizer::tokenize()
49 using Tokenizer = boost::tokenizer< boost::escaped_list_separator<char>>;
51 boost::escaped_list_separator<char> sep(
"\\", m_sep_str,
"\"");
57 bool inside_quotes(
false);
60 m_tokenized_contents.clear();
61 std::istringstream in_stream(m_utf8_contents);
65 while (std::getline (in_stream, buffer))
68 buffer = boost::trim_copy (buffer);
69 last_quote = buffer.find_first_of(
'"');
70 while (last_quote != std::string::npos)
73 inside_quotes = !inside_quotes;
74 else if (buffer[ last_quote - 1 ] !=
'\\')
75 inside_quotes = !inside_quotes;
77 last_quote = buffer.find_first_of(
'"',last_quote+1);
91 auto bs_pos = line.find (
'\\');
92 while (bs_pos != std::string::npos)
94 if ((bs_pos == line.size()) ||
95 (line.find_first_of (
"\"\\n", bs_pos + 1) != bs_pos + 1))
96 line = line.substr(0, bs_pos) +
"\\\\" + line.substr(bs_pos + 1);
98 bs_pos = line.find (
'\\', bs_pos);
104 bs_pos = line.find (
"\"\"");
105 while (bs_pos != std::string::npos)
113 if (!(((bs_pos == 0) ||
114 (m_sep_str.find (line[bs_pos-1]) != std::string::npos))
116 ((bs_pos + 2 >= line.length()) ||
117 (m_sep_str.find (line[bs_pos+2]) != std::string::npos))))
119 line.replace (bs_pos, 2,
"\\\"");
120 bs_pos = line.find (
"\"\"", bs_pos + 2);
123 Tokenizer tok(line, sep);
124 vec.assign(tok.begin(),tok.end());
125 m_tokenized_contents.push_back(vec);
129 catch (boost::escaped_list_error &e)
131 throw (std::range_error N_(
"There was an error parsing the file."));
Class to convert a csv file into vector of string vectors.