1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| template <typename InputIt, typename Container>
size_t parse_csv(InputIt first, InputIt last, Container& match, char separator = ',', char quote = '\"')
{
enum { buf_size = 256 };
match.clear();
bool in_quote = false;
char buf[buf_size];
char* pbase = &buf[0]; // pointer to the beginning of the output buffer.
char* pptr = pbase; // pointer to the next element of the output buffer.
char* epptr = pbase + buf_size; // pointer just past the end of the output buffer.
while (first != last)
{
char ch = *first++;
if (ch == quote)
{
in_quote = !in_quote;
continue;
}
if (ch == separator && !in_quote)
{
std::string::size_type size = pptr - pbase;
match.push_back(std::string(pbase, size));
pptr = pbase;
}
else
if (pptr == epptr)
continue; //overflow -> troncature
else
*pptr++ = ch;
}
std::string::size_type size = pptr - pbase;
match.push_back(std::string(pbase, size));
return match.size();
} |
Partager