-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv2xml.cpp
291 lines (236 loc) · 6.27 KB
/
csv2xml.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* $Id: csv2xml.cpp,v 1.11 2005/07/18 07:36:54 jacob Exp $
*
* csv2xml - A simple csv to xml converter
*
* Origionally developed by Jacob Rhoden, for
* usage in FreeBSD. This program is released
* under the open source liscence.
*
* Please email/report any bugs to:
* url: http://rhoden.id.au/contact.html
* email: [email protected]
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TOKEN 2000
#define MAX_FIELDS 200
int line;
int fieldcount;
char *fields[MAX_FIELDS];
int xmlmode;
char sub_char = '\0';
int nullify;
char *encoding;
void cleanup(void) {
int i;
for(i=0;i<fieldcount;i++) {
delete fields[i];
fields[i]=NULL;
}
}
int warn(char *message) {
fprintf(stderr,"Warning: %s on line %d\n",message,line);
}
int fatal(char *message) {
fprintf(stderr,"Error: %s on line %d\n",message,line);
cleanup();
exit(1);
}
int get_csv_token(char *token, int maxlen) {
int c;
int quoted;
int len;
len=0;
quoted=0;
while((c=getchar()) && len<maxlen) {
if(c==-1) { break; }
if(c=='"' && quoted==1) {
c=getchar();
if(c!='"') { quoted=0; }
}
if(len==0 && c=='"' && quoted==0) { quoted=1; continue; }
if(quoted==0 && c==',') { *token='\0'; return(1); }
if(c==10) { line++; }
if(quoted==0 && c==10) { *token='\0'; return(0); }
*token=c;
len++;
token++;
}
if(len==maxlen) { fatal("Field was too long for this program to handle"); }
*token='\0';
return(-1);
}
/*
* If a field header is to be used as a xml token, we
* must remove special characters and so fourth
*/
void sanitize(char *token) {
while(*token!='\0') {
if(
(*token>='0' && *token<='9') ||
(*token>='a' && *token<='z') ||
(*token>='A' && *token<='Z')
) {
*token++;
}
else {
*token='_';
*token++;
}
}
}
/*
* This function is called to read the first row and store
* the field names as the header names.
*/
int get_field_headers() {
char token[MAX_TOKEN+1];
int status;
status=1;
fieldcount=0;
memset(&fields,0,sizeof(fields));
while(fieldcount<MAX_FIELDS && status>0) {
status=get_csv_token((char*)&token,MAX_TOKEN);
if(status<0) { break; }
if(token[0]==0) { fatal("Header row has null string"); }
if(xmlmode!=2) { sanitize((char*)&token); }
fields[fieldcount]=new char[strlen(token)+1];
strcpy(fields[fieldcount],token);
fieldcount++;
}
if(fieldcount==MAX_FIELDS) { fatal("Too many columns in this csv file"); }
return(status);
}
void print_entity(char *e) {
/*
* printing a string character by character is not the best way to do this
* but it works for the time being.
*/
while(*e!='\0') {
if(*e=='&') { printf("&"); }
else if(*e=='<') { printf("<"); }
else if(*e=='>') { printf(">"); }
else if(*e=='"') { printf("""); }
else if(*e=='\'') { printf("'"); }
else if(*e<0 && sub_char!='\0') { printf("%c", sub_char); }
else printf("%c",*e);
e++;
}
}
int get_row() {
char *crow[MAX_FIELDS];
char token[MAX_TOKEN+1];
int status;
int colcount;
int i;
colcount=0;
status=1;
memset(&crow,0,sizeof(crow));
while(colcount<MAX_FIELDS && status>0) {
status=get_csv_token((char*)&token,MAX_TOKEN);
if(status<0) { break; }
crow[colcount]=new char[strlen(token)+1];
strcpy(crow[colcount],token);
colcount++;
}
if(colcount==0) {
cleanup();
exit(0);
}
if(colcount==MAX_FIELDS) {
fatal("Row contained more fields than this sofware can handle");
}
if(colcount!=fieldcount) {
warn("Row count does not match field count");
return(status);
}
printf("<row>\n");
for(i=0;i<colcount;i++) {
if(nullify==0 || crow[i][0]!='\0') {
if(xmlmode==1) {
printf(" <%s value=\"",fields[i]);
print_entity(crow[i]);
printf("\" />\n");
}
else if(xmlmode==2) {
printf(" <item name=\"%s\">",fields[i]);
print_entity(crow[i]);
printf("</item>\n");
}
else {
printf(" <%s>",fields[i]);
print_entity(crow[i]);
printf("</%s>\n",fields[i]);
}
}
delete crow[i];
crow[i]=NULL;
}
printf("</row>\n");
return(status);
}
void version_help(void) {
fprintf(stderr,"csv2xml 0.6 - Open source csv to xml converter\n");
fprintf(stderr,"\n");
fprintf(stderr,"Version: 0.6\n");
fprintf(stderr,"Date: July 15, 2005\n");
fprintf(stderr,"\n");
exit(0);
}
void option_help(void) {
fprintf(stderr,"csv2xml 0.6 - Open source csv to xml converter\n");
fprintf(stderr,"\n");
fprintf(stderr,"General usage: csv2xml < infile > outfile\n");
fprintf(stderr,"\n");
// fprintf(stderr," -e=ENCODING Set encoding, default is utf-8\n");
fprintf(stderr," -m=number Set xml mode. 0-2\n");
fprintf(stderr," -s=char To strip unicode characters, specify a replacement character.");
fprintf(stderr," -n Hide fields that are null/empty\n");
fprintf(stderr," -h Output this help.\n");
fprintf(stderr," -v Display version information.\n");
fprintf(stderr,"\n");
exit(1);
}
void opt_xmlmode(char *opt) {
if(opt[2]!='=') { option_help(); }
if(opt[3]<'0' || opt[3]>'9') { option_help(); }
if(opt[4]!='\0') { option_help(); }
xmlmode=(int)opt[3]-'0';
if(xmlmode>2) { option_help(); }
}
void opt_encoding(char *opt) {
if(opt[2]!='=') { option_help(); }
}
void opt_sub_non_unicode(char *opt) {
if(opt[2]!='=' || opt[3]=='\0') { option_help(); }
sub_char = opt[3];
}
void get_options(int argc,char *argv[]) {
int i;
xmlmode=0;
encoding=NULL;
for(i=1;i<argc;i++) {
if(argv[i][0]!='-') { option_help(); }
if(argv[i][1]=='v') { version_help(); }
else if(argv[i][1]=='h') { option_help(); }
else if(argv[i][1]=='n') { nullify=1; }
else if(argv[i][1]=='m') { opt_xmlmode(argv[i]); }
// else if(argv[i][1]=='e') { opt_encoding(argv[i]); }
else if(argv[i][1]=='s') { opt_sub_non_unicode(argv[i]); }
else { option_help(); }
}
}
int main(int argc,char *argv[]) {
int status;
get_options(argc,argv);
fieldcount=0;
status=get_field_headers();
if(status<0) {
fatal("Header row contains no entities.");
}
line=1;
while(get_row()>=0);
cleanup();
exit(0);
}