-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv-reader.cpp
More file actions
99 lines (89 loc) · 2.67 KB
/
csv-reader.cpp
File metadata and controls
99 lines (89 loc) · 2.67 KB
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
//
// Read file csv
//
#include "csv-reader.h"
//
// Function to parse a CSV file using Finite Automaton
// return 1 if found a valid CSV line
// return 0 if the line is invalid
// Return the fields in the vector v
//
int getCSVLine(ifstream &inCSVFile, vector<string> &v)
{
// Define Automaton states
#define AUT_START 0
#define AUT_VALID_CHR 1
#define AUT_QUOTATION 2
#define AUT_BACK_SLASH 3
#define AUT_END_QUOTATION 4
#define AUT_END 5
#define AUT_ERROR 6
string field;
char c = 0;
char currentState = AUT_START; // identify the current automaton state
string valids = "abcdefghijklmnoprstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*():;/<>.[]{}|\\-_+= ";
while (inCSVFile && currentState != AUT_ERROR && currentState != AUT_END) // loop until ends or find an erro on automaton
{
inCSVFile.get(c);
switch (currentState)
{
case AUT_START: // First state
if (strchr(valids.c_str(), c)) // found a valid character go to the next automaton
currentState = AUT_VALID_CHR;
else if (c == '"') // found a quotation mark
currentState = AUT_QUOTATION;
else
currentState = AUT_ERROR;
field += c; // append a new character into field
break;
case AUT_VALID_CHR:
if (c == '\n' || !inCSVFile || c == ',') // Delimiter, end of line or End of File
{
v.push_back(field);
field = "";
currentState = ((c == ',') ? AUT_START : AUT_END);
break;
}
else if (strchr(valids.c_str(), c))
currentState = AUT_VALID_CHR;
else
currentState = AUT_ERROR;
field += c;
break;
case AUT_QUOTATION:
if (c == '\\')
currentState = AUT_BACK_SLASH;
else if (strchr(valids.c_str(), c))
currentState = AUT_QUOTATION;
else if (c == '"')
currentState = AUT_END_QUOTATION;
else
currentState = AUT_ERROR;
if (c != '\\')
field += c;
break;
case AUT_END_QUOTATION:
if (c == '\n' || !inCSVFile || c == ',') // Delimiter, end of line or End of File
{
v.push_back(field);
field = "";
currentState = ((c == ',') ? AUT_START : AUT_END);
break;
}
else
currentState = AUT_ERROR;
break;
case AUT_BACK_SLASH:
field += c; // append the character without verification
currentState = AUT_QUOTATION;
break;
}
}
if (currentState == AUT_END) // Verify if next character is the end of file
{
inCSVFile.peek();
if (inCSVFile.eof())
inCSVFile.get(c);
}
return (currentState == AUT_END);
}