-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseCSV.m
253 lines (226 loc) · 6.84 KB
/
parseCSV.m
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
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <stdbool.h>
#import "parseCSV.h"
/* Macros for determining if the given character is End Of Line or not */
#define EOL(x) ((*(x) == '\r' || *(x) == '\n') && *(x) != '\0')
#define NOT_EOL(x) (*(x) != '\0' && *(x) != '\r' && *(x) != '\n')
/*
* replacement for strstr() which does only check every char instead
* of complete strings
* Warning: Do not call it with haystack == NULL || needle == NULL!
*
*/
static char *cstrstr(const char *haystack, const char needle) {
char *it = (char*)haystack;
while (*it != '\0') {
if (*it == needle)
return it;
it++;
}
return NULL;
}
@implementation CSVParser
/*
* Copies a string without beginning- and end-quotes if there are
* any and returns a pointer to the string or NULL if malloc() failed
*
*/
-(NSString*)parseString:(char*)textp withLastStop:(char*)laststop {
int stringSize = (int)(textp - laststop);
if (*laststop == '\"' && *(laststop+1) != '\0' && *(laststop + stringSize - 1) == '\"') {
laststop++;
stringSize -= 2;
}
char *retval = (char*)malloc(stringSize + 1);
if (retval != NULL) {
strncpy(retval, laststop, (size_t)(stringSize));
retval[stringSize] = '\0';
}
NSMutableString *tempString = [NSMutableString stringWithCString: retval encoding:encoding];
[tempString replaceOccurrencesOfString:@"\"\""
withString:@"\""
options:0
range:NSMakeRange(0, [tempString length])];
return [NSString stringWithString: tempString];
}
-(id)init {
self = [super init];
if (self) {
// Set default bufferSize
bufferSize = 2048;
// Set fileHandle to an invalid value
fileHandle = 0;
// Set delimiter to 0
delimiter = '\0';
// Set default encoding
encoding = NSISOLatin1StringEncoding;
}
return self;
}
/*
* Gets the CSV-delimiter from the given filename using the first line
* which should be the header-line. Returns 0 on error.
*
*/
-(char)autodetectDelimiter {
char possibleDelimiters[4] = ",;\t\0";
char buffer[bufferSize+1];
// Seek to the beginning of the file
lseek(fileHandle, 0, SEEK_SET);
// Fill the buffer
if (read(fileHandle, buffer, bufferSize) > 0) {
char *textp = buffer;
// ...we assume that this is the header which also contains the separation character
while (NOT_EOL(textp) && cstrstr(possibleDelimiters, *textp) == NULL)
textp++;
// Check if a delimiter was found and set it
if (NOT_EOL(textp)) {
delimiter = *cstrstr((const char*)possibleDelimiters, *textp);
return delimiter;
}
}
return 0;
}
/*
* Parses the CSV-file with the given filename and stores the result in a
* NSMutableArray.
*
*/
-(NSMutableArray*)parseFile {
NSMutableArray *csvLine = [NSMutableArray array];
NSMutableArray *csvContent = [NSMutableArray array];
if (fileHandle <= 0)
return csvContent;
char possibleDelimiters[4] = ",;\t\0";
size_t n = 1, diff;
unsigned int quoteCount = 0;
bool firstLine = true;
char *buffer = malloc(sizeof(char) * (bufferSize + 1));
char *textp, *laststop, *lineBeginning, *lastLineBuffer = NULL;
lseek(fileHandle, 0, SEEK_SET);
while (n > 0) {
if (lastLineBuffer != NULL) {
if (strlen(lastLineBuffer) == bufferSize) {
[csvContent removeAllObjects];
[csvContent addObject: [NSArray arrayWithObject: @"ERROR: Buffer too small"]];
return csvContent;
}
// Care for the quotes in lastLineBuffer!
textp = lastLineBuffer;
while (*textp != '\0') {
if (*textp == '\"')
quoteCount++;
textp++;
}
strcpy(buffer, lastLineBuffer);
diff = strlen(lastLineBuffer);
// Make the buffer bigger
buffer = realloc(buffer, diff + bufferSize);
if (buffer == NULL) {
[csvContent removeAllObjects];
[csvContent addObject: [NSArray arrayWithObject: @"ERROR: Could not allocate bytes for buffer"]];
return csvContent;
}
lastLineBuffer = NULL;
} else diff = 0;
n = read(fileHandle, (buffer + diff), bufferSize);
if (n <= 0)
break;
// Terminate buffer correctly
if ((diff+n) <= (bufferSize + diff))
buffer[diff+n] = '\0';
textp = (char*)buffer;
while (*textp != '\0') {
// If we don't have a delimiter yet and this is the first line...
if (firstLine && delimiter == '\0') {
firstLine = false;
// ...we assume that this is the header which also contains the separation character
while (NOT_EOL(textp) && cstrstr(possibleDelimiters, *textp) == NULL)
textp++;
// Check if a delimiter was found and set it
if (NOT_EOL(textp)) {
delimiter = *cstrstr((const char*)possibleDelimiters, *textp);
printf("delim is %c / %d :-)\n", delimiter, delimiter);
while (NOT_EOL(textp))
textp++;
}
textp = (char*)buffer;
}
if (strlen(textp) > 0) {
// This is data
laststop = textp;
lineBeginning = textp;
// Parsing is splitted in parts till EOL
while (NOT_EOL(textp) || (*textp != '\0' && (quoteCount % 2) != 0)) {
// If we got two quotes and a delimiter before and after, this is an empty value
if ( *textp == '\"' &&
*(textp+1) == '\"') {
// we'll just skip this, but firstly check if it's an empty value
if ( (textp > (const char*)buffer) &&
*(textp-1) == delimiter &&
*(textp+2) == delimiter) {
[csvLine addObject: @""];
}
textp++;
} else if (*textp == '\"')
quoteCount++;
else if (*textp == delimiter && (quoteCount % 2) == 0) {
// There is a delimiter which is not between an unmachted pair of quotes?
[csvLine addObject: [self parseString:textp withLastStop:laststop]];
laststop = textp + 1;
}
// Go to the next character
textp++;
}
if (laststop == textp && *(textp-1) == delimiter) {
[csvLine addObject:@""];
if ((int)(buffer + bufferSize + diff - textp) > 0) {
lineBeginning = textp + 1;
[csvContent addObject: csvLine];
}
csvLine = [NSMutableArray new];
}
if (laststop != textp && (quoteCount % 2) == 0) {
[csvLine addObject: [self parseString:textp withLastStop:laststop]];
if ((int)(buffer + bufferSize + diff - textp) > 0) {
lineBeginning = textp + 1;
[csvContent addObject: csvLine];
}
csvLine = [NSMutableArray new];
}
if ((*textp == '\0' || (quoteCount % 2) != 0) && lineBeginning != textp) {
lastLineBuffer = lineBeginning;
csvLine = [NSMutableArray new];
}
}
while (EOL(textp))
textp++;
}
}
return csvContent;
}
-(BOOL)openFileWithPath:(NSString*)fileName {
fileHandle = open([fileName cStringUsingEncoding: NSUTF8StringEncoding], O_RDONLY);
return (fileHandle > 0);
}
-(void)closeFile {
if (fileHandle > 0) {
close(fileHandle);
fileHandle = 0;
}
}
-(char)delimiter {
return delimiter;
}
-(void)setDelimiter:(char)newDelimiter {
delimiter = newDelimiter;
}
-(void)setBufferSize:(int)newBufferSize {
bufferSize = newBufferSize;
}
-(void)setEncoding:(NSStringEncoding)newEncoding {
encoding = newEncoding;
}
@end