Debian Codegraph @

matio-1.5.30/tools/matdump.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2015-2026, The matio contributors
3 * Copyright (c) 2005-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 "matioConfig.h"
29 #include "matio.h"
30 #include <getopt.h>
31 #include <stdlib.h>
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <math.h>
36 #if defined(_MSC_VER) || defined(__MINGW32__)
37 #define SIZE_T_FMTSTR "Iu"
38 #else
39 #define SIZE_T_FMTSTR "zu"
40 #endif
41
42 /* snprintf.c */
43 #if !HAVE_SNPRINTF
44 int rpl_snprintf(char *, size_t, const char *, ...);
45 #define mat_snprintf rpl_snprintf
46 #else
47 #define mat_snprintf snprintf
48 #endif /* !HAVE_SNPRINTF */
49
50 static const char *optstring = "df:hvo:HV";
51 static struct option options[] = {
52 {"data", no_argument, NULL, 'd'}, {"format", required_argument, NULL, 'f'},
53 {"human", no_argument, NULL, 'h'}, {"verbose", optional_argument, NULL, 'v'},
54 {"help", no_argument, NULL, 'H'}, {"version", no_argument, NULL, 'V'},
55 {"output", required_argument, NULL, 'o'}, {NULL, 0, NULL, 0}};
56
57 static const char *helpstr[] = {"",
58 "Usage: matdump [OPTIONS] mat_file [var1 var2 ...]",
59 "",
60 "Dumps the variables of the MAT file using libmatio",
61 "",
62 "OPTIONS",
63 "-d,--data Print data with header information",
64 "-f,--format whos Turn on 'whos' display mode",
65 "-h,--human Human readable sizes in 'whos' display mode",
66 "-v,--verbose Turn on verbose messages",
67 "-H,--help This output",
68 "-V,--version version information",
69 "",
70 "mat_file name of the MAT file to dump",
71 "var1 var2 ... If specified, dumps only listed variables",
72 "",
73 "Report bugs to <[email protected]>.",
74 NULL};
75
76 static const char *mxclass[18] = {
77 "mxUNKNOWN_CLASS", "mxCELL_CLASS", "mxSTRUCT_CLASS", "mxOBJECT_CLASS", "mxCHAR_CLASS",
78 "mxSPARSE_CLASS", "mxDOUBLE_CLASS", "mxSINGLE_CLASS", "mxINT8_CLASS", "mxUINT8_CLASS",
79 "mxINT16_CLASS", "mxUINT16_CLASS", "mxINT32_CLASS", "mxUINT32_CLASS", "mxINT64_CLASS",
80 "mxUINT64_CLASS", "mxFUNCTION_CLASS", "mxOPAQUE_CLASS"};
81 static int printdata = 0;
82 static int human_readable = 0;
83 static int print_whos_first = 1;
84
85 /* Print Functions */
86 static void print_whos(const matvar_t *matvar);
87 static void print_default(const matvar_t *matvar);
88
89 static void (*printfunc)(const matvar_t *matvar) = NULL;
90
91 static char *
92 get_next_token(char *str)
93 {
94 const char *tokens = "(){}.";
95 char *next_tok, *tok;
96
97 next_tok = NULL;
98 while ( *tokens != '\0' ) {
99 tok = strchr(str, *tokens);
100 if ( tok != NULL ) {
101 if ( NULL == next_tok )
102 next_tok = tok;
103 else if ( tok < next_tok )
104 next_tok = tok;
105 }
106 tokens++;
107 }
108 if ( NULL == next_tok )
109 next_tok = str;
110 return next_tok;
111 }
112
113 static int
114 slab_get_rank(char *open, const char *close)
115 {
116 int rank = 0;
117 char *ptr = open + 1;
118 rank = 1;
119 while ( ptr != close ) {
120 if ( *ptr++ == ',' )
121 rank++;
122 }
123 return rank;
124 }
125
126 static void
127 slab_get_select(char *open, const char *close, int rank, int *start, int *stride, int *edge)
128 {
129 char *ptr = open;
130 const char *valptr = open + 1;
131 int dim = 0;
132 int nvals = 0;
133 do {
134 ptr++;
135 if ( *ptr == ',' ) {
136 if ( nvals == 2 ) {
137 *ptr = '\0';
138 if ( !strcmp(valptr, "end") ) {
139 edge[dim] = -1;
140 } else {
141 edge[dim] = (int)strtol(valptr, NULL, 10);
142 }
143 } else if ( nvals == 1 ) {
144 *ptr = '\0';
145 if ( !strcmp(valptr, "end") ) {
146 edge[dim] = -1;
147 } else {
148 edge[dim] = (int)strtol(valptr, NULL, 10);
149 }
150 } else if ( nvals == 0 ) {
151 *ptr = '\0';
152 if ( !strcmp(valptr, "end") ) {
153 start[dim] = -1;
154 edge[dim] = -1;
155 } else {
156 int i = (int)strtol(valptr, NULL, 10);
157 start[dim] = i - 1;
158 edge[dim] = i;
159 }
160 }
161 dim++;
162 valptr = ptr + 1;
163 nvals = 0;
164 } else if ( *ptr == ':' ) {
165 *ptr = '\0';
166 if ( !strcmp(valptr, "end") ) {
167 if ( nvals == 0 )
168 start[dim] = -1;
169 else if ( nvals == 1 )
170 edge[dim] = -1;
171 else if ( nvals == 2 )
172 edge[dim] = -1;
173 else
174 fprintf(stderr, "Too many inputs to dim %d", dim + 1);
175 } else {
176 int i = (int)strtol(valptr, NULL, 10);
177 if ( nvals == 0 )
178 start[dim] = i - 1;
179 else if ( nvals == 1 )
180 stride[dim] = i;
181 else if ( nvals == 2 )
182 edge[dim] = i;
183 else
184 fprintf(stderr, "Too many inputs to dim %d", dim + 1);
185 }
186 nvals++;
187 valptr = ptr + 1;
188 } else if ( *ptr == ')' || *ptr == '}' ) {
189 *ptr = '\0';
190 if ( !strcmp(valptr, "end") ) {
191 if ( nvals == 0 ) {
192 start[dim] = -1;
193 edge[dim] = -1;
194 } else if ( nvals == 1 )
195 edge[dim] = -1;
196 else if ( nvals == 2 )
197 edge[dim] = -1;
198 else
199 fprintf(stderr, "Too many inputs to dim %d", dim + 1);
200 } else {
201 int i = (int)strtol(valptr, NULL, 10);
202 if ( nvals == 0 ) {
203 start[dim] = i - 1;
204 edge[dim] = i;
205 } else if ( nvals == 1 )
206 edge[dim] = i;
207 else if ( nvals == 2 )
208 edge[dim] = i;
209 else
210 fprintf(stderr, "Too many inputs to dim %d", dim + 1);
211 }
212 nvals++;
213 valptr = ptr + 1;
214 }
215 } while ( ptr != close );
216 }
217
218 static int
219 slab_select_valid(int rank, int *start, const int *stride, int *edge, matvar_t *matvar)
220 {
221 int valid = 1, i, nmemb = 1;
222
223 if ( (matvar->rank != rank) && (rank != 1) ) {
224 valid = 0;
225 } else if ( rank == 1 ) {
226 for ( i = 0; i < matvar->rank; i++ )
227 nmemb *= matvar->dims[i];
228 if ( *stride < 1 ) {
229 /* Check stride is at least 1 */
230 fprintf(stderr, "stride must be positive");
231 valid = 0;
232 } else if ( *edge > nmemb ) {
233 /* edge can't be bigger than the size of the dimension */
234 fprintf(stderr, "edge out of bound");
235 valid = 0;
236 } else if ( *start >= nmemb || (*start > *edge && *edge > 0) ) {
237 /* Start can't be bigger than the size of the dimension and
238 * can't be greater than the edge unless edge == -1 => end
239 */
240 fprintf(stderr, "start out of bound");
241 valid = 0;
242 } else if ( *edge == -1 && *start == -1 ) {
243 /* If edge == start == -1, then a single end was used */
244 *edge = 1;
245 *start = nmemb - 1;
246 } else if ( *edge == -1 && *stride == 1 ) {
247 /* index of the form 1:end, 1:1:end, or : */
248 *edge = nmemb;
249 /* If ':' was specified, start[i] will be -1 */
250 if ( *start < 0 )
251 *start = 0;
252 } else if ( *edge == -1 ) {
253 /* index of the form 1:stride:end */
254 *edge = nmemb;
255 *edge = (int)floor((double)(*edge - *start - 1) / (double)*stride) + 1;
256 } else if ( *edge > 0 ) {
257 *edge = (int)floor((double)(*edge - *start - 1) / (double)*stride) + 1;
258 }
259 nmemb = *edge;
260 } else {
261 for ( i = 0; i < rank && valid; i++ ) {
262 if ( stride[i] < 1 ) {
263 /* Check stride is at least 1 */
264 fprintf(stderr, "stride must be positive");
265 valid = 0;
266 break;
267 } else if ( edge[i] == -1 && start[i] == -1 ) {
268 /* If edge == start == -1, then a single end was used */
269 edge[i] = 1;
270 start[i] = matvar->dims[i] - 1;
271 } else if ( edge[i] < 0 && stride[i] == 1 ) {
272 /* index of the form 1:end, 1:1:end, or : */
273 edge[i] = matvar->dims[i];
274 /* If ':' was specified, start[i] will be -1 */
275 if ( start[i] < 0 )
276 start[i] = 0;
277 } else if ( edge[i] < 0 ) {
278 /* index of the form 1:stride:end */
279 edge[i] =
280 (int)floor((double)(matvar->dims[i] - start[i] - 1) / (double)stride[i]) + 1;
281 } else if ( (size_t)edge[i] > matvar->dims[i] ) {
282 /* edge can't be bigger than the size of the dimension */
283 fprintf(stderr, "edge out of bound on dimension %d", i + 1);
284 valid = 0;
285 break;
286 } else if ( (size_t)start[i] >= matvar->dims[i] ||
287 (start[i] > edge[i] && edge[i] > 0) ) {
288 /* Start can't be bigger than the size of the dimension and
289 * can't be greater than the edge unless edge == -1 => end
290 */
291 fprintf(stderr, "start out of bound on dimension %d", i + 1);
292 valid = 0;
293 break;
294 } else if ( edge[i] == (start[i] + 1) ) {
295 /* index of the form 3:3 */
296 edge[i] = 1;
297 } else if ( edge[i] > 0 ) {
298 edge[i] = (int)floor((double)(edge[i] - start[i] - 1) / (double)stride[i]) + 1;
299 }
300 nmemb *= edge[i];
301 }
302 }
303 if ( !valid )
304 nmemb = 0;
305 return nmemb;
306 }
307
308 static void
309 read_selected_data(mat_t *mat, matvar_t **_matvar, char *index_str)
310 {
311 char *next_tok_pos, next_tok = 0;
312 char *open = NULL, *close = NULL;
313 int err = 1, j, done = 0;
314 matvar_t *matvar = *_matvar;
315
316 next_tok_pos = get_next_token(index_str);
317 next_tok = *next_tok_pos;
318
319 while ( !done ) {
320 /* Check If the user is selecting a subset of the dataset */
321 if ( next_tok == '(' ) {
322 int rank, *start, *stride, *edge, nmemb;
323
324 open = next_tok_pos;
325 close = strchr(open + 1, ')');
326
327 /* Get the next token after this selection */
328 next_tok_pos = get_next_token(close + 1);
329 if ( next_tok_pos != (close + 1) ) {
330 *next_tok_pos = '\0';
331 next_tok = *next_tok_pos;
332 } else {
333 done = 1;
334 }
335 /* Make sure that the partial I/O is the last token */
336 if ( !done ) {
337 fprintf(stderr, "Partial I/O must be the last operation in the expression");
338 break;
339 }
340 /* Get the rank of the dataset */
341 rank = slab_get_rank(open, close);
342 start = (int *)malloc(rank * sizeof(int));
343 stride = (int *)malloc(rank * sizeof(int));
344 edge = (int *)malloc(rank * sizeof(int));
345 for ( j = 0; j < rank; j++ ) {
346 start[j] = 0;
347 stride[j] = 1;
348 edge[j] = 1;
349 }
350 /* Get the start,stride,edge using matlab syntax */
351 slab_get_select(open, close, rank, start, stride, edge);
352
353 /* Check if the users selection is valid and if so read the data */
354 if ( (nmemb = slab_select_valid(rank, start, stride, edge, matvar)) ) {
355 matvar->data_size = Mat_SizeOfClass(matvar->class_type);
356 matvar->nbytes = (size_t)nmemb * matvar->data_size;
357 if ( matvar->isComplex ) {
358 mat_complex_split_t *z;
359 matvar->data = malloc(sizeof(*z));
360 z = (mat_complex_split_t *)matvar->data;
361 z->Re = malloc(matvar->nbytes);
362 z->Im = malloc(matvar->nbytes);
363 } else {
364 matvar->data = malloc(matvar->nbytes);
365 }
366 if ( matvar->data == NULL ) {
367 fprintf(stderr, "Couldn't allocate memory for the data");
368 err = 1;
369 } else if ( rank == 1 ) {
370 Mat_VarReadDataLinear(mat, matvar, matvar->data, *start, *stride, *edge);
371 if ( matvar->rank == 2 && matvar->dims[0] == 1 ) {
372 matvar->dims[1] = *edge;
373 } else if ( matvar->rank == 2 && matvar->dims[1] == 1 ) {
374 matvar->dims[0] = *edge;
375 } else {
376 matvar->rank = 2;
377 matvar->dims[0] = *edge;
378 matvar->dims[1] = 1;
379 }
380 } else {
381 int i;
382 err = Mat_VarReadData(mat, matvar, matvar->data, start, stride, edge);
383 for ( i = 0; i < rank; i++ )
384 matvar->dims[i] = (size_t)edge[i];
385 }
386 }
387 free(start);
388 free(stride);
389 free(edge);
390 } else if ( next_tok == '.' ) {
391 matvar_t *field;
392 const char *varname = next_tok_pos + 1;
393 if ( matvar->class_type == MAT_C_STRUCT ) {
394 next_tok_pos = get_next_token(next_tok_pos + 1);
395 if ( next_tok_pos != varname ) {
396 next_tok = *next_tok_pos;
397 *next_tok_pos = '\0';
398 } else {
399 done = 1;
400 }
401 /* FIXME: Handle structures > 1x1 */
402 field = Mat_VarGetStructFieldByName(matvar, varname, 0);
403 if ( field == NULL ) {
404 fprintf(stderr, "field %s was not found in structure %s", varname,
405 matvar->name);
406 break;
407 }
408 field = Mat_VarDuplicate(field, 1);
409 Mat_VarFree(matvar);
410 matvar = field;
411 if ( done == 1 ) {
412 err = Mat_VarReadDataAll(mat, matvar);
413 }
414 } else if ( matvar->class_type == MAT_C_CELL ) {
415 int ncells;
416 matvar_t **cells;
417
418 ncells = matvar->nbytes / matvar->data_size;
419 cells = (matvar_t **)matvar->data;
420 next_tok_pos = get_next_token(next_tok_pos + 1);
421 if ( next_tok_pos != varname ) {
422 next_tok = *next_tok_pos;
423 *next_tok_pos = '\0';
424 } else {
425 done = 1;
426 }
427 for ( j = 0; j < ncells; j++ ) {
428 matvar_t *cell = Mat_VarGetCell(matvar, j);
429 if ( cell == NULL || cell->class_type != MAT_C_STRUCT ) {
430 fprintf(stderr, "cell index %d is not a structure", j);
431 break;
432 } else {
433 /* FIXME: Handle structures > 1x1 */
434 field = Mat_VarGetStructFieldByName(cell, varname, 0);
435 if ( field == NULL ) {
436 fprintf(stderr, "field %s was not found in structure %s", varname,
437 matvar->name);
438 break;
439 }
440 field = Mat_VarDuplicate(field, 1);
441 Mat_VarFree(cell);
442 cells[j] = field;
443 }
444 }
445 if ( j != ncells )
446 break;
447 } else {
448 fprintf(stderr, "%s is not a structure", varname);
449 break;
450 }
451 } else if ( next_tok == '{' ) {
452 int rank, *start, *stride, *edge, nmemb;
453
454 if ( matvar->class_type != MAT_C_CELL ) {
455 fprintf(stderr, "Only Cell Arrays can index with {}");
456 break;
457 }
458 open = next_tok_pos;
459 close = strchr(open + 1, '}');
460
461 /* Get the next token after this selection */
462 next_tok_pos = get_next_token(close + 1);
463 if ( *next_tok_pos != '\0' ) {
464 next_tok = *next_tok_pos;
465 *next_tok_pos = '\0';
466 } else {
467 done = 1;
468 }
469 /* Get the rank of the dataset */
470 rank = slab_get_rank(open, close);
471 start = (int *)malloc(rank * sizeof(int));
472 stride = (int *)malloc(rank * sizeof(int));
473 edge = (int *)malloc(rank * sizeof(int));
474 for ( j = 0; j < rank; j++ ) {
475 start[j] = 0;
476 stride[j] = 1;
477 edge[j] = 1;
478 }
479 /* Get the start,stride,edge using matlab syntax */
480 slab_get_select(open, close, rank, start, stride, edge);
481 /* Check if the users selection is valid and if so read the data */
482 if ( (nmemb = slab_select_valid(rank, start, stride, edge, matvar)) ) {
483 matvar_t **cells, *tmp;
484 if ( rank == 1 ) {
485 cells = Mat_VarGetCellsLinear(matvar, *start, *stride, *edge);
486 if ( matvar->rank == 2 && matvar->dims[0] == 1 ) {
487 matvar->dims[1] = *edge;
488 } else if ( matvar->rank == 2 && matvar->dims[1] == 1 ) {
489 matvar->dims[0] = *edge;
490 } else {
491 matvar->rank = 1;
492 matvar->dims[0] = *edge;
493 }
494 } else {
495 int i;
496 cells = Mat_VarGetCells(matvar, start, stride, edge);
497 for ( i = 0; i < rank; i++ )
498 matvar->dims[i] = (size_t)edge[i];
499 }
500 if ( cells == NULL ) {
501 fprintf(stderr, "Error getting the indexed cells");
502 err = 1;
503 } else {
504 for ( j = 0; j < nmemb; j++ )
505 cells[j] = Mat_VarDuplicate(cells[j], 1);
506 tmp = Mat_VarCreate(matvar->name, MAT_C_CELL, MAT_T_CELL, matvar->rank,
507 matvar->dims, cells, MAT_F_DONT_COPY_DATA);
508 Mat_VarFree(matvar);
509 matvar = tmp;
510 }
511 } else {
512 fprintf(stderr, "Cell selection not valid");
513 err = 1;
514 }
515 free(start);
516 free(stride);
517 free(edge);
518 if ( err )
519 break;
520 }
521 }
522 *_matvar = matvar;
523 }
524
525 static void
526 print_whos(const matvar_t *matvar)
527 {
528 size_t nbytes;
529
530 if ( print_whos_first ) {
531 printf("%-20s %-10s %-10s %-18s\n\n", "Name", "Size", "Bytes", "Class");
532 print_whos_first = 0;
533 }
534 printf("%-20s", matvar->name);
535 if ( matvar->rank > 0 ) {
536 int cnt = 0, i;
537 char size[32] = {
538 '\0',
539 };
540 printf("%8" SIZE_T_FMTSTR, matvar->dims[0]);
541 for ( i = 1; i < matvar->rank; i++ ) {
542 if ( ceil(log10((double)matvar->dims[i])) + 1 < 32 ) {
543 cnt += mat_snprintf(size + cnt, sizeof(size) - cnt, "x%" SIZE_T_FMTSTR,
544 matvar->dims[i]);
545 if ( cnt >= sizeof(size) ) {
546 break;
547 }
548 }
549 }
550 printf("%-10s", size);
551 } else {
552 printf(" ");
553 }
554 nbytes = Mat_VarGetSize(matvar);
555 if ( human_readable ) {
556 if ( nbytes > 1073741824L )
557 printf(" %10.1fG", (double)nbytes / 1073741824.0);
558 else if ( nbytes > 1048576 )
559 printf(" %10.1fM", (double)nbytes / 1048576.0);
560 else if ( nbytes > 1024 )
561 printf(" %10.1fK", (double)nbytes / 1024.0);
562 else
563 printf(" %10" SIZE_T_FMTSTR "B", nbytes);
564 } else {
565 printf(" %10" SIZE_T_FMTSTR, nbytes);
566 }
567 printf(" %-18s\n", mxclass[matvar->class_type]);
568 }
569
570 static size_t indent = 0;
571
572 static void
573 default_printf_func(int log_level, const char *message)
574 {
575 size_t i;
576
577 for ( i = 0; i < indent; i++ )
578 printf(" ");
579 printf("%s\n", message);
580 }
581
582 static void
583 print_default_number(enum matio_types type, const void *data)
584 {
585 switch ( type ) {
586 case MAT_T_DOUBLE:
587 printf("%g", *(double *)data);
588 break;
589 case MAT_T_SINGLE:
590 printf("%g", *(float *)data);
591 break;
592 #ifdef _mat_int64_t
593 case MAT_T_INT64:
594 #if MATIO_HAVE_INTTYPES_H
595 printf("%" PRIi64, *(mat_int64_t *)data);
596 #elif defined(_MSC_VER) && _MSC_VER >= 1200
597 printf("%I64i", *(mat_int64_t *)data);
598 #else
599 printf("%lld", (long long)(*(mat_int64_t *)data));
600 #endif
601 break;
602 #endif
603 #ifdef _mat_uint64_t
604 case MAT_T_UINT64:
605 #if MATIO_HAVE_INTTYPES_H
606 printf("%" PRIu64, *(mat_uint64_t *)data);
607 #elif defined(_MSC_VER) && _MSC_VER >= 1200
608 printf("%I64u", *(mat_uint64_t *)data);
609 #else
610 printf("%llu", (unsigned long long)(*(mat_uint64_t *)data));
611 #endif
612 break;
613 #endif
614 case MAT_T_INT32:
615 printf("%d", *(mat_int32_t *)data);
616 break;
617 case MAT_T_UINT32:
618 printf("%u", *(mat_uint32_t *)data);
619 break;
620 case MAT_T_INT16:
621 printf("%hd", *(mat_int16_t *)data);
622 break;
623 case MAT_T_UINT16:
624 printf("%hu", *(mat_uint16_t *)data);
625 break;
626 case MAT_T_INT8:
627 printf("%hhd", *(mat_int8_t *)data);
628 break;
629 case MAT_T_UINT8:
630 printf("%hhu", *(mat_uint8_t *)data);
631 break;
632 default:
633 break;
634 }
635 }
636
637 static void
638 print_default_numeric_2d(const matvar_t *matvar)
639 {
640 size_t i, j, stride;
641
642 if ( NULL == matvar->data )
643 return;
644
645 stride = Mat_SizeOf(matvar->data_type);
646 if ( matvar->isComplex ) {
647 const mat_complex_split_t *complex_data = (mat_complex_split_t *)matvar->data;
648 const char *rp = (char *)complex_data->Re;
649 const char *ip = (char *)complex_data->Im;
650 for ( i = 0; i < matvar->dims[0]; i++ ) {
651 for ( j = 0; j < matvar->dims[1]; j++ ) {
652 size_t idx = matvar->dims[0] * j + i;
653 print_default_number(matvar->data_type, rp + idx * stride);
654 printf(" + ");
655 print_default_number(matvar->data_type, ip + idx * stride);
656 printf("i ");
657 }
658 printf("\n");
659 }
660 } else {
661 const char *data = (char *)matvar->data;
662 for ( i = 0; i < matvar->dims[0]; i++ ) {
663 for ( j = 0; j < matvar->dims[1]; j++ ) {
664 size_t idx = matvar->dims[0] * j + i;
665 print_default_number(matvar->data_type, data + idx * stride);
666 printf(" ");
667 }
668 printf("\n");
669 }
670 }
671 }
672
673 static void
674 print_default_numeric_3d(const matvar_t *matvar)
675 {
676 size_t i, j, k, l, stride;
677
678 if ( NULL == matvar->data )
679 return;
680
681 stride = Mat_SizeOf(matvar->data_type);
682 if ( matvar->isComplex ) {
683 const mat_complex_split_t *complex_data = (mat_complex_split_t *)matvar->data;
684 const char *rp = (char *)complex_data->Re;
685 const char *ip = (char *)complex_data->Im;
686 for ( k = 0; k < matvar->dims[2]; k++ ) {
687 Mat_Message("%s(:,:,%zu) = ", matvar->name, k);
688 indent++;
689 for ( i = 0; i < matvar->dims[0]; i++ ) {
690 for ( l = 0; l < indent; l++ )
691 printf(" ");
692 for ( j = 0; j < matvar->dims[1]; j++ ) {
693 size_t idx = matvar->dims[0] * matvar->dims[1] * k + matvar->dims[0] * j + i;
694 print_default_number(matvar->data_type, rp + idx * stride);
695 printf(" + ");
696 print_default_number(matvar->data_type, ip + idx * stride);
697 printf("i ");
698 }
699 printf("\n");
700 }
701 indent--;
702 printf("\n");
703 }
704 } else {
705 const char *data = (char *)matvar->data;
706 for ( k = 0; k < matvar->dims[2]; k++ ) {
707 Mat_Message("%s(:,:,%zu) = ", matvar->name, k);
708 indent++;
709 for ( i = 0; i < matvar->dims[0]; i++ ) {
710 for ( l = 0; l < indent; l++ )
711 printf(" ");
712 for ( j = 0; j < matvar->dims[1]; j++ ) {
713 size_t idx = matvar->dims[0] * matvar->dims[1] * k + matvar->dims[0] * j + i;
714 print_default_number(matvar->data_type, data + idx * stride);
715 printf(" ");
716 }
717 printf("\n");
718 }
719 indent--;
720 printf("\n");
721 }
722 }
723 }
724
725 static void
726 print_default(const matvar_t *matvar)
727 {
728 if ( NULL == matvar )
729 return;
730
731 switch ( matvar->class_type ) {
732 case MAT_C_DOUBLE:
733 case MAT_C_SINGLE:
734 case MAT_C_INT64:
735 case MAT_C_UINT64:
736 case MAT_C_INT32:
737 case MAT_C_UINT32:
738 case MAT_C_INT16:
739 case MAT_C_UINT16:
740 case MAT_C_INT8:
741 case MAT_C_UINT8: {
742 if ( matvar->rank == 2 )
743 print_default_numeric_2d(matvar);
744 else if ( matvar->rank == 3 )
745 print_default_numeric_3d(matvar);
746 break;
747 }
748 case MAT_C_CHAR:
749 case MAT_C_SPARSE:
750 Mat_VarPrint(matvar, printdata);
751 break;
752 case MAT_C_STRUCT: {
753 int nfields;
754 int i;
755 size_t nmemb;
756
757 if ( matvar->name )
758 Mat_Message(" Name: %s", matvar->name);
759 Mat_Message(" Rank: %d", matvar->rank);
760 if ( matvar->rank == 0 )
761 return;
762 Mat_Message("Class Type: Structure");
763 nfields = Mat_VarGetNumberOfFields(matvar);
764 nmemb = matvar->dims[0];
765 for ( i = 1; i < matvar->rank; i++ )
766 nmemb *= matvar->dims[i];
767 if ( nfields > 0 && nmemb < 1 ) {
768 char *const *fieldnames = Mat_VarGetStructFieldnames(matvar);
769 Mat_Message("Fields[%d] {", nfields);
770 indent++;
771 if ( NULL != fieldnames ) {
772 for ( i = 0; i < nfields; i++ )
773 Mat_Message(" Name: %s", fieldnames[i]);
774 }
775 indent--;
776 Mat_Message("}");
777 } else if ( nfields > 0 && nmemb > 0 ) {
778 Mat_Message("Fields[%d] {", nfields);
779 indent++;
780 {
781 matvar_t **fields = (matvar_t **)matvar->data;
782 if ( NULL != fields ) {
783 size_t j;
784 for ( j = 0; j < nfields * nmemb; j++ )
785 print_default(fields[j]);
786 }
787 }
788 indent--;
789 Mat_Message("}");
790 }
791 break;
792 }
793 case MAT_C_CELL: {
794 size_t ncells;
795 int i;
796
797 if ( matvar->name )
798 Mat_Message(" Name: %s", matvar->name);
799 Mat_Message(" Rank: %d", matvar->rank);
800 if ( matvar->rank == 0 )
801 return;
802 ncells = matvar->dims[0];
803 for ( i = 1; i < matvar->rank; i++ )
804 ncells *= matvar->dims[i];
805 Mat_Message("Class Type: Cell Array");
806 Mat_Message("{");
807 indent++;
808 {
809 matvar_t **cells = (matvar_t **)matvar->data;
810 if ( NULL != cells ) {
811 size_t j;
812 for ( j = 0; j < ncells; j++ )
813 print_default(cells[j]);
814 }
815 }
816 indent--;
817 Mat_Message("}");
818 break;
819 }
820 default:
821 Mat_Message("Empty");
822 }
823 }
824
825 static void
826 redirect_output(const char *output)
827 {
828 if ( output != NULL )
829 if ( freopen(output, "w", stdout) == NULL )
830 fprintf(stderr, "Unable to open %s for writing. Using stdout instead.", output);
831 }
832
833 int
834 main(int argc, char *argv[])
835 {
836 const char *prog_name = "matdump";
837 int c, err = EXIT_SUCCESS;
838 mat_t *mat;
839 matvar_t *matvar;
840 int version[3];
841
842 Mat_GetLibraryVersion(version, version + 1, version + 2);
843 if ( MATIO_MAJOR_VERSION != version[0] || MATIO_MINOR_VERSION != version[1] ||
844 MATIO_RELEASE_LEVEL != version[2] ) {
845 fprintf(stderr, "matio version in header does not match runtime version\n");
846 return EXIT_FAILURE;
847 }
848
849 Mat_LogInitFunc(prog_name, default_printf_func);
850
851 printfunc = print_default;
852
853 while ( (c = getopt_long(argc, argv, optstring, options, NULL)) != EOF ) {
854 switch ( c ) {
855 case 'd':
856 printdata = 1;
857 Mat_VerbMessage(1, "Printing data\n");
858 break;
859 case 'f':
860 if ( NULL != optarg ) {
861 if ( !strcmp(optarg, "whos") ) {
862 printfunc = print_whos;
863 } else {
864 Mat_Warning("%s is not a recognized output format. Using default\n",
865 optarg);
866 }
867 } else {
868 Mat_Warning("Missing output format. Using default\n");
869 }
870 break;
871 case 'h':
872 human_readable = 1;
873 break;
874 case 'v':
875 Mat_SetVerbose(1, 0);
876 break;
877 case 'o':
878 redirect_output(optarg);
879 break;
880 case 'H':
881 Mat_Help(helpstr);
882 /* Note: Mat_Help() calls exit() */
883 case 'V':
884 printf(
885 "%s %s\nWritten by Christopher Hulbert\n\n"
886 "Copyright(C) 2015-2026, The matio contributors\n"
887 "Copyright(C) 2006-2014, Christopher C. Hulbert\n",
888 prog_name, MATIO_VERSION_STR);
889 exit(EXIT_SUCCESS);
890 default:
891 printf("%c not a valid option\n", c);
892 break;
893 }
894 }
895
896 if ( (argc - optind) < 1 ) {
897 Mat_Critical("Must specify at least one argument");
898 return EXIT_FAILURE;
899 }
900
901 mat = Mat_Open(argv[optind], MAT_ACC_RDONLY);
902 if ( NULL == mat ) {
903 Mat_Critical("Error opening %s\n", argv[optind]);
904 return EXIT_FAILURE;
905 }
906
907 optind++;
908
909 if ( optind < argc ) {
910 int i;
911 /* variables specified on the command line */
912 for ( i = optind; i < argc; i++ ) {
913 char *next_tok_pos, next_tok = 0;
914
915 next_tok_pos = get_next_token(argv[i]);
916 if ( next_tok_pos != argv[i] ) {
917 next_tok = *next_tok_pos;
918 *next_tok_pos = '\0';
919 }
920
921 matvar = Mat_VarReadInfo(mat, argv[i]);
922 if ( matvar ) {
923 if ( printdata ) {
924 if ( next_tok == '\0' ) {
925 /* No indexing tokens found, so read all of the data */
926 err = Mat_VarReadDataAll(mat, matvar);
927 } else {
928 *next_tok_pos = next_tok;
929 read_selected_data(mat, &matvar, next_tok_pos);
930 }
931 }
932 (*printfunc)(matvar);
933 Mat_VarFree(matvar);
934 } else {
935 Mat_Warning("Couldn't find variable %s in the MAT file", argv[i]);
936 }
937 } /* for ( i = optind; i < argc; i++ ) */
938 } else {
939 /* print all variables */
940 if ( printdata ) {
941 while ( (matvar = Mat_VarReadNext(mat)) != NULL ) {
942 (*printfunc)(matvar);
943 Mat_VarFree(matvar);
944 }
945 } else {
946 while ( (matvar = Mat_VarReadNextInfo(mat)) != NULL ) {
947 (*printfunc)(matvar);
948 Mat_VarFree(matvar);
949 }
950 }
951 }
952
953 Mat_Close(mat);
954
955 Mat_LogClose();
956
957 return err;
958 }