Debian Codegraph @

matio-1.5.30/src/matvar_struct.c

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