-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadTimestamp.cpp
More file actions
executable file
·275 lines (230 loc) · 6.29 KB
/
ReadTimestamp.cpp
File metadata and controls
executable file
·275 lines (230 loc) · 6.29 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
* @file ReadTimestamp.cpp
* @ingroup DataManagement
* @author Dominique Vaufreydaz, Grenoble Alpes University, Inria
* @copyright All right reserved.
*/
#include "ReadTimestamp.h"
using namespace std;
using namespace MobileRGBD;
const size_t ReadTimestamp::DefaultLineBufferSize = 10*1024*1024; /*!< @brief Default buffer size for line reading (default 10MiB) */
const unsigned short int ReadTimestamp::DefaultValidityTimeInMs = 33; /*!< @brief When searching for a specified timestamp, DefaultValidityTimeInMs specifies a threshold to for validity (33ms). */
/** @brief Constructor. Create a ReadTimeStamp object using specific file.
*
* @param FileName [in] Name of the file to open (even with '/' separator under Windows as Windows handles it also as a folder/file separator).
* @param SizeOfLineBuffer [in] Size of buffer to read each line of the file (default=DefaultLineBufferSize).
*/
ReadTimestamp::ReadTimestamp( const string& FileName, size_t SizeOfLineBuffer /* = 10 MB */ )
{
// init internal variables
FiletoOpen = FileName;
EndOfTimestampPosition = 0;
CurrentTimestamp.time = 0;
CurrentTimestamp.millitm = 0;
CurrentTimestamp.timezone = 0;
CurrentTimestampIsInitialized = false;
PreviousTimestampPosInFile[0] = -1;
PreviousTimestampPosInFile[1] = -1;
// Allocate line buffer
LineBufferSize = 0;
LineBuffer = new char[SizeOfLineBuffer];
// Ok size is good
LineBufferSize = (int)SizeOfLineBuffer;
}
/** @brief virtual destructor (always).
*/
ReadTimestamp::~ReadTimestamp()
{
Close();
if ( LineBuffer != nullptr )
{
delete LineBuffer;
}
}
/** @brief Restart file at beginning (if file is closed, file is re-opened).
*/
void ReadTimestamp::Reinit()
{
if ( fin == (FILE*)NULL )
{
#ifdef DEBUG
// fprintf( stderr, "Try to open '%s'\n", FiletoOpen.c_str() );
#endif
fin.Open( FiletoOpen.c_str(), DataFile::READ_MODE );
}
else
{
fseek( fin, 0, SEEK_SET );
}
PreviousTimestampPosInFile[0] = -1;
PreviousTimestampPosInFile[1] = -1;
CurrentTimestamp.time = 0;
CurrentTimestamp.millitm = 0;
CurrentTimestamp.timezone = 0;
CurrentTimestampIsInitialized = false;
}
/** @brief Close file.
*/
void ReadTimestamp::Close()
{
if ( fin != (FILE*)NULL )
{
fin.Close();
}
}
/** @brief Search for a specific timestamp in the file.
*
* @param RequestedTimestamp [in] Timestamp to search for.
* @param ValidityTimeInMs [in]
* @return Searching for a line starting with the RequestedTimestamp. Return true if this line exists. If the previous timestamp is less than ValidityTimeInMs ms before,
it is considered as valid (for synchronous read of several files). In all other cases, return false;
*/
bool ReadTimestamp::SearchDataForTimestamp(const TimeB &RequestedTimestamp, unsigned short int ValidityTimeInMs /* DefaultValidityTimeInMs */ )
{
int ValidityTime = -(int)ValidityTimeInMs;
if ( fin == (FILE*)NULL )
{
// Open and get next timestamp
Reinit();
}
if ( fin == (FILE*)NULL )
{
return false;
}
if ( feof(fin) )
{
return CurrentTimestampIsInitialized;
}
bool TotalFileParsed = false;
if ( CurrentTimestampIsInitialized == false )
{
GetNextTimestamp();
}
while( CurrentTimestampIsInitialized == true )
{
int Comp = CompareTime( RequestedTimestamp, CurrentTimestamp );
// Requested time stamp is in future
if ( Comp > 0 )
{
if ( fin != nullptr && feof(fin) )
{
return (CurrentTimestampIsInitialized && Comp <= 100); // let's say that if last data is older taht 100ms, we did not take care of it anymore
}
// Cancel current result
LineBuffer[0] = '\0';
EndOfTimestampPosition = 0;
/*CurrentTimestamp.time = 0;
CurrentTimestamp.millitm = 0;
CurrentTimestamp.timezone = 0;*/
CurrentTimestampIsInitialized = false;
// Go ahead
GetNextTimestamp();
continue;
}
// Here we have no data, our data is in the futur
if ( Comp < 0 )
{
if ( fin != nullptr && feof(fin) )
{
// Our current data is in the futur, we can not search for new data as we are at end of the file
return false;
}
if ( PreviousTimestamp.time != 0 )
{
// We have a previous timestamp, check if it is near enough
if ( CompareTime( RequestedTimestamp, PreviousTimestamp ) > ValidityTime )
{
Rewind();
GetNextTimestamp();
return true;
}
}
// First timestamp is after Requested time
return false;
}
// Here we have the right timestamp, Comp == 0
return true;
}
return false;
}
/** @brief Get the next timestamp of the file if any.
*
* @return True is the next timestamp has been retrieve.
*/
bool ReadTimestamp::GetPreviousTimestamp()
{
return (Rewind() && GetNextTimestamp());
}
/** @brief Get the next timestamp of the file if any.
*
* @return True is the previous timestamp has been retrieve.
*/
bool ReadTimestamp::GetNextTimestamp()
{
if ( fin == (FILE*)NULL )
{
Reinit();
}
if ( fin == (FILE*)NULL )
{
return false;
}
if ( feof(fin) )
{
// return last value or none
return CurrentTimestampIsInitialized;
}
while( !feof(fin) )
{
int iTmp;
int length = 0;
TimeB lTimestamp;
// Remember where I am
AddTimestampPos();
// try to read a line
if ( fgets( LineBuffer, LineBufferSize-1, fin ) == (char*)NULL )
{
break;
}
// Ok, we have a line, remeber CurrentTimestamp
PreviousTimestamp.time = CurrentTimestamp.time;
PreviousTimestamp.millitm = CurrentTimestamp.millitm;
PreviousTimestamp.timezone = CurrentTimestamp.timezone;
// try to parse line
if ( sscanf( LineBuffer, "%d.%hu%*[ \t]%n", &iTmp, &lTimestamp.millitm, &length ) != 2 )
{
continue;
}
lTimestamp.time = iTmp;
// Copy data to internal
CurrentTimestamp = lTimestamp;
EndOfTimestampPosition = length;
CurrentTimestampIsInitialized = true;
return true;
}
// Ok, close file and say we wo not find it
// fclose( fin );
// fin = (FILE*)NULL;
return false; // say end of file
}
/** @brief Rewind if possible to the previous timestamp.
*
* @return True is the rewind was possible and done.
*/
bool ReadTimestamp::Rewind()
{
if ( fin != (FILE*)NULL )
{
if ( PreviousTimestampPosInFile[0] != -1 )
{
fseek( fin, PreviousTimestampPosInFile[0], SEEK_SET );
PreviousTimestampPosInFile[0] = -1;
PreviousTimestampPosInFile[1] = -1;
PreviousTimestamp.time = 0;
PreviousTimestamp.millitm = 0;
PreviousTimestamp.timezone = 0;
return true;
}
}
return false;
}