TkN 2.7
Toolkit for Nuclei
Loading...
Searching...
No Matches
tkcsv.h
1/********************************************************************************
2 * Licensed under the MIT License <http://opensource.org/licenses/MIT>. *
3 * SPDX-License-Identifier: MIT *
4 ********************************************************************************/
5
6#ifndef tkcsv_H
7#define tkcsv_H
8
9#include <string>
10#include <vector>
11
12namespace tkn::detail {
13
14inline bool parse_csv_row(const std::string &line, std::vector<std::string> &fields)
15{
16 fields.clear();
17 std::string field;
18 bool quoted = false;
19
20 for (std::size_t index = 0; index < line.size(); ++index) {
21 const char character = line[index];
22 if (character == '"') {
23 if (quoted && index + 1 < line.size() && line[index + 1] == '"') {
24 field += '"';
25 ++index;
26 } else {
27 quoted = !quoted;
28 }
29 } else if (character == ',' && !quoted) {
30 fields.push_back(field);
31 field.clear();
32 } else if (character != '\r') {
33 field += character;
34 }
35 }
36 fields.push_back(field);
37 return !quoted;
38}
39
40inline std::vector<std::string> parse_csv_row(const std::string &line)
41{
42 std::vector<std::string> fields;
43 parse_csv_row(line, fields);
44 return fields;
45}
46
47} // namespace tkn::detail
48
49#endif
bool parse_csv_row(const std::string &line, std::vector< std::string > &fields)
Definition tkcsv.h:14