Repository navigation
Expand file tree
/
Copy pathdata.c
More file actions
372 lines (298 loc) · 7.4 KB
/
Copy pathdata.c
File metadata and controls
372 lines (298 loc) · 7.4 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
* Copyright (c) 2008 Rainer Clasen
*
* This program is free software; you can redistribute it and/or modify
* it under the terms described in the file LICENSE included in this
* distribution.
*
*/
#include "common.h"
/*
* allocate and initialize new data structure to hold data retrieved
* from PCV or file
*
* on success pointer is returned
* returns NULL on error and sets errno.
*/
srmio_data_t srmio_data_new( srmio_error_t *err )
{
srmio_data_t data;
if( NULL == (data = malloc( sizeof( struct _srmio_data_t) )) ){
srmio_error_errno( err, "new data" );
return NULL;
}
memset( data, 0, sizeof(struct _srmio_data_t));
if( NULL == (data->chunks = malloc(sizeof(srmio_chunk_t *)))){
srmio_error_errno( err, "new data chunks" );
goto clean1;
}
*data->chunks = NULL;
if( NULL == (data->marker = malloc(sizeof(srmio_marker_t *)))){
srmio_error_errno( err, "new data marker" );
goto clean2;
}
*data->marker = NULL;
return data;
clean2:
free( data->chunks );
clean1:
free( data );
return NULL;
}
/*
* copies "header" to a newly allocated data structure
*/
srmio_data_t srmio_data_header( srmio_data_t src, srmio_error_t *err )
{
srmio_data_t dst;
assert(src);
if( NULL == ( dst = srmio_data_new( err ) ))
return NULL;
if( src->notes && NULL == ( dst->notes = strdup( src->notes ) ) ){
srmio_error_errno( err, "data notes" );
goto clean1;
}
if( src->athlete && NULL == ( dst->athlete = strdup( src->athlete ) ) ){
srmio_error_errno( err, "data athlete");
goto clean1;
}
dst->slope = src->slope;
dst->zeropos = src->zeropos;
dst->circum = src->circum;
return dst;
clean1:
srmio_data_free(dst);
return NULL;
}
/*
* add chunk to end of data's chunk list. Extends list when necessary.
* Chunk is not copied.
*
* on success 0 is returned
* returns -1 and sets errno on error
*/
bool srmio_data_add_chunkp( srmio_data_t data, srmio_chunk_t chunk, srmio_error_t *err )
{
if( data->cused >= data->cavail ){
srmio_chunk_t *tmp;
if( data->cavail > UINT_MAX - 1001 ){
srmio_error_set( err, "too many chunks" );
return false;
}
if( NULL == (tmp = realloc( data->chunks,
(data->cavail + 1001) * sizeof(srmio_chunk_t)))){
srmio_error_errno( err, "enlarge data chunks" );
return false;
}
data->cavail += 1000;
data->chunks = tmp;
}
data->chunks[data->cused] = chunk;
data->chunks[++ data->cused] = NULL;
return true;
}
/*
* add copy of chunk to end of data's chunk_list.
*
* on success 0 is returned
* returns -1 and sets errno on error
*/
bool srmio_data_add_chunk( srmio_data_t data, srmio_chunk_t chunk, srmio_error_t *err )
{
srmio_chunk_t nc;
if( NULL == (nc = srmio_chunk_clone(chunk, err)))
return false;
return srmio_data_add_chunkp( data, nc, err );
}
/*
* add marker to end of data's marker list. Extends list when necessary.
* Marker is not copied.
*
* on success 0 is returned
* returns -1 and sets errno on error
*/
bool srmio_data_add_markerp( srmio_data_t data, srmio_marker_t mk, srmio_error_t *err )
{
if( data->mused >= data->mavail ){
srmio_marker_t *tmp;
if( data->mavail > UINT_MAX - 11 ){
srmio_error_set( err, "too many marker");
return false;
}
if( NULL == (tmp = realloc( data->marker,
(data->mavail + 11) * sizeof(srmio_marker_t)))){
srmio_error_errno( err, "enlarge data marker" );
return false;
}
data->mavail += 10;
data->marker = tmp;
}
data->marker[data->mused] = mk;
data->marker[++ data->mused] = NULL;
return true;
}
/*
* create new marker and add it to data's marker list
*
* parameters:
* data: the data structure to modify
* first: number of first marked chunk (counts from 0)
* last: number of first marked chunk
*
* on success 0 is returned
* returns -1 and sets errno on error
*/
bool srmio_data_add_marker( srmio_data_t data, unsigned first, unsigned last, srmio_error_t *err )
{
srmio_marker_t mk;
DPRINTF( "%u to %u",
first,
last );
if( first >= data->cused ){
srmio_error_set(err, "marker out of range: first=%u > used=%u",
first, data->cused );
return false;
}
if( first > last ){
srmio_error_set(err, "marker out of range: first=%u > last=%u",
first, last );
return false;
}
if( NULL == (mk = srmio_marker_new( err ) ))
return false;
mk->first = first;
mk->last = last;
if( ! srmio_data_add_markerp( data, mk, err ) )
goto clean1;
return true;
clean1:
free(mk);
return false;
}
/*
* return start time of first chunk
*/
bool srmio_data_time_start( srmio_data_t data, srmio_time_t *start, srmio_error_t *err )
{
assert( data );
assert( start );
if( ! data->cused ){
srmio_error_set( err, "no data available" );
return false;
}
*start = data->chunks[0]->time;
return true;
}
bool srmio_data_time_end( srmio_data_t data, srmio_time_t *end, srmio_error_t *err )
{
assert( data );
assert( end );
if( ! data->cused ){
srmio_error_set( err, "no data available" );
return false;
}
*end = data->chunks[data->cused -1]->time
+ data->chunks[data->cused -1]->dur;
return true;
}
/*
* return common recording interval
*/
bool srmio_data_recint( srmio_data_t data, srmio_time_t *recint, srmio_error_t *err )
{
assert( data );
assert( recint );
if( ! data->cused ){
srmio_error_set( err, "no data available" );
return false;
}
*recint = data->chunks[data->cused-1]->dur;
return true;
}
/*
* find gaps in chunklist (non-continuos time), allocate and build list
* with marker identifying the continuos blocks.
*
* parameters:
* data: the data structure to check
*
* on success pointer to list is returned
* returns NULL on error and sets errno.
*/
srmio_marker_t *srmio_data_blocks( srmio_data_t data, srmio_error_t *err )
{
srmio_marker_t *blocks;
unsigned avail = 10;
unsigned used = 1;
unsigned i;
if( data->cused < 1 ){
srmio_error_set( err, "no data available");
return NULL;
}
if( NULL == (blocks = malloc( (1+ avail) * (sizeof(srmio_marker_t))))){
srmio_error_errno( err, "data blocks" );
return NULL;
}
if( NULL == (*blocks = srmio_marker_new( err )))
goto clean1;
blocks[0]->first = 0;
blocks[0]->last = data->cused -1;
blocks[1] = NULL;
for( i=1; i < data->cused; ++i ){
srmio_chunk_t prev = data->chunks[i-1];
srmio_chunk_t this = data->chunks[i];
if( prev->time + prev->dur != this->time ){
if( used >= avail ){
srmio_marker_t *tmp;
unsigned ns = avail + 10;
if( ns < avail ){
srmio_error_set( err, "too many blocks");
goto clean2;
}
if( NULL == (tmp = realloc(blocks, (1+ ns)
* (sizeof(srmio_marker_t)) ))){
srmio_error_errno( err, "enlage data blocks" );
goto clean2;
}
blocks = tmp;
avail = ns;
}
DPRINTF("found gap @%u "
"%.1f - %.1f = %.1f",
i,
(double)prev->time/10,
(double)this->time/10,
(double)(this->time - prev->time)/10);
if( NULL == (blocks[used] = srmio_marker_new(err) ) )
goto clean2;
blocks[used-1]->last = i -1;
blocks[used]->first = i;
blocks[used]->last = data->cused -1;
blocks[++used] = NULL;
}
}
return blocks;
clean2:
for( i=0; i < used; ++i )
srmio_marker_free( blocks[i] );
clean1:
free(blocks);
return NULL;
}
/*
* free all memory held in data structure
*/
void srmio_data_free( srmio_data_t data )
{
unsigned i;
if( data == NULL )
return;
for( i=0; i < data->cused; ++i )
srmio_chunk_free(data->chunks[i]);
free(data->chunks);
for( i=0; i < data->mused; ++i )
srmio_marker_free(data->marker[i]);
free(data->marker);
free(data->notes);
free(data);
}