-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vals.cpp
79 lines (79 loc) · 1.93 KB
/
Vals.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
#include <cctype>
#include "Vals.h"
using namespace std;
namespace sict{
string Vals::trim(string s){
size_t cur = 0;
while (s.length() != 0 && isspace(s.at(cur))){
s.erase(cur, 1);
}
cur = s.length() - 1;
while (s.length() != 0 && isspace(s.at(cur))){
s.erase(cur, 1);
cur = s.length() - 1;
}
return s;
}
Vals::Vals(char delim){
_delim = delim;
}
void Vals::set(const char* str, char delim){
assign(str);
if (delim) _delim = delim;
set();
}
void Vals::set(char delim){
_delim = delim;
set();
}
void Vals::clear(){
string::clear();
_values.clear();
}
void Vals::set(){
size_t first = find_first_of(_delim);
size_t last = find_last_of(_delim);
_values.clear();
if (first != string::npos){
if (first == last){
_values.push_back(trim(substr(0, first)));
_values.push_back(trim(substr(first + 1, length() - first)));
}
else{
size_t from = 0;
_values.push_back(trim(substr(from, first)));
while (first != last){
from = first + 1;
first = find_first_of(_delim, from);
_values.push_back(trim(substr(from, first - from)));
}
_values.push_back(trim(substr(first+1, length() - first)));
}
}
else if (length() > 0){
_values.push_back(trim(*this));
}
}
Vals::Vals(std::string csv, char delim) : std::string(csv){
_delim = delim;
set();
}
int Vals::size(){
return _values.size();
}
string& Vals::operator[](int index){
string* val = &_nothing;
if (size() > 0) val = &_values[index%size()];
return *val;
}
std::ifstream& operator>>(std::ifstream& ifstr, Vals& V){
char line[2048] = "";
ifstr.getline(line, 2048, '\n');
V.clear();
// not failed and not a comment
if (!ifstr.fail() && !(line[0] == '-' && line[1] == '-')){
V.set(line);
}
return ifstr;
}
}