This repository was archived by the owner on Apr 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSVMANAG.h
277 lines (256 loc) · 9.14 KB
/
CSVMANAG.h
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
#ifndef CSVMANAGER_H_
#define CSVMANAGER_H_
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct CsvRow {
char **fields_;
int numOfFields_;
} CsvRow;
typedef struct CsvParser {
char *filePath_;
char delimiter_;
int firstLineIsHeader_;
char *errMsg_;
CsvRow *header_;
FILE *fileHandler_;
int fromString_;
char *csvString_;
int csvStringIter_;
} CsvParser;
// Public
CsvParser *CsvParser_new(const char *filePath, const char *delimiter, int firstLineIsHeader);
CsvParser *CsvParser_new_from_string(const char *csvString, const char *delimiter, int firstLineIsHeader);
void CsvParser_destroy(CsvParser *csvParser);
void CsvParser_destroy_row(CsvRow *csvRow);
CsvRow *CsvParser_getHeader(CsvParser *csvParser);
CsvRow *CsvParser_getRow(CsvParser *csvParser);
int CsvParser_getNumFields(CsvRow *csvRow);
char **CsvParser_getFields(CsvRow *csvRow);
const char* CsvParser_getErrorMessage(CsvParser *csvParser);
// Private
CsvRow *_CsvParser_getRow(CsvParser *csvParser);
int _CsvParser_delimiterIsAccepted(const char *delimiter);
void _CsvParser_setErrorMessage(CsvParser *csvParser, const char *errorMessage);
// Public
CsvParser *CsvParser_new(const char *filePath, const char *delimiter, int firstLineIsHeader) {
CsvParser *csvParser = (CsvParser*)malloc(sizeof(CsvParser));
if (filePath == NULL) {
csvParser->filePath_ = NULL;
} else {
int filePathLen = strlen(filePath);
csvParser->filePath_ = (char*)malloc((filePathLen + 1));
strcpy(csvParser->filePath_, filePath);
}
csvParser->firstLineIsHeader_ = firstLineIsHeader;
csvParser->errMsg_ = NULL;
if (delimiter == NULL) {
csvParser->delimiter_ = ',';
} else if (_CsvParser_delimiterIsAccepted(delimiter)) {
csvParser->delimiter_ = *delimiter;
} else {
csvParser->delimiter_ = '\0';
}
csvParser->header_ = NULL;
csvParser->fileHandler_ = NULL;
csvParser->fromString_ = 0;
csvParser->csvString_ = NULL;
csvParser->csvStringIter_ = 0;
return csvParser;
}
CsvParser *CsvParser_new_from_string(const char *csvString, const char *delimiter, int firstLineIsHeader){
CsvParser *csvParser = CsvParser_new(NULL, delimiter, firstLineIsHeader);
csvParser->fromString_ = 1;
if (csvString != NULL) {
int csvStringLen = strlen(csvString);
csvParser->csvString_ = (char*)malloc(csvStringLen + 1);
strcpy(csvParser->csvString_, csvString);
}
return csvParser;
}
void CsvParser_destroy(CsvParser *csvParser){
if (csvParser == NULL) {
return;
}
if (csvParser->filePath_ != NULL) {
free(csvParser->filePath_);
}
if (csvParser->errMsg_ != NULL) {
free(csvParser->errMsg_);
}
if (csvParser->fileHandler_ != NULL) {
fclose(csvParser->fileHandler_);
}
if (csvParser->header_ != NULL) {
CsvParser_destroy_row(csvParser->header_);
}
if (csvParser->csvString_ != NULL) {
free(csvParser->csvString_);
}
free(csvParser);
}
void CsvParser_destroy_row(CsvRow *csvRow){
int i;
for (i = 0 ; i < csvRow->numOfFields_ ; i++) {
free(csvRow->fields_[i]);
}
free(csvRow->fields_);
free(csvRow);
}
CsvRow *CsvParser_getHeader(CsvParser *csvParser){
if (! csvParser->firstLineIsHeader_) {
_CsvParser_setErrorMessage(csvParser, "Cannot supply header, as current CsvParser object does not support header");
return NULL;
}
if (csvParser->header_ == NULL) {
csvParser->header_ = _CsvParser_getRow(csvParser);
}
return csvParser->header_;
}
CsvRow *CsvParser_getRow(CsvParser *csvParser){
if (csvParser->firstLineIsHeader_ && csvParser->header_ == NULL) {
csvParser->header_ = _CsvParser_getRow(csvParser);
}
return _CsvParser_getRow(csvParser);
}
int CsvParser_getNumFields(CsvRow *csvRow){
return csvRow->numOfFields_;
}
char **CsvParser_getFields(CsvRow *csvRow){
return (char**)csvRow->fields_;
}
const char* CsvParser_getErrorMessage(CsvParser *csvParser){
return csvParser->errMsg_;
}
// Private
CsvRow *_CsvParser_getRow(CsvParser *csvParser){
int numRowRealloc = 0;
int acceptedFields = 64;
int acceptedCharsInField = 64;
if (csvParser->filePath_ == NULL && (! csvParser->fromString_)) {
_CsvParser_setErrorMessage(csvParser, "Supplied CSV file path is NULL");
return NULL;
}
if (csvParser->csvString_ == NULL && csvParser->fromString_) {
_CsvParser_setErrorMessage(csvParser, "Supplied CSV string is NULL");
return NULL;
}
if (csvParser->delimiter_ == '\0') {
_CsvParser_setErrorMessage(csvParser, "Supplied delimiter is not supported");
return NULL;
}
if (! csvParser->fromString_) {
if (csvParser->fileHandler_ == NULL) {
csvParser->fileHandler_ = fopen(csvParser->filePath_, "r");
if (csvParser->fileHandler_ == NULL) {
int errorNum = errno;
const char *errStr = strerror(errorNum);
char *errMsg = (char*)malloc(1024 + strlen(errStr));
strcpy(errMsg, "");
sprintf(errMsg, "Error opening CSV file for reading: %s : %s", csvParser->filePath_, errStr);
_CsvParser_setErrorMessage(csvParser, errMsg);
free(errMsg);
return NULL;
}
}
}
CsvRow *csvRow = (CsvRow*)malloc(sizeof(CsvRow));
csvRow->fields_ = (char**)malloc(acceptedFields * sizeof(char*));
csvRow->numOfFields_ = 0;
int fieldIter = 0;
char *currField = (char*)malloc(acceptedCharsInField);
int inside_complex_field = 0;
int currFieldCharIter = 0;
int seriesOfQuotesLength = 0;
int lastCharIsQuote = 0;
int isEndOfFile = 0;
while (1) {
char currChar = (csvParser->fromString_) ? csvParser->csvString_[csvParser->csvStringIter_] : fgetc(csvParser->fileHandler_);
csvParser->csvStringIter_++;
int endOfFileIndicator;
if (csvParser->fromString_) {
endOfFileIndicator = (currChar == '\0');
} else {
endOfFileIndicator = feof(csvParser->fileHandler_);
}
if (endOfFileIndicator) {
if (currFieldCharIter == 0 && fieldIter == 0) {
_CsvParser_setErrorMessage(csvParser, "Reached EOF");
free(currField);
CsvParser_destroy_row(csvRow);
return NULL;
}
currChar = '\n';
isEndOfFile = 1;
}
if (currChar == '\r') {
continue;
}
if (currFieldCharIter == 0 && ! lastCharIsQuote) {
if (currChar == '\"') {
inside_complex_field = 1;
lastCharIsQuote = 1;
continue;
}
} else if (currChar == '\"') {
seriesOfQuotesLength++;
inside_complex_field = (seriesOfQuotesLength % 2 == 0);
if (inside_complex_field) {
currFieldCharIter--;
}
} else {
seriesOfQuotesLength = 0;
}
if (isEndOfFile || ((currChar == csvParser->delimiter_ || currChar == '\n') && ! inside_complex_field) ){
currField[lastCharIsQuote ? currFieldCharIter - 1 : currFieldCharIter] = '\0';
csvRow->fields_[fieldIter] = (char*)malloc(currFieldCharIter + 1);
strcpy(csvRow->fields_[fieldIter], currField);
free(currField);
csvRow->numOfFields_++;
if (currChar == '\n') {
return csvRow;
}
if (csvRow->numOfFields_ != 0 && csvRow->numOfFields_ % acceptedFields == 0) {
csvRow->fields_ = (char**)realloc(csvRow->fields_, ((numRowRealloc + 2) * acceptedFields) * sizeof(char*));
numRowRealloc++;
}
acceptedCharsInField = 64;
currField = (char*)malloc(acceptedCharsInField);
currFieldCharIter = 0;
fieldIter++;
inside_complex_field = 0;
} else {
currField[currFieldCharIter] = currChar;
currFieldCharIter++;
if (currFieldCharIter == acceptedCharsInField - 1) {
acceptedCharsInField *= 2;
currField = (char*)realloc(currField, acceptedCharsInField);
}
}
lastCharIsQuote = (currChar == '\"') ? 1 : 0;
}
}
int _CsvParser_delimiterIsAccepted(const char *delimiter){
char actualDelimiter = *delimiter;
if (actualDelimiter == '\n' || actualDelimiter == '\r' || actualDelimiter == '\0' ||
actualDelimiter == '\"') {
return 0;
}
return 1;
}
void _CsvParser_setErrorMessage(CsvParser *csvParser, const char *errorMessage){
if (csvParser->errMsg_ != NULL) {
free(csvParser->errMsg_);
}
int errMsgLen = strlen(errorMessage);
csvParser->errMsg_ = (char*)malloc(errMsgLen + 1);
strcpy(csvParser->errMsg_, errorMessage);
}
#ifdef __cplusplus
}
#endif
#endif