-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseFunctions.cpp
79 lines (70 loc) · 1.93 KB
/
BaseFunctions.cpp
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
/*
* BaseFunctions.cpp
*
* Author: Florian Lungu
*
* Version History:
* 2020-Jun-15 Initial creation
*/
#include <string>
#include <vector>
using namespace std;
const string WHITESPACE = " \n\r\t\f\v";
string ltrim(const string& s) {
size_t start = s.find_first_not_of(WHITESPACE);
return (start == string::npos) ? "" : s.substr(start);
}
string rtrim(const string& s) {
size_t end = s.find_last_not_of(WHITESPACE);
return (end == string::npos) ? "" : s.substr(0, end + 1);
}
string trim(const string& s) {
return rtrim(ltrim(s));
}
string ReplaceAll(string str, const string& from, const string& to) {
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // Handles case where 'to' is a substring of 'from'
}
return str;
}
vector<string> CsvlinePopulate(string line, string delimiter)
{
vector<string> record;
int linepos=0;
bool inquotes=false;
string c;
int linemax=line.length();
string curstring;
record.clear();
while(linepos < linemax){
c = line.substr(linepos,1);
if (!inquotes && curstring.length()==0 && c=="\"") {
//beginquotechar
inquotes=true;
} else if (inquotes && c=="\"") {
//quotechar
if ( (linepos+1 <linemax) && (line.substr(linepos+1,1)=="\"") ) {
//encountered 2 double quotes in a row (resolves to 1 double quote)
curstring += c;
linepos++;
} else {
//endquotechar
inquotes=false;
}
} else if (!inquotes && c==delimiter) {
//end of field
record.push_back( curstring );
curstring="";
} else if (!inquotes && (c=="\r" || c=="\n") ) {
record.push_back( curstring );
return record;
} else {
curstring += c;
}
linepos++;
}
record.push_back( curstring );
return record;
}