Debian Codegraph @

matio-1.5.0/src/matvar_struct.c

raw
1 /*
2 * Copyright (C) 2012 Christopher C. Hulbert
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY CHRISTOPHER C. HULBERT ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL CHRISTOPHER C. HULBERT OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include <stdlib.h>
29 #include <string.h>
30 #include "matio_private.h"
31
32 /** @brief Creates a structure MATLAB variable with the given name and fields
33 *
34 * @ingroup MAT
35 * @param name Name of the structure variable to create
36 * @param rank Rank of the variable
37 * @param dims array of dimensions of the variable of size rank
38 * @param fields Array of @c nfields fieldnames
39 * @param nfields Number of fields in the structure
40 * @param matvar Pointer to store the new structure MATLAB variable
41 * @return @c MATIO_SUCCESS if successful, or an error value (See
42 * @ref enum matio_error_t).
43 */
44 matvar_t *
45 Mat_VarCreateStruct(const char *name,int rank,size_t *dims,const char **fields,
46 unsigned nfields)
47 {
48 int i, nmemb = 1;
49 matvar_t *matvar;
50
51 if ( NULL == dims )
52 return NULL;
53
54 matvar = Mat_VarCalloc();
55 if ( NULL == matvar )
56 return NULL;
57
58 matvar->compression = MAT_COMPRESSION_NONE;
59 if ( NULL != name )
60 matvar->name = strdup(name);
61 matvar->rank = rank;
62 matvar->dims = malloc(matvar->rank*sizeof(*matvar->dims));
63 for ( i = 0; i < matvar->rank; i++ ) {
64 matvar->dims[i] = dims[i];
65 nmemb *= dims[i];
66 }
67 matvar->class_type = MAT_C_STRUCT;
68 matvar->data_type = MAT_T_STRUCT;
69
70 matvar->data_size = sizeof(matvar_t *);
71
72 if ( nfields ) {
73 matvar->internal->num_fields = nfields;
74 matvar->internal->fieldnames =
75 malloc(nfields*sizeof(*matvar->internal->fieldnames));
76 if ( NULL == matvar->internal->fieldnames ) {
77 Mat_VarFree(matvar);
78 matvar = NULL;
79 } else {
80 for ( i = 0; i < nfields; i++ ) {
81 if ( NULL == fields[i] ) {
82 Mat_VarFree(matvar);
83 matvar = NULL;
84 break;
85 } else {
86 matvar->internal->fieldnames[i] = strdup(fields[i]);
87 }
88 }
89 }
90 if ( NULL != matvar && nmemb > 0 && nfields > 0 ) {
91 matvar->nbytes = nmemb*nfields*matvar->data_size;
92 matvar->data = malloc(matvar->nbytes);
93 }
94 }
95
96 return matvar;
97 }
98
99 /** @brief Adds a field to a structure
100 *
101 * Adds the given field to the structure. fields should be an array of matvar_t
102 * pointers of the same size as the structure (i.e. 1 field per structure
103 * element).
104 * @ingroup MAT
105 * @param matvar Pointer to the Structure MAT variable
106 * @param fields Array of fields to be added
107 * @retval 0 on success
108 */
109 int
110 Mat_VarAddStructField(matvar_t *matvar,const char *fieldname)
111 {
112 int i, f, nfields, nmemb, cnt = 0;
113 matvar_t **new_data, **old_data;
114
115 if ( matvar == NULL || fieldname == NULL )
116 return -1;
117 nmemb = 1;
118 for ( i = 0; i < matvar->rank; i++ )
119 nmemb *= matvar->dims[i];
120
121 nfields = matvar->internal->num_fields+1;
122 matvar->internal->num_fields = nfields;
123 matvar->internal->fieldnames =
124 realloc(matvar->internal->fieldnames,
125 nfields*sizeof(*matvar->internal->fieldnames));
126 matvar->internal->fieldnames[nfields-1] = strdup(fieldname);
127
128 new_data = malloc(nfields*nmemb*sizeof(*new_data));
129 if ( new_data == NULL )
130 return -1;
131
132 old_data = matvar->data;
133 for ( i = 0; i < nmemb; i++ ) {
134 for ( f = 0; f < nfields-1; f++ )
135 new_data[cnt++] = old_data[i*(nfields-1)+f];
136 new_data[cnt++] = NULL;
137 }
138
139 free(matvar->data);
140 matvar->data = new_data;
141 matvar->nbytes = nfields*nmemb*sizeof(*new_data);
142
143 return 0;
144 }
145
146 /** @brief Returns the number of fields in a structure variable
147 *
148 * Returns the number of fields in the given structure.
149 * @ingroup MAT
150 * @param matvar Structure matlab variable
151 * @returns Number of fields
152 */
153 unsigned
154 Mat_VarGetNumberOfFields(matvar_t *matvar)
155 {
156 int nfields;
157 if ( matvar == NULL || matvar->class_type != MAT_C_STRUCT ||
158 NULL == matvar->internal ) {
159 nfields = 0;
160 } else {
161 nfields = matvar->internal->num_fields;
162 }
163 return nfields;
164 }
165
166 /** @brief Returns the fieldnames of a structure variable
167 *
168 * Returns the fieldnames for the given structure. The returned pointers are
169 * internal to the structure and should not be free'd.
170 * @ingroup MAT
171 * @param matvar Structure matlab variable
172 * @returns Array of fieldnames
173 */
174 char * const *
175 Mat_VarGetStructFieldnames(const matvar_t *matvar)
176 {
177 if ( matvar == NULL || matvar->class_type != MAT_C_STRUCT ||
178 NULL == matvar->internal ) {
179 return NULL;
180 } else {
181 return matvar->internal->fieldnames;
182 }
183 }
184
185 /** @brief Finds a field of a structure by the field's index
186 *
187 * Returns a pointer to the structure field at the given 0-relative index.
188 * @ingroup MAT
189 * @param matvar Pointer to the Structure MAT variable
190 * @param field_index 0-relative index of the field.
191 * @param index linear index of the structure array
192 * @return Pointer to the structure field on success, NULL on error
193 */
194 matvar_t *
195 Mat_VarGetStructFieldByIndex(matvar_t *matvar,size_t field_index,size_t index)
196 {
197 int i, nfields;
198 matvar_t *field = NULL;
199 size_t nmemb = 1;
200
201 if ( matvar == NULL || matvar->class_type != MAT_C_STRUCT ||
202 matvar->data_size == 0 )
203 return field;
204
205 nmemb = 1;
206 for ( i = 0; i < matvar->rank; i++ )
207 nmemb *= matvar->dims[i];
208
209 nfields = matvar->internal->num_fields;
210
211 if ( nmemb > 0 && index >= nmemb ) {
212 Mat_Critical("Mat_VarGetStructField: structure index out of bounds");
213 } else if ( nfields > 0 ) {
214 if ( field_index > nfields ) {
215 Mat_Critical("Mat_VarGetStructField: field index out of bounds");
216 } else {
217 field = *((matvar_t **)matvar->data+index*nfields+field_index);
218 }
219 }
220
221 return field;
222 }
223
224 /** @brief Finds a field of a structure by the field's name
225 *
226 * Returns a pointer to the structure field at the given 0-relative index.
227 * @ingroup MAT
228 * @param matvar Pointer to the Structure MAT variable
229 * @param name Name of the structure field
230 * @param index linear index of the structure array
231 * @return Pointer to the structure field on success, NULL on error
232 */
233 matvar_t *
234 Mat_VarGetStructFieldByName(matvar_t *matvar,const char *field_name,
235 size_t index)
236 {
237 int i, nfields, field_index;
238 matvar_t *field = NULL;
239 size_t nmemb;
240
241 if ( matvar == NULL || matvar->class_type != MAT_C_STRUCT ||
242 matvar->data_size == 0 )
243 return field;
244
245 nmemb = 1;
246 for ( i = 0; i < matvar->rank; i++ )
247 nmemb *= matvar->dims[i];
248
249 nfields = matvar->internal->num_fields;
250 field_index = -1;
251 for ( i = 0; i < nfields; i++ ) {
252 if ( !strcmp(matvar->internal->fieldnames[i],field_name) ) {
253 field_index = i;
254 break;
255 }
256 }
257
258 if ( index >= nmemb ) {
259 Mat_Critical("Mat_VarGetStructField: structure index out of bounds");
260 } else if ( field_index >= 0 ) {
261 field = *((matvar_t **)matvar->data+index*nfields+field_index);
262 }
263
264 return field;
265 }
266
267 /** @brief Finds a field of a structure
268 *
269 * Returns a pointer to the structure field at the given 0-relative index.
270 * @ingroup MAT
271 * @param matvar Pointer to the Structure MAT variable
272 * @param name_or_index Name of the field, or the 1-relative index of the field
273 * If the index is used, it should be the address of an integer variable whose
274 * value is the index number.
275 * @param opt MAT_BY_NAME if the name_or_index is the name or MAT_BY_INDEX if
276 * the index was passed.
277 * @param index linear index of the structure to find the field of
278 * @return Pointer to the Structure Field on success, NULL on error
279 */
280 matvar_t *
281 Mat_VarGetStructField(matvar_t *matvar,void *name_or_index,int opt,int index)
282 {
283 int i, err = 0, nfields, nmemb;
284 matvar_t *field = NULL;
285
286 nmemb = 1;
287 for ( i = 0; i < matvar->rank; i++ )
288 nmemb *= matvar->dims[i];
289
290 nfields = matvar->internal->num_fields;
291
292 if ( index < 0 || (nmemb > 0 && index >= nmemb ))
293 err = 1;
294 else if ( nfields < 1 )
295 err = 1;
296
297 if ( !err && (opt == MAT_BY_INDEX) ) {
298 size_t field_index = *(int *)name_or_index;
299 if ( field_index > 0 )
300 field = Mat_VarGetStructFieldByIndex(matvar,field_index-1,index);
301 } else if ( !err && (opt == MAT_BY_NAME) ) {
302 field = Mat_VarGetStructFieldByName(matvar,name_or_index,index);
303 }
304
305 return field;
306 }
307
308 /** @brief Indexes a structure
309 *
310 * Finds structures of a structure array given a start, stride, and edge for
311 * each dimension. The structures are placed in a new structure array. If
312 * copy_fields is non-zero, the indexed structures are copied and should be
313 * freed, but if copy_fields is zero, the indexed structures are pointers to
314 * the original, but should still be freed. The structures have a flag set
315 * so that the structure fields are not freed.
316 *
317 * Note that this function is limited to structure arrays with a rank less than
318 * 10.
319 *
320 * @ingroup MAT
321 * @param matvar Structure matlab variable
322 * @param start vector of length rank with 0-relative starting coordinates for
323 * each diemnsion.
324 * @param stride vector of length rank with strides for each diemnsion.
325 * @param edge vector of length rank with the number of elements to read in
326 * each diemnsion.
327 * @param copy_fields 1 to copy the fields, 0 to just set pointers to them.
328 * @returns A new structure array with fields indexed from @c matvar.
329 */
330 matvar_t *
331 Mat_VarGetStructs(matvar_t *matvar,int *start,int *stride,int *edge,
332 int copy_fields)
333 {
334 size_t i,j,N,I,nfields,field,idx[10] = {0,},cnt[10] = {0,},dimp[10] = {0,};
335 matvar_t **fields, *struct_slab;
336
337 if ( (matvar == NULL) || (start == NULL) || (stride == NULL) ||
338 (edge == NULL) ) {
339 return NULL;
340 } else if ( matvar->rank > 9 ) {
341 return NULL;
342 } else if ( matvar->class_type != MAT_C_STRUCT ) {
343 return NULL;
344 }
345
346 struct_slab = Mat_VarDuplicate(matvar,0);
347 if ( !copy_fields )
348 struct_slab->mem_conserve = 1;
349
350 nfields = matvar->internal->num_fields;
351
352 dimp[0] = matvar->dims[0];
353 N = edge[0];
354 I = start[0];
355 struct_slab->dims[0] = edge[0];
356 idx[0] = start[0];
357 for ( i = 1; i < matvar->rank; i++ ) {
358 idx[i] = start[i];
359 dimp[i] = dimp[i-1]*matvar->dims[i];
360 N *= edge[i];
361 I += start[i]*dimp[i-1];
362 struct_slab->dims[i] = edge[i];
363 }
364 I *= nfields;
365 struct_slab->nbytes = N*nfields*sizeof(matvar_t *);
366 struct_slab->data = malloc(struct_slab->nbytes);
367 if ( struct_slab->data == NULL ) {
368 Mat_VarFree(struct_slab);
369 return NULL;
370 }
371 fields = struct_slab->data;
372 for ( i = 0; i < N; i+=edge[0] ) {
373 for ( j = 0; j < edge[0]; j++ ) {
374 for ( field = 0; field < nfields; field++ ) {
375 if ( copy_fields )
376 fields[(i+j)*nfields+field] =
377 Mat_VarDuplicate(*((matvar_t **)matvar->data + I),1);
378 else
379 fields[(i+j)*nfields+field] =
380 *((matvar_t **)matvar->data + I);
381 I++;
382 }
383 if ( stride != 0 )
384 I += (stride[0]-1)*nfields;
385 }
386 idx[0] = start[0];
387 I = idx[0];
388 cnt[1]++;
389 idx[1] += stride[1];
390 for ( j = 1; j < matvar->rank; j++ ) {
391 if ( cnt[j] == edge[j] ) {
392 cnt[j] = 0;
393 idx[j] = start[j];
394 cnt[j+1]++;
395 idx[j+1] += stride[j+1];
396 }
397 I += idx[j]*dimp[j-1];
398 }
399 I *= nfields;
400 }
401 return struct_slab;
402 }
403
404 /** @brief Indexes a structure
405 *
406 * Finds structures of a structure array given a single (linear)start, stride,
407 * and edge. The structures are placed in a new structure array. If
408 * copy_fields is non-zero, the indexed structures are copied and should be
409 * freed, but if copy_fields is zero, the indexed structures are pointers to
410 * the original, but should still be freed since the mem_conserve flag is set
411 * so that the structures are not freed.
412 * MAT File version must be 5.
413 * @ingroup MAT
414 * @param matvar Structure matlab variable
415 * @param start starting index (0-relative)
416 * @param stride stride (1 reads consecutive elements)
417 * @param edge Number of elements to read
418 * @param copy_fields 1 to copy the fields, 0 to just set pointers to them.
419 * @returns A new structure with fields indexed from matvar
420 */
421 matvar_t *
422 Mat_VarGetStructsLinear(matvar_t *matvar,int start,int stride,int edge,
423 int copy_fields)
424 {
425 int i, I = 0, field, nfields;
426 matvar_t *struct_slab, **fields;
427
428 /* FIXME: Check allocations */
429 if ( matvar == NULL || matvar->rank > 10 ) {
430 struct_slab = NULL;
431 } else {
432
433 struct_slab = Mat_VarDuplicate(matvar,0);
434 if ( !copy_fields )
435 struct_slab->mem_conserve = 1;
436
437 nfields = matvar->internal->num_fields;
438
439 struct_slab->nbytes = edge*nfields*sizeof(matvar_t *);
440 struct_slab->data = malloc(struct_slab->nbytes);
441 struct_slab->dims[0] = edge;
442 struct_slab->dims[1] = 1;
443 fields = struct_slab->data;
444 I = start*nfields;
445 for ( i = 0; i < edge; i++ ) {
446 if ( copy_fields ) {
447 for ( field = 0; field < nfields; field++ ) {
448 fields[i*nfields+field] =
449 Mat_VarDuplicate(*((matvar_t **)matvar->data+I),1);
450 I++;
451 }
452 } else {
453 for ( field = 0; field < nfields; field++ ) {
454 fields[i*nfields+field] = *((matvar_t **)matvar->data + I);
455 I++;
456 }
457 }
458 I += (stride-1)*nfields;
459 }
460 }
461 return struct_slab;
462 }
463
464 /** @brief Sets the structure field to the given variable
465 *
466 * Sets the structure field specified by the 0-relative field index
467 * @c field_index for the given 0-relative structure index @c index to
468 * @c field.
469 * @ingroup MAT
470 * @param matvar Pointer to the structure MAT variable
471 * @param field_index 0-relative index of the field.
472 * @param index linear index of the structure array
473 * @param field New field variable
474 * @return Pointer to the previous field (NULL if no previous field)
475 */
476 matvar_t *
477 Mat_VarSetStructFieldByIndex(matvar_t *matvar,size_t field_index,size_t index,
478 matvar_t *field)
479 {
480 int i, nfields;
481 matvar_t *old_field = NULL;
482 size_t nmemb = 1;
483
484 if ( matvar == NULL || matvar->class_type != MAT_C_STRUCT ||
485 matvar->data == NULL )
486 return old_field;
487
488 nmemb = 1;
489 for ( i = 0; i < matvar->rank; i++ )
490 nmemb *= matvar->dims[i];
491
492 nfields = matvar->internal->num_fields;
493
494 if ( index < nmemb && field_index < nfields ) {
495 matvar_t **fields = matvar->data;
496 old_field = fields[index*nfields+field_index];
497 fields[index*nfields+field_index] = field;
498 if ( NULL != field->name ) {
499 free(field->name);
500 }
501 field->name = strdup(matvar->internal->fieldnames[field_index]);
502 }
503
504 return old_field;
505 }
506
507 /** @brief Sets the structure field to the given variable
508 *
509 * Sets the specified structure fieldname at the given 0-relative @c index to
510 * @c field.
511 * @ingroup MAT
512 * @param matvar Pointer to the Structure MAT variable
513 * @param field_name Name of the structure field
514 * @param index linear index of the structure array
515 * @param field New field variable
516 * @return Pointer to the previous field (NULL if no previous field)
517 */
518 matvar_t *
519 Mat_VarSetStructFieldByName(matvar_t *matvar,const char *field_name,
520 size_t index,matvar_t *field)
521 {
522 int i, nfields, field_index;
523 matvar_t *old_field = NULL;
524 size_t nmemb;
525
526 if ( matvar == NULL || matvar->class_type != MAT_C_STRUCT ||
527 matvar->data == NULL )
528 return old_field;
529
530 nmemb = 1;
531 for ( i = 0; i < matvar->rank; i++ )
532 nmemb *= matvar->dims[i];
533
534 nfields = matvar->internal->num_fields;
535 field_index = -1;
536 for ( i = 0; i < nfields; i++ ) {
537 if ( !strcmp(matvar->internal->fieldnames[i],field_name) ) {
538 field_index = i;
539 break;
540 }
541 }
542
543 if ( index < nmemb && field_index >= 0 ) {
544 matvar_t **fields = matvar->data;
545 old_field = fields[index*nfields+field_index];
546 fields[index*nfields+field_index] = field;
547 if ( NULL != field->name ) {
548 free(field->name);
549 }
550 field->name = strdup(matvar->internal->fieldnames[field_index]);
551 }
552
553 return old_field;
554 }