Newer
Older
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
string title;
vector <string> coordinates;
vector <string> colors;
vector <float> stepTime;
vector <vector <vector <float>>> data; // first dim = step; second dim = point; third dim = coordinate ( or component )
double stringToDouble( const std::string& s )
{
std::istringstream i(s);
double x;
if (!(i >> x))
return 0;
return x;
}
bool check_double( const std::string& s )
{
std::istringstream i(s);
double x;
if (!(i >> x))
return false;
return true;
}
bool stripeInfo(string &line, string tag)
{
auto titlePos = line.find(tag);
auto titleFound = false;
if (titlePos != std::string::npos) {
//cout << "TAG " << tag << " FOUND!!!!" << endl;
titleFound = false;
auto const pos = line.find_last_of(':');
line = line.substr(pos+1);
return true;
}
return false;
}
std::string ReplaceAll(std::string str, const std::string& from, const std::string& to)
{
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // Handles case where 'to' is a substring of 'from'
}
return str;
}
bool isEmpty(std::string str)
{
if ( ReplaceAll(str, " ", "").empty() )
return true;
else
return false;
}
void extractInfo(string line)
{
cout << "check comment info in: " << line << endl;
auto titleFound = stripeInfo(line, "Title");
auto coordinatesFound = stripeInfo(line, "Coordinates");
auto colorsFound = stripeInfo(line, "Colors");
auto stepTimeFound = stripeInfo(line, "Time");
istringstream iss(line);
if ( titleFound || coordinatesFound || colorsFound || stepTimeFound )
{
do
{
string word;
iss >> word;
auto i = 0;
if ( !word.empty() )
{
if ( titleFound )
{
cout << "Title: " << word << endl;
title += word;
}
if ( coordinatesFound )
{
cout << "Coordinate[" << coordinates.size() << "]: " << word << endl;
coordinates.push_back(word);
}
if ( colorsFound )
{
cout << "Colors[" << colors.size() << "]: " << word << endl;
colors.push_back(word);
}
if ( stepTimeFound )
{
cout << "Time: " << word << endl;
stepTime.push_back(stringToDouble(word));
}
}
}
while (iss);
}
}
void t2(void)
{
ifstream file;
file.open ("testc.dat");
string line; line.clear();
auto commentFound = false;
auto emptyLineFound = false;
//coordinates.erase(coordinates.begin());
cout << "-----------------------" << endl;
// Read file line by line
while (!file.eof())
{
getline(file, line);
// Check if it is a comment line
auto commentPos = line.find("#");
if ( commentPos == 0 ) {
//cout << "This is a comment. " << endl;
commentFound = true;
// Check if it has any information tag
extractInfo(line);
}
else {
//cout << "This is NOT a comment. " << endl;
commentFound = false;
}
// Check if the line is empty (will start new group of data)
if ( isEmpty(line) ) {
emptyLineFound = true;
cout << "EMPTY LINE" << endl;
}
else if ( !commentFound ) {
emptyLineFound = false;
// Split line into words
istringstream iss(line);
do
{
string word;
iss >> word;
if ( !word.empty())
{
cout << "Word: " << word;
if ( check_double(word) )
cout << " : is a double" << endl;
else
cout << " : is NOT a double" << endl;
}
}
while (iss);
}
cout << "-----------------------" << endl;
}
}
void show(void)
{
cout << "==============================" << endl;
cout << "Title = " << title << endl;
for (auto i = 0; i < coordinates.size(); i++)
cout << "Coordinate[" << i << "] = " << coordinates[i] << endl;
for (auto i = 0; i < stepTime.size(); i++)
cout << "Time[" << i << "] = " << stepTime[i] << endl;
for (auto i = 0; i < colors.size(); i++)
cout << "Colors[" << i << "] = " << colors[i] << endl;
cout << "==============================" << endl;
}
int main (void)
{
t2();
show();
}