-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcsvReader.cpp
More file actions
136 lines (128 loc) · 3.78 KB
/
Copy pathcsvReader.cpp
File metadata and controls
136 lines (128 loc) · 3.78 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
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
/*
* csvReader.cpp: a class to read CSV files. I cadged the basic code for this
* from the web but soon discovered that it had real issues with anything but
* the most basic of CSV files. Quotes, multi line fields, etc. all threw it
* for a loop. Now, as it turned out I only needed basic functionality because
* well, I simply got tired of trying to figure out anything more!
*
* Copyright 2012 Wayne Hortensius <whortens@shaw.ca>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "csvReader.h"
using namespace std;
// constructor: takes the name of a csv file and opens it
CsvReader::CsvReader(string csvFilename)
{
m_csvFilename = csvFilename;
m_ifs.open(m_csvFilename.c_str(),ifstream::in);
}
// destructor: make sure the csv file is closed before we close up shop
CsvReader::~CsvReader()
{
if( m_ifs.is_open() ) {
m_ifs.close();
}
}
// Strip: remove enclosing single or double quotes and replace
// internal quote pairs with singles
string CsvReader::Strip(string field)
{
string output=field;
char quote=0;
if( output.length()>1 ) {
if( output[0]=='"' && output[output.length()-1]=='"' ) {
quote = '"';
} else if( output[0]=='\'' && output[output.length()-1]=='\'' ) {
quote = '\'';
}
if( quote!=0 ) {
output = output.substr(1,output.length()-2);
}
}
if( quote!=0 ) {
string quotes = string(2,quote);
size_t index = output.find(quotes);
while( index != string::npos ) {
output.replace(index,2,1,quote);
index = output.find(quotes,index+1);
}
index = output.find('\\');
while( index != string::npos ) {
output.erase(index,1);
index = output.find('\\',index+1);
}
} else {
size_t index = output.find('\\');
while( index != string::npos ) {
output.erase(index,1);
index = output.find('\\',index+1);
}
}
return output;
}
// Parse: given a line from the csv file, return the individual columns
// stripped of any enclosing quotes in a string vector
void CsvReader::Parse(string line,vector<string> &fields)
{
int begin;
int current;
char quote=0;
int length = line.length();
current = 0;
begin = current;
fields.clear();
while( current < length ) {
if( quote && line[current]==quote && line[current-1]!='\\' && current!=begin ) {
quote = 0;
} else if( !quote && (line[current]=='"' || line[current]=='\'') ) {
quote = line[current];
}
if( !quote && (line[current]==',' || current==length-1) ) {
if( line[current]==',' ) {
fields.push_back(Strip(line.substr(begin,current-begin)));
} else {
fields.push_back(Strip(line.substr(begin,current-begin+1)));
}
begin = ++current;
quote = 0;
continue;
}
++current;
}
}
// Parse: parse the next line from the csv file into a string vector. Returns
// true if data is returned or false if we're at the end of the file.
bool CsvReader::Parse(vector<string> &fields)
{
string line;
if( m_ifs.is_open() && m_ifs.good() ) {
getline(m_ifs,line);
size_t eol = line.find_last_of('\n');
if( eol != string::npos ) {
line.erase(eol);
}
Parse(line,fields);
return true;
} else {
m_ifs.close();
return false;
}
}