This repository was archived by the owner on Dec 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
111 lines (101 loc) · 2.36 KB
/
util.cpp
File metadata and controls
111 lines (101 loc) · 2.36 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
#include "util.h"
using namespace std;
bool validateTaskName(string& s){
if (s.size() > 0 && isalpha(s[0]))
return true;
return false;
}
bool validateItemName(string& s){
return ((not s.empty()) and isalpha(s[0]));
}
void csvprint(vector<vector<string>>& csvData){
for (size_t row = 0; row < csvData.size(); row++){
for (size_t col = 0; col < csvData[row].size(); col++)
cout << csvData[row][col];
cout << endl;
}
cout << endl;
}
string& trimSpace(string& s, char trimChar)
{
while (not s.empty() and s[0] == trimChar)
s.erase(0, 1);
while (not s.empty() and s[s.size() - 1] == trimChar)
s.erase(s.size() - 1, 1);
return s;
}
#define PROCESS_DOUBLE_QUOTES
void csvread(string& filename, char delim, std::vector< std::vector<std::string> > &csv)
{
std::ifstream is(filename);
if (is) {
std::string line;
std::vector<std::string> fields;
while (getline(is, line)) {
auto cr = line.find('\r');
if (cr != std::string::npos) line.erase(cr, 1);
std::string field;
for (size_t i = 0; i < line.size(); i++) {
#ifdef PROCESS_DOUBLE_QUOTES
if (line[i] == '"') {
field += line[i]; // copy 1st "
for (i++; i < line.size(); i++) {
field += line[i];
if (line[i] == '"') // found 2nd "
break;
}
}
else
#endif
if (line[i] != delim) {
field += line[i];
}
else {
trimSpace(field);
fields.push_back(field);
field.clear();
}
}
trimSpace(field);
fields.push_back(field);
csv.push_back(fields);
fields.clear();
}
is.close();
}
else {
std::cerr << "cannot open file " << filename << "\n";
}
}
/*
void csvdump(string& filename, char separator, vector< vector<string> >& csvData){
ifstream is(filename);
if (not is.is_open()){
throw string("Cannot open file '") + filename + "'";
}
string line;
while (getline(is, line)){
auto cr = line.find('\r');
if (cr != string::npos){
line.erase(cr, 1);
}
//cout << line << "\n";
string field;
vector <string> columns;
size_t index = 0;
while (index < line.size()) {
while (line[index] != separator) {
field += line[index];
index++;
}
index++; // skip sep
trimSpace(field);
columns.push_back(field);
//cout << "field =" << field << "\n";
field.clear();
}
csvData.push_back(columns);
columns.clear();
}
is.close();
}*/