-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIOCSV.cpp
More file actions
249 lines (209 loc) · 5 KB
/
Copy pathIOCSV.cpp
File metadata and controls
249 lines (209 loc) · 5 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include "IOCSV.h"
#include "strConv.h"
#include <QFile>
#include <QTextStream>
using namespace std;
const int totalColumnCount = IOSpreadSheet::TOTAL_COLUMN_COUNT;
struct Record
{
Record()
{
}
Record(const Record &r)
{
for(int i = 0; i < totalColumnCount; i++)
values[i] = r[i];
}
string &operator[](size_t i)
{
return values[i];
}
const string &operator[](size_t i) const
{
return values[i];
}
string values[totalColumnCount];
};
///////////////////////////////////////////////////////////////////////////////
struct IOCSV::Data
{
Data()
{
timeStep = 0;
cachedRow = 1;
cachedDate = Date(0, 0, 0, 0, 0);
}
vector<Record> records;
Date firstDate;
Date lastDate;
int timeStep;
// cache
int cachedRow;
Date cachedDate;
};
///////////////////////////////////////////////////////////////////////////////
IOCSV::IOCSV()
{
data = new Data();
}
IOCSV::~IOCSV()
{
if(data)
delete data;
}
int IOCSV::test()
{
return 0;
}
int IOCSV::init(const char *)
{
return 0;
}
int IOCSV::open(const char *fileName)
{
data->records.clear();
QFile file(fileName);
if(!file.open(QFile::ReadOnly))
{
return -1;
}
QTextStream textStream(&file);
QStringList str;
textStream.readLine(); // skip header
while(!textStream.atEnd())
{
// str = textStream.readLine().split(',');
// Record r;
// for(int i = 0; i < totalColumnCount && i < str.size(); i++)
// r[i] = str[i].toStdString();
// data->records.push_back(r);
}
file.close();
readTimeData();
return 0;
}
string IOCSV::read(int nRow, int nColumn)
{
if(nRow >= (int)data->records.size())
return "";
return data->records[nRow - 1][nColumn - 1];
}
Date IOCSV::readTimeStamp(int nRow)
{
int year, month, day, hour, min;
strConv::from_string(read(nRow, YEAR_COL), year);
strConv::from_string(read(nRow, MONTH_COL), month);
strConv::from_string(read(nRow, DAY_COL), day);
strConv::from_string(read(nRow, HOUR_COL), hour);
strConv::from_string(read(nRow, MIN_COL), min);
return Date(year, month, day, hour, min);
}
int IOCSV::searchDateRow(const Date &searchedDate)
{
int searchedRow = 1;
if(data->cachedDate <= searchedDate)
searchedRow = data->cachedRow;
if((searchedRow = searchExcelRow(searchedDate.getYear(), searchedRow, YEAR_COL)) > -1)
{
if((searchedRow = searchExcelRow(searchedDate.getMonth(), searchedRow, MONTH_COL)) > -1)
{
if((searchedRow = searchExcelRow(searchedDate.getDay(), searchedRow, DAY_COL)) > -1)
{
if((searchedRow = searchExcelRow(searchedDate.getHour(), searchedRow, HOUR_COL)) > -1)
{
searchedRow = searchExcelRow(searchedDate.getMinute(), searchedRow, MIN_COL);
}
}
}
}
data->cachedRow = searchedRow;
data->cachedDate = searchedDate;
return searchedRow;
}
Date *IOCSV::getFirstDate()
{
return &data->firstDate;
}
Date *IOCSV::getLastDate()
{
return &data->lastDate;
}
int IOCSV::getTimeStep()
{
return data->timeStep;
}
QStringList IOCSV::extensions() const
{
return QStringList() << "*.csv";
}
void IOCSV::readTimeData()
{
int emptyCount = 0;
string buffer;
int firstRow = -1;
int lastRow = -1;
int currentRow = 1;
int currentValue;
while(emptyCount<10)
{
if((buffer = read(currentRow, YEAR_COL)).length()==0)
{
emptyCount++;
}
else if(strConv::from_string(buffer, currentValue) && currentValue > 1990)
{
if(firstRow == -1){
firstRow = currentRow;
}
lastRow = currentRow;
}
currentRow++;
}
//Create the dates if found
if(firstRow > 0 && lastRow > 0){
int year,month,day,hour,min;
buffer = read(firstRow, YEAR_COL);
strConv::from_string(buffer, year);
buffer = read(firstRow, MONTH_COL);
strConv::from_string(buffer, month);
buffer = read(firstRow, DAY_COL);
strConv::from_string(buffer, day);
buffer = read(firstRow, HOUR_COL);
strConv::from_string(buffer, hour);
buffer = read(firstRow, MIN_COL);
strConv::from_string(buffer, min);
data->firstDate = Date(year,month,day,hour,min);
buffer = read(lastRow, YEAR_COL);
strConv::from_string(buffer, year);
buffer = read(lastRow, MONTH_COL);
strConv::from_string(buffer, month);
buffer = read(lastRow, DAY_COL);
strConv::from_string(buffer, day);
buffer = read(lastRow, HOUR_COL);
strConv::from_string(buffer, hour);
buffer = read(lastRow, MIN_COL);
strConv::from_string(buffer, min);
data->lastDate = Date(year,month,day,hour,min);
}
data->timeStep = (data->lastDate-data->firstDate)/(lastRow-firstRow);
}
int IOCSV::searchExcelRow(int searchedNumber, int startRow, int column)
{
int emptyCount = 0;
string buffer;
int currentValue;
int currentRow = startRow;
while(emptyCount < 10)
{
if((buffer = read(currentRow, column)).length()==0)
{
emptyCount++;
}
else if(strConv::from_string(buffer, currentValue) && currentValue == searchedNumber)
{
return currentRow;
}
currentRow++;
}
return -1;
}