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
#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;
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,e.i.a new "step")
if ( !emptyLineFound )
{
data.push_back(vector<vector <float> >); // Create a "step" in data multi dim vector
emptyLineFound = true;
}
cout << "EMPTY LINE" << endl;
}
// Is line is not empty and not a comment so it contains data (coordinates)
else if ( !commentFound ) {
emptyLineFound = false;
// Split line into words
istringstream iss(line);
data[data.size() -1].push_back(vector<float>);
do
{
string word;
iss >> word;
if ( !word.empty())
{
cout << "Word: " << word;
if ( check_double(word) )
cout << " : is a double" << endl;
data[data.size() -1].push_back(vector<float>);
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
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;
}
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
void test-vector1(void)
{
vector< vector<int> > vec;
for (int i = 0; i < 10; i++) {
vector<int> row; // Create an empty row
for (int j = 0; j < 20; j++) {
row.push_back(i * j); // Add an element (column) to the row
}
vec.push_back(row); // Add the row to the main vector
}
}
void test-vector2(void)
{
vector< vector<int> > vec;
for (int i = 0; i < 10; i++) {
vec.push_back(vector<int>()); // Add an empty row
}
for (int j = 0; j < 20; j++) {
for (int i = 0; i < vec.size(); i++) {
vec[i].push_back(i * j); // Add column to all rows
}
// You could also use iterators:
for (vector< vector<int> >::iterator it = vec.begin(); it != vec.end(); ++it) {
it->push_back(j); // Add column to all rows
}
}
}