-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv2json.cpp
155 lines (137 loc) · 4.04 KB
/
csv2json.cpp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
Copyright (c) 2012-2023 James Baker
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
The official repository for this library is at https://github.com/VA7ODR/json
*/
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#if defined USE_DATA_DOCUMENT
# include "data.hpp"
#else
# include "json.hpp"
#endif
std::vector<std::vector<sdstring>> CSVData;
sdstring deQuote(const sdstring & strIn)
{
sdstring str(strIn);
if (str[0] == '\"') {
str = str.substr(1);
}
if (str[str.length() - 1] == '\"') {
str = str.substr(0, str.length() - 1);
}
return str;
}
size_t ParseCSV(const sdstring & szFileName)
{
FILE * fp;
size_t iLines = 0;
fp = fopen(szFileName.c_str(), "r");
if (fp) {
fseek(fp, 0, SEEK_END);
size_t l = ftell(fp);
fseek(fp, 0, SEEK_SET);
char * szData = new char[l + 1];
char * szField = szData;
memset(szData, 0, l + 1);
if (fread(szData, 1, l, fp) != l) {
printf("Error reading file.");
}
fclose(fp);
bool bQuote = false;
size_t iFields = 0;
for (size_t i = 0; i < l; i++) {
if (bQuote) {
if (szData[i] == '\"') {
bQuote = false;
}
} else {
if (szData[i] == '\"') {
bQuote = true;
continue;
} else if (szData[i] == ',') {
printf("Got Comma %i characters %i lines %i fields\n", (int)i, (int)iLines, (int)iFields);
if (iLines >= CSVData.size()) {
CSVData.resize(iLines + 1);
}
CSVData[iLines].push_back(deQuote(sdstring(szField, &szData[i] - szField)));
szField = &szData[i] + 1;
iFields++;
} else if (szData[i] == '\n') {
printf("Got newline %i characters %i lines %i fields\n", (int)i, (int)iLines, (int)iFields);
if (iLines >= CSVData.size()) {
CSVData.resize(iLines + 1);
}
CSVData[iLines].push_back(deQuote(sdstring(szField, &szData[i] - szField)));
iFields = 0;
szField = &szData[i] + 1;
iLines++;
} else if (i == l - 1) {
printf("Got end %i characters %i lines %i fields\n", (int)i, (int)iLines, (int)iFields);
if (iLines >= CSVData.size()) {
CSVData.resize(iLines + 1);
}
CSVData[iLines].push_back(deQuote(sdstring(szField)));
iFields = 0;
// szField = &szData[i] + 1;
iLines++;
}
}
}
delete[] szData;
}
return iLines;
}
int main(int argc, char ** argv)
{
if (std::string(argv[0]) == "csv2xml") {
if (argc != 4) {
printf("Useage: %s input.csv output.json root_tag\n", argv[0]);
return 1;
}
} else {
if (argc != 3) {
printf("Useage: %s input.csv output.json \n", argv[0]);
return 1;
}
}
size_t iLines = ParseCSV(argv[1]);
#if defined USE_DATA_DOCUMENT
odata::document outDoc;
#else
ojson::document outDoc;
#endif
for (size_t i = 1; i < iLines; i++) {
for (size_t j = 0; j < CSVData[i].size(); j++) {
if (CSVData[0][j].empty()) {
continue;
}
// printf("%s = %s\n", CSVData[0][j].c_str(), CSVData[i][j].c_str());
outDoc[i - 1][CSVData[0][j]] = CSVData[i][j];
}
}
#if defined USE_DATA_DOCUMENT
if (std::string(argv[0]) == "csv2xml") {
outDoc.writeXMLFile(argv[2], argv[3], true);
return 0;
}
#endif
outDoc.writeFile(argv[2], true);
return 0;
}