54{
55 using Tokenizer = boost::tokenizer< boost::escaped_list_separator<char>>;
56
57 boost::escaped_list_separator<char> sep("\\", m_sep_str, "\"");
58
59 StrVec vec;
60 std::string line;
61 std::string buffer;
62
63 bool inside_quotes(false);
64 size_t last_quote(0);
65
66 m_tokenized_contents.clear();
67 std::istringstream in_stream(m_utf8_contents);
68
69 try
70 {
71 while (std::getline (in_stream, buffer))
72 {
73
74 buffer = boost::trim_copy (buffer);
75 last_quote = buffer.find_first_of('"');
76 while (last_quote != std::string::npos)
77 {
78 if (last_quote == 0)
79 inside_quotes = !inside_quotes;
80 else if (!m_enable_escape || buffer[ last_quote - 1 ] != '\\')
81 inside_quotes = !inside_quotes;
82
83 last_quote = buffer.find_first_of('"',last_quote+1);
84 }
85
86 line.append(buffer);
87 if (inside_quotes)
88 {
89 line.append(" ");
90 continue;
91 }
92
93
94
95
96
97 auto bs_pos = line.find ('\\');
98 while (bs_pos != std::string::npos)
99 {
100 if (! m_enable_escape)
101 {
102
103 line.insert (bs_pos, 1, '\\');
104 bs_pos += 2;
105 }
106 else
107 {
108
109 if ((bs_pos == line.size()) ||
110 (line.find_first_of ("\"\\n", bs_pos + 1) != bs_pos + 1))
111 line = line.substr(0, bs_pos) + "\\\\" + line.substr(bs_pos + 1);
112 bs_pos += 2;
113 }
114
115 bs_pos = line.find ('\\', bs_pos);
116 }
117
118
119
120
121 bs_pos = line.find ("\"\"");
122 while (bs_pos != std::string::npos)
123 {
124
125
126
127
128
129
130 if (!(((bs_pos == 0) ||
131 (m_sep_str.find (line[bs_pos-1]) != std::string::npos))
132 &&
133 ((bs_pos + 2 >= line.length()) ||
134 (m_sep_str.find (line[bs_pos+2]) != std::string::npos))))
135
136 line.replace (bs_pos, 2, "\\\"");
137 bs_pos = line.find ("\"\"", bs_pos + 2);
138 }
139
140 Tokenizer tok(line, sep);
141 vec.assign(tok.begin(),tok.end());
142 m_tokenized_contents.push_back(vec);
143 line.clear();
144 }
145 }
146 catch (boost::escaped_list_error &e)
147 {
148 throw (std::range_error N_("There was an error parsing the file."));
149 }
150
151 return 0;
152}