-
Notifications
You must be signed in to change notification settings - Fork 1
/
read.c
177 lines (160 loc) · 4.83 KB
/
read.c
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
/*
Data structure and function implementations for a
csv parsing module intended to convert from CSV files
into C strings.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "read.h"
#include "record_struct.h"
#include "record_struct.c"
#define INIT_RECORDS 1
#define NUM_FIELDS 19
/*
Reads a line - removing trailing whitespace, returns a csvRecord or NULL
if parsing was unsuccessful.
*/
struct csvRecord *parseLine(char *line);
struct csvRecord **readCSV(FILE *csvFile, int *n){
struct csvRecord **records = NULL;
int numRecords = 0;
int spaceRecords = 0;
char *line = NULL;
size_t size = 0;
/*
We want to drop the header as the reading does not have that
functionality.
*/
int droppedFirstLine = 0;
while(getline(&line, &size, csvFile) > 0){
if(! droppedFirstLine){
droppedFirstLine = 1;
continue;
}
if(spaceRecords == 0){
records = (struct csvRecord **)
malloc(sizeof(struct csvRecord *) * INIT_RECORDS);
assert(records);
spaceRecords = INIT_RECORDS;
} else if(numRecords >= spaceRecords){
spaceRecords *= 2;
records = (struct csvRecord **)
realloc(records, sizeof(struct csvRecord *) * spaceRecords);
assert(records);
}
records[numRecords] = parseLine(line);
if(records[numRecords]){
numRecords++;
}
}
if(line){
free(line);
}
/* Shrink. */
records = (struct csvRecord **)
realloc(records, sizeof(struct csvRecord *) * numRecords);
assert(records);
*n = numRecords;
return records;
}
/* This function reads CSVs where fields do not contain quotes.
To handle this, we could add an additional Step 3 which shifted the
string one character back for every quote seen. */
struct csvRecord *parseLine(char *line){
struct csvRecord *ret = NULL;
char **fields = (char **) malloc(sizeof(char *) * NUM_FIELDS);
assert(fields);
int fieldNum = 0;
int len = strlen(line);
/* Remove trailing whitespace first. */
while(len > 0 && (line[len - 1] == '\n' || line[len - 1] == '\r')){
line[len - 1] = '\0';
len--;
}
/* Check for empty lines. */
if(len == 0){
return NULL;
}
int progress = 0;
int start = 0;
/* For simplicity assume quotes only escape comma fields. */
int inQuotes = 0;
while(progress <= len){
if(line[progress] == '\"'){
if(inQuotes){
inQuotes = 0;
} else {
inQuotes = 1;
}
} else if(line[progress] == ',' || line[progress] == '\0'){
if(inQuotes){
/* Skip over, comma is escaped. */
/* Verify CSV not malformed. */
assert(line[progress] != '\0');
} else {
/* Check fields match expected. */
assert(line[progress] != '\0' || fieldNum == (NUM_FIELDS - 1));
/* Terminate */
line[progress] = '\0';
assert(fieldNum < NUM_FIELDS);
fields[fieldNum] = strdup(line + start);
assert(fields[fieldNum]);
fieldNum++;
start = progress + 1;
}
}
progress++;
}
/* Sanity check! Did we get everything? */
assert(fieldNum == NUM_FIELDS);
/* Step 2: Clean extraneous quotes. */
for(int i = 0; i < NUM_FIELDS; i++){
if(strlen(fields[i]) > 0){
if(fields[i][0] == '\"'){
assert(fields[i][strlen(fields[i]) - 1] == '\"');
fields[i][strlen(fields[i]) - 1] = '\0';
int len = strlen(fields[i]);
for(int j = 0; j < len; j++){
// Shuffle all characters along one character.
fields[i][j] = fields[i][j + 1];
}
}
}
}
ret = (struct csvRecord *) malloc(sizeof(struct csvRecord));
assert(ret);
ret->fieldCount = fieldNum;
ret->fields = fields;
return ret;
}
char *getQuery(FILE *f){
char *line = NULL;
size_t size = 0;
if(getline(&line, &size, f) > 0){
while(strlen(line) > 0 && (line[strlen(line) - 1] == '\n'
|| line[strlen(line) - 1] == '\r')){
line[strlen(line) - 1] = '\0';
}
return line;
} else {
if(line){
free(line);
}
return NULL;
}
}
void freeCSV(struct csvRecord **dataset, int n){
if(! dataset){
return;
}
for(int i = 0; i < n; i++){
for(int j = 0; j < dataset[i]->fieldCount; j++){
free(dataset[i]->fields[j]);
}
free(dataset[i]->fields);
free(dataset[i]);
}
free(dataset);
}