Debian Codegraph @

matio-1.5.0/src/mat.c

raw
1 /** @file mat.c
2 * Matlab MAT version 5 file functions
3 * @ingroup MAT
4 */
5 /*
6 * Copyright (C) 2005-2011 Christopher C. Hulbert
7 *
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY CHRISTOPHER C. HULBERT ``AS IS'' AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL CHRISTOPHER C. HULBERT OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /* FIXME: Implement Unicode support */
34 #include <stdlib.h>
35 #include <string.h>
36 #include <stdio.h>
37 #include <math.h>
38 #include <time.h>
39 #include "matio_private.h"
40 #include "mat5.h"
41 #include "mat4.h"
42 #if defined(MAT73) && MAT73
43 # include "mat73.h"
44 #endif
45
46 static void
47 ReadData(mat_t *mat, matvar_t *matvar)
48 {
49 if ( mat == NULL || matvar == NULL || mat->fp == NULL )
50 return;
51 else if ( mat->version == MAT_FT_MAT5 )
52 Read5(mat,matvar);
53 #if defined(MAT73) && MAT73
54 else if ( mat->version == MAT_FT_MAT73 )
55 Mat_VarRead73(mat,matvar);
56 #endif
57 else if ( mat->version == MAT_FT_MAT4 )
58 Read4(mat,matvar);
59 return;
60 }
61
62 static void
63 Mat_PrintNumber(enum matio_types type, void *data)
64 {
65 switch ( type ) {
66 case MAT_T_DOUBLE:
67 printf("%g",*(double*)data);
68 break;
69 case MAT_T_SINGLE:
70 printf("%g",*(float*)data);
71 break;
72 #ifdef HAVE_MAT_INT64_T
73 case MAT_T_INT64:
74 printf("%lld",*(mat_int64_t*)data);
75 break;
76 #endif
77 #ifdef HAVE_MAT_UINT64_T
78 case MAT_T_UINT64:
79 printf("%llu",*(mat_uint64_t*)data);
80 break;
81 #endif
82 case MAT_T_INT32:
83 printf("%d",*(mat_int32_t*)data);
84 break;
85 case MAT_T_UINT32:
86 printf("%u",*(mat_uint32_t*)data);
87 break;
88 case MAT_T_INT16:
89 printf("%hd",*(mat_int16_t*)data);
90 break;
91 case MAT_T_UINT16:
92 printf("%hu",*(mat_uint16_t*)data);
93 break;
94 case MAT_T_INT8:
95 printf("%hhd",*(mat_int8_t*)data);
96 break;
97 case MAT_T_UINT8:
98 printf("%hhu",*(mat_uint8_t*)data);
99 break;
100 }
101 }
102
103 /*
104 *====================================================================
105 * Public Functions
106 *====================================================================
107 */
108
109 /** @brief Get the version of the library
110 *
111 * Gets the version number of the library
112 * @param major Pointer to store the library major version number
113 * @param major Pointer to store the library major version number
114 * @param major Pointer to store the library major version number
115 */
116 void
117 Mat_GetLibraryVersion(int *major,int *minor,int *release)
118 {
119 if ( NULL != major )
120 *major = MATIO_MAJOR_VERSION;
121 if ( NULL != minor )
122 *minor = MATIO_MINOR_VERSION;
123 if ( NULL != release )
124 *release = MATIO_RELEASE_LEVEL;
125 }
126
127 /** @brief Creates a new Matlab MAT file
128 *
129 * Tries to create a new Matlab MAT file with the given name and optional
130 * header string. If no header string is given, the default string
131 * is used containing the software, version, and date in it. If a header
132 * string is given, at most the first 116 characters is written to the file.
133 * The given header string need not be the full 116 characters, but MUST be
134 * NULL terminated.
135 * @ingroup MAT
136 * @param matname Name of MAT file to create
137 * @param hdr_str Optional header string, NULL to use default
138 * @param mat_file_ver MAT file version to create
139 * @return A pointer to the MAT file or NULL if it failed. This is not a
140 * simple FILE * and should not be used as one.
141 */
142 mat_t *
143 Mat_CreateVer(const char *matname,const char *hdr_str,enum mat_ft mat_file_ver)
144 {
145 mat_t *mat;
146
147 if ( MAT_FT_MAT5 == mat_file_ver )
148 mat = Mat_Create5(matname,hdr_str);
149 else if ( MAT_FT_MAT73 == mat_file_ver )
150 #if defined(MAT73) && MAT73
151 mat = Mat_Create73(matname,hdr_str);
152 #else
153 mat = NULL;
154 #endif
155 else
156 mat = Mat_Create5(matname,hdr_str);
157
158 return mat;
159 }
160
161 /** @brief Opens an existing Matlab MAT file
162 *
163 * Tries to open a Matlab MAT file with the given name
164 * @ingroup MAT
165 * @param matname Name of MAT file to open
166 * @param mode File access mode (MAT_ACC_RDONLY,MAT_ACC_RDWR,etc).
167 * @return A pointer to the MAT file or NULL if it failed. This is not a
168 * simple FILE * and should not be used as one.
169 */
170 mat_t *
171 Mat_Open(const char *matname,int mode)
172 {
173 FILE *fp = NULL;
174 mat_int16_t tmp, tmp2;
175 int err;
176 mat_t *mat = NULL;
177
178 if ( (mode & 0x00000001) == MAT_ACC_RDONLY ) {
179 fp = fopen( matname, "rb" );
180 if ( !fp )
181 return NULL;
182 } else if ( (mode & 0x00000001) == MAT_ACC_RDWR ) {
183 fp = fopen( matname, "r+b" );
184 if ( !fp ) {
185 mat = Mat_CreateVer(matname,NULL,mode&0xfffffffe);
186 return mat;
187 }
188 } else {
189 mat = Mat_CreateVer(matname,NULL,mode&0xfffffffe);
190 return mat;
191 }
192
193 mat = malloc(sizeof(*mat));
194 if ( NULL == mat ) {
195 Mat_Critical("Couldn't allocate memory for the MAT file");
196 fclose(fp);
197 return NULL;
198 }
199
200 mat->fp = fp;
201 mat->header = calloc(128,1);
202 mat->subsys_offset = calloc(8,1);
203 mat->filename = NULL;
204 mat->byteswap = 0;
205
206 err = fread(mat->header,1,116,fp);
207 mat->header[116] = '\0';
208 err = fread(mat->subsys_offset,1,8,fp);
209 err = fread(&tmp2,2,1,fp);
210 fread(&tmp,1,2,fp);
211
212 mat->byteswap = -1;
213 if (tmp == 0x4d49)
214 mat->byteswap = 0;
215 else if (tmp == 0x494d) {
216 mat->byteswap = 1;
217 Mat_int16Swap(&tmp2);
218 }
219
220 mat->version = (int)tmp2;
221 if ( (mat->version == 0x0100 || mat->version == 0x0200) &&
222 -1 != mat->byteswap ) {
223 mat->bof = ftell(mat->fp);
224 mat->next_index = 0;
225 } else {
226 /* Maybe a V4 MAT file */
227 matvar_t *var;
228 if ( NULL != mat->header )
229 free(mat->header);
230 if ( NULL != mat->subsys_offset )
231 free(mat->subsys_offset);
232
233 mat->header = NULL;
234 mat->subsys_offset = NULL;
235 mat->fp = fp;
236 mat->version = MAT_FT_MAT4;
237 mat->byteswap = 0;
238 mat->mode = mode;
239 mat->bof = 0;
240 mat->next_index = 0;
241
242 Mat_Rewind(mat);
243 var = Mat_VarReadNextInfo4(mat);
244 /* Check sanity of variable information */
245 if ( NULL == var || NULL == var->name ||
246 (var->class_type != MAT_C_DOUBLE &&
247 var->class_type != MAT_C_SPARSE &&
248 var->class_type != MAT_C_CHAR) ) {
249 /* Does not seem to be a valid V4 file */
250 Mat_Critical("%s does not seem to be a valid MAT file",matname);
251 if ( NULL != var )
252 Mat_VarFree(var);
253 Mat_Close(mat);
254 mat = NULL;
255 } else {
256 Mat_VarFree(var);
257 Mat_Rewind(mat);
258 }
259 }
260
261 if ( NULL == mat )
262 return mat;
263
264 mat->filename = strdup_printf("%s",matname);
265 mat->mode = mode;
266
267 if ( mat->version == 0x0200 ) {
268 fclose(mat->fp);
269 #if defined(MAT73) && MAT73
270
271 mat->fp = malloc(sizeof(hid_t));
272
273 if ( (mode & 0x00ff) == MAT_ACC_RDONLY )
274 *(hid_t*)mat->fp=H5Fopen(mat->filename,H5F_ACC_RDONLY,H5P_DEFAULT);
275 else if ( (mode & 0x00ff) == MAT_ACC_RDWR )
276 *(hid_t*)mat->fp=H5Fopen(mat->filename,H5F_ACC_RDWR,H5P_DEFAULT);
277
278 if ( -1 < *(hid_t*)mat->fp ) {
279 hsize_t num_objs;
280 H5Gget_num_objs(*(hid_t*)mat->fp,&num_objs);
281 mat->num_datasets = num_objs;
282 mat->refs_id = -1;
283 }
284 #else
285 mat->fp = NULL;
286 Mat_Close(mat);
287 mat = NULL;
288 #endif
289 }
290
291 return mat;
292 }
293
294 /** @brief Closes an open Matlab MAT file
295 *
296 * Closes the given Matlab MAT file and frees any memory with it.
297 * @ingroup MAT
298 * @param mat Pointer to the MAT file
299 * @retval 0
300 */
301 int
302 Mat_Close( mat_t *mat )
303 {
304 if ( NULL != mat ) {
305 #if defined(MAT73) && MAT73
306 if ( mat->version == 0x0200 ) {
307 if ( mat->refs_id > -1 )
308 H5Gclose(mat->refs_id);
309 H5Fclose(*(hid_t*)mat->fp);
310 free(mat->fp);
311 mat->fp = NULL;
312 }
313 #endif
314 if ( mat->fp )
315 fclose(mat->fp);
316 if ( mat->header )
317 free(mat->header);
318 if ( mat->subsys_offset )
319 free(mat->subsys_offset);
320 if ( mat->filename )
321 free(mat->filename);
322 free(mat);
323 }
324 return 0;
325 }
326
327 /** @brief Gets the filename for the given MAT file
328 *
329 * Gets the filename for the given MAT file
330 * @ingroup MAT
331 * @param mat Pointer to the MAT file
332 * @return MAT filename
333 */
334 const char *
335 Mat_GetFilename(mat_t *matfp)
336 {
337 const char *filename = NULL;
338 if ( NULL != matfp )
339 filename = matfp->filename;
340 return filename;
341 }
342
343 /* gjl March 2013 Mat_GetHeader */
344 /** @brief Gets the header for the given MAT file
345 *
346 * Gets the header for the given MAT file
347 * @ingroup MAT
348 * @param mat Pointer to the MAT file
349 * @return MAT header
350 */
351 const char *
352 Mat_GetHeader(mat_t *matfp)
353 {
354 const char *header = NULL;
355 if ( NULL != matfp )
356 header = matfp->header;
357 return header;
358 }
359
360 /** @brief Gets the version of the given MAT file
361 *
362 * Gets the version of the given MAT file
363 * @ingroup MAT
364 * @param mat Pointer to the MAT file
365 * @return MAT file version
366 */
367 enum mat_ft
368 Mat_GetVersion(mat_t *matfp)
369 {
370 enum mat_ft file_type = 0;
371 if ( NULL != matfp )
372 file_type = matfp->version;
373 return file_type;
374 }
375
376 /** @brief Rewinds a Matlab MAT file to the first variable
377 *
378 * Rewinds a Matlab MAT file to the first variable
379 * @ingroup MAT
380 * @param mat Pointer to the MAT file
381 * @retval 0 on success
382 */
383 int
384 Mat_Rewind( mat_t *mat )
385 {
386 switch ( mat->version ) {
387 case MAT_FT_MAT73:
388 mat->next_index = 0;
389 break;
390 case MAT_FT_MAT5:
391 fseek(mat->fp,128L,SEEK_SET);
392 break;
393 case MAT_FT_MAT4:
394 fseek(mat->fp,0L,SEEK_SET);
395 break;
396 default:
397 return -1;
398 }
399 return 0;
400 }
401
402 /** @brief Returns the size of a Matlab Class
403 *
404 * Returns the size (in bytes) of the matlab class class_type
405 * @ingroup MAT
406 * @param class_type Matlab class type (MAT_C_*)
407 * @returns Size of the class
408 */
409 size_t
410 Mat_SizeOfClass(int class_type)
411 {
412 switch (class_type) {
413 case MAT_C_DOUBLE:
414 return sizeof(double);
415 case MAT_C_SINGLE:
416 return sizeof(float);
417 #ifdef HAVE_MAT_INT64_T
418 case MAT_C_INT64:
419 return sizeof(mat_int64_t);
420 #endif
421 #ifdef HAVE_MAT_UINT64_T
422 case MAT_C_UINT64:
423 return sizeof(mat_uint64_t);
424 #endif
425 case MAT_C_INT32:
426 return sizeof(mat_int32_t);
427 case MAT_C_UINT32:
428 return sizeof(mat_uint32_t);
429 case MAT_C_INT16:
430 return sizeof(mat_int16_t);
431 case MAT_C_UINT16:
432 return sizeof(mat_uint16_t);
433 case MAT_C_INT8:
434 return sizeof(mat_int8_t);
435 case MAT_C_UINT8:
436 return sizeof(mat_uint8_t);
437 case MAT_C_CHAR:
438 return sizeof(mat_int16_t);
439 default:
440 return 0;
441 }
442 }
443
444 /*
445 *===================================================================
446 * MAT Variable Functions
447 *===================================================================
448 */
449
450 /** @brief Allocates memory for a new matvar_t and initializes all the fields
451 *
452 * @ingroup MAT
453 * @return A newly allocated matvar_t
454 */
455 matvar_t *
456 Mat_VarCalloc(void)
457 {
458 matvar_t *matvar;
459
460 matvar = malloc(sizeof(*matvar));
461
462 if ( NULL != matvar ) {
463 matvar->nbytes = 0;
464 matvar->rank = 0;
465 matvar->data_type = MAT_T_UNKNOWN;
466 matvar->data_size = 0;
467 matvar->class_type = MAT_C_EMPTY;
468 matvar->isComplex = 0;
469 matvar->isGlobal = 0;
470 matvar->isLogical = 0;
471 matvar->dims = NULL;
472 matvar->name = NULL;
473 matvar->data = NULL;
474 matvar->mem_conserve = 0;
475 matvar->compression = 0;
476 matvar->internal = malloc(sizeof(*matvar->internal));
477 if ( NULL == matvar->internal ) {
478 free(matvar);
479 matvar = NULL;
480 } else {
481 matvar->internal->hdf5_name = NULL;
482 matvar->internal->hdf5_ref = 0;
483 matvar->internal->id = -1;
484 matvar->internal->fp = NULL;
485 matvar->internal->fpos = 0;
486 matvar->internal->datapos = 0;
487 matvar->internal->fieldnames = NULL;
488 matvar->internal->num_fields = 0;
489 #if defined(HAVE_ZLIB)
490 matvar->internal->z = NULL;
491 #endif
492 }
493 }
494
495 return matvar;
496 }
497
498 /** @brief Creates a MAT Variable with the given name and (optionally) data
499 *
500 * Creates a MAT variable that can be written to a Matlab MAT file with the
501 * given name, data type, dimensions and data. Rank should always be 2 or more.
502 * i.e. Scalar values would have rank=2 and dims[2] = {1,1}. Data type is
503 * one of the MAT_T types. MAT adds MAT_T_STRUCT and MAT_T_CELL to create
504 * Structures and Cell Arrays respectively. For MAT_T_STRUCT, data should be a
505 * NULL terminated array of matvar_t * variables (i.e. for a 3x2 structure with
506 * 10 fields, there should be 61 matvar_t * variables where the last one is
507 * NULL). For cell arrays, the NULL termination isn't necessary. So to create
508 * a cell array of size 3x2, data would be the address of an array of 6
509 * matvar_t * variables.
510 *
511 * EXAMPLE:
512 * To create a struct of size 3x2 with 3 fields:
513 * @code
514 * int rank=2, dims[2] = {3,2}, nfields = 3;
515 * matvar_t **vars;
516 *
517 * vars = malloc((3*2*nfields+1)*sizeof(matvar_t *));
518 * vars[0] = Mat_VarCreate(...);
519 * :
520 * vars[3*2*nfields-1] = Mat_VarCreate(...);
521 * vars[3*2*nfields] = NULL;
522 * @endcode
523 *
524 * EXAMPLE:
525 * To create a cell array of size 3x2:
526 * @code
527 * int rank=2, dims[2] = {3,2};
528 * matvar_t **vars;
529 *
530 * vars = malloc(3*2*sizeof(matvar_t *));
531 * vars[0] = Mat_VarCreate(...);
532 * :
533 * vars[5] = Mat_VarCreate(...);
534 * @endcode
535 *
536 * @ingroup MAT
537 * @param name Name of the variable to create
538 * @param class_type class type of the variable in Matlab(one of the mx Classes)
539 * @param data_type data type of the variable (one of the MAT_T_ Types)
540 * @param rank Rank of the variable
541 * @param dims array of dimensions of the variable of size rank
542 * @param data pointer to the data
543 * @param opt 0, or bitwise or of the following options:
544 * - MAT_F_DONT_COPY_DATA to just use the pointer to the data and not copy the
545 * data itself. Note that the pointer should not be freed until you are
546 * done with the mat variable. The Mat_VarFree function will NOT free
547 * data that was created with MAT_F_DONT_COPY_DATA, so free it yourself.
548 * - MAT_F_COMPLEX to specify that the data is complex. The data variable
549 * should be a pointer to a mat_complex_split_t type.
550 * - MAT_F_GLOBAL to assign the variable as a global variable
551 * - MAT_F_LOGICAL to specify that it is a logical variable
552 * @return A MAT variable that can be written to a file or otherwise used
553 */
554 matvar_t *
555 Mat_VarCreate(const char *name,enum matio_classes class_type,
556 enum matio_types data_type,int rank,size_t *dims,void *data,int opt)
557 {
558 size_t i, nmemb = 1, nfields = 0, data_size;
559 matvar_t *matvar = NULL;
560
561 if (dims == NULL) return NULL;
562
563 matvar = Mat_VarCalloc();
564 if ( NULL == matvar )
565 return NULL;
566
567 matvar->compression = MAT_COMPRESSION_NONE;
568 matvar->isComplex = opt & MAT_F_COMPLEX;
569 matvar->isGlobal = opt & MAT_F_GLOBAL;
570 matvar->isLogical = opt & MAT_F_LOGICAL;
571 if ( name )
572 matvar->name = strdup_printf("%s",name);
573 matvar->rank = rank;
574 matvar->dims = malloc(matvar->rank*sizeof(*matvar->dims));
575 for ( i = 0; i < matvar->rank; i++ ) {
576 matvar->dims[i] = dims[i];
577 nmemb *= dims[i];
578 }
579 matvar->class_type = class_type;
580 matvar->data_type = data_type;
581 switch ( data_type ) {
582 case MAT_T_INT8:
583 data_size = 1;
584 break;
585 case MAT_T_UINT8:
586 data_size = 1;
587 break;
588 case MAT_T_INT16:
589 data_size = 2;
590 break;
591 case MAT_T_UINT16:
592 data_size = 2;
593 break;
594 case MAT_T_INT64:
595 data_size = 8;
596 break;
597 case MAT_T_UINT64:
598 data_size = 8;
599 break;
600 case MAT_T_INT32:
601 data_size = 4;
602 break;
603 case MAT_T_UINT32:
604 data_size = 4;
605 break;
606 case MAT_T_SINGLE:
607 data_size = sizeof(float);
608 break;
609 case MAT_T_DOUBLE:
610 data_size = sizeof(double);
611 break;
612 case MAT_T_UTF8:
613 data_size = 1;
614 break;
615 case MAT_T_UTF16:
616 data_size = 2;
617 break;
618 case MAT_T_UTF32:
619 data_size = 4;
620 break;
621 case MAT_T_CELL:
622 data_size = sizeof(matvar_t **);
623 break;
624 case MAT_T_STRUCT:
625 {
626 matvar_t **fields;
627
628 data_size = sizeof(matvar_t **);
629 if ( data != NULL ) {
630 fields = data;
631 nfields = 0;
632 while ( fields[nfields] != NULL )
633 nfields++;
634 if ( nmemb )
635 nfields = nfields / nmemb;
636 matvar->internal->num_fields = nfields;
637 matvar->internal->fieldnames =
638 calloc(nfields,sizeof(*matvar->internal->fieldnames));
639 for ( i = 0; i < nfields; i++ )
640 matvar->internal->fieldnames[i] = strdup(fields[i]->name);
641 if ( nfields )
642 nmemb *= nfields;
643 }
644 break;
645 }
646 default:
647 Mat_Error("Unrecognized data_type");
648 Mat_VarFree(matvar);
649 return NULL;
650 }
651 if ( matvar->class_type == MAT_C_SPARSE ) {
652 matvar->data_size = sizeof(mat_sparse_t);
653 matvar->nbytes = matvar->data_size;
654 } else {
655 matvar->data_size = data_size;
656 matvar->nbytes = nmemb*matvar->data_size;
657 }
658 if ( data == NULL ) {
659 if ( MAT_C_CELL == matvar->class_type && nmemb > 0 )
660 matvar->data = calloc(nmemb,sizeof(matvar_t*));
661 else
662 matvar->data = NULL;
663 } else if ( opt & MAT_F_DONT_COPY_DATA ) {
664 matvar->data = data;
665 matvar->mem_conserve = 1;
666 } else if ( MAT_C_SPARSE == matvar->class_type ) {
667 mat_sparse_t *sparse_data, *sparse_data_in;
668
669 sparse_data_in = data;
670 sparse_data = malloc(sizeof(mat_sparse_t));
671 if ( NULL != sparse_data ) {
672 sparse_data->nzmax = sparse_data_in->nzmax;
673 sparse_data->nir = sparse_data_in->nir;
674 sparse_data->njc = sparse_data_in->njc;
675 sparse_data->ndata = sparse_data_in->ndata;
676 sparse_data->ir = malloc(sparse_data->nir*sizeof(*sparse_data->ir));
677 if ( NULL != sparse_data->ir )
678 memcpy(sparse_data->ir,sparse_data_in->ir,
679 sparse_data->nir*sizeof(*sparse_data->ir));
680 sparse_data->jc = malloc(sparse_data->njc*sizeof(*sparse_data->jc));
681 if ( NULL != sparse_data->jc )
682 memcpy(sparse_data->jc,sparse_data_in->jc,
683 sparse_data->njc*sizeof(*sparse_data->jc));
684 if ( matvar->isComplex ) {
685 sparse_data->data = malloc(sizeof(mat_complex_split_t));
686 if ( NULL != sparse_data->data ) {
687 mat_complex_split_t *complex_data,*complex_data_in;
688 complex_data = sparse_data->data;
689 complex_data_in = sparse_data_in->data;
690 complex_data->Re = malloc(sparse_data->ndata*data_size);
691 complex_data->Im = malloc(sparse_data->ndata*data_size);
692 if ( NULL != complex_data->Re )
693 memcpy(complex_data->Re,complex_data_in->Re,
694 sparse_data->ndata*data_size);
695 if ( NULL != complex_data->Im )
696 memcpy(complex_data->Im,complex_data_in->Im,
697 sparse_data->ndata*data_size);
698 }
699 } else {
700 sparse_data->data = malloc(sparse_data->ndata*data_size);
701 if ( NULL != sparse_data->data )
702 memcpy(sparse_data->data,sparse_data_in->data,
703 sparse_data->ndata*data_size);
704 }
705 }
706 matvar->data = sparse_data;
707 } else {
708 if ( matvar->isComplex ) {
709 matvar->data = malloc(sizeof(mat_complex_split_t));
710 if ( NULL != matvar->data && matvar->nbytes > 0 ) {
711 mat_complex_split_t *complex_data = matvar->data;
712 mat_complex_split_t *complex_data_in = data;
713
714 complex_data->Re = malloc(matvar->nbytes);
715 complex_data->Im = malloc(matvar->nbytes);
716 if ( NULL != complex_data->Re )
717 memcpy(complex_data->Re,complex_data_in->Re,matvar->nbytes);
718 if ( NULL != complex_data->Im )
719 memcpy(complex_data->Im,complex_data_in->Im,matvar->nbytes);
720 }
721 } else if ( matvar->nbytes > 0 ) {
722 matvar->data = malloc(matvar->nbytes);
723 if ( NULL != matvar->data )
724 memcpy(matvar->data,data,matvar->nbytes);
725 }
726 matvar->mem_conserve = 0;
727 }
728
729 return matvar;
730 }
731
732 /** @brief Deletes a variable from a file
733 *
734 * @ingroup MAT
735 * @param mat Pointer to the mat_t file structure
736 * @param name Name of the variable to delete
737 * @returns 0 on success
738 */
739 int
740 Mat_VarDelete(mat_t *mat, const char *name)
741 {
742 int err = 1;
743 enum mat_ft mat_file_ver;
744 char *tmp_name, *new_name, *temp;
745 mat_t *tmp;
746 matvar_t *matvar;
747
748 switch ( mat->version ) {
749 case 0x0200:
750 mat_file_ver = MAT_FT_MAT73;
751 break;
752 case 0x0100:
753 mat_file_ver = MAT_FT_MAT5;
754 break;
755 case 0x0010:
756 mat_file_ver = MAT_FT_MAT4;
757 break;
758 }
759
760 temp = strdup_printf("XXXXXX");
761 tmp_name = mktemp(temp);
762 tmp = Mat_CreateVer(tmp_name,mat->header,mat_file_ver);
763 if ( tmp != NULL ) {
764 while ( NULL != (matvar = Mat_VarReadNext(mat)) ) {
765 if ( strcmp(matvar->name,name) )
766 Mat_VarWrite(tmp,matvar,0);
767 else
768 err = 0;
769 Mat_VarFree(matvar);
770 }
771 /* FIXME: Memory leak */
772 new_name = strdup_printf("%s",mat->filename);
773 fclose(mat->fp);
774
775 if ( (err = remove(new_name)) == -1 ) {
776 Mat_Critical("remove of %s failed",new_name);
777 } else if ( !Mat_Close(tmp) && (err=rename(tmp_name,new_name))==-1) {
778 Mat_Critical("rename failed oldname=%s,newname=%s",tmp_name,
779 new_name);
780 } else {
781 tmp = Mat_Open(new_name,mat->mode);
782 memcpy(mat,tmp,sizeof(mat_t));
783 }
784 free(tmp);
785 free(new_name);
786 }
787 free(temp);
788 return err;
789 }
790
791 /** @brief Duplicates a matvar_t structure
792 *
793 * Provides a clean function for duplicating a matvar_t structure.
794 * @ingroup MAT
795 * @param in pointer to the matvar_t structure to be duplicated
796 * @param opt 0 does a shallow duplicate and only assigns the data pointer to
797 * the duplicated array. 1 will do a deep duplicate and actually
798 * duplicate the contents of the data. Warning: If you do a shallow
799 * copy and free both structures, the data will be freed twice and
800 * memory will be corrupted. This may be fixed in a later release.
801 * @returns Pointer to the duplicated matvar_t structure.
802 */
803 matvar_t *
804 Mat_VarDuplicate(const matvar_t *in, int opt)
805 {
806 matvar_t *out;
807 int i;
808
809 out = Mat_VarCalloc();
810 if ( out == NULL )
811 return NULL;
812
813 out->nbytes = in->nbytes;
814 out->rank = in->rank;
815 out->data_type = in->data_type;
816 out->data_size = in->data_size;
817 out->class_type = in->class_type;
818 out->isComplex = in->isComplex;
819 out->isGlobal = in->isGlobal;
820 out->isLogical = in->isLogical;
821 out->mem_conserve = in->mem_conserve;
822 out->compression = in->compression;
823
824 out->name = NULL;
825 out->dims = NULL;
826 out->data = NULL;
827
828 if ( NULL != in->internal->hdf5_name )
829 out->internal->hdf5_name = strdup(in->internal->hdf5_name);
830
831 out->internal->hdf5_ref = in->internal->hdf5_ref;
832 out->internal->id = in->internal->id;
833 out->internal->fpos = in->internal->fpos;
834 out->internal->datapos = in->internal->datapos;
835 #if defined(HAVE_ZLIB)
836 out->internal->z = NULL;
837 #endif
838 out->internal->num_fields = in->internal->num_fields;
839 if ( NULL != in->internal->fieldnames && in->internal->num_fields > 0 ) {
840 out->internal->fieldnames = calloc(in->internal->num_fields,
841 sizeof(*in->internal->fieldnames));
842 for ( i = 0; i < in->internal->num_fields; i++ ) {
843 if ( NULL != in->internal->fieldnames[i] )
844 out->internal->fieldnames[i] =
845 strdup(in->internal->fieldnames[i]);
846 }
847 }
848
849 if (in->name != NULL && (NULL != (out->name = malloc(strlen(in->name)+1))))
850 memcpy(out->name,in->name,strlen(in->name)+1);
851
852 out->dims = malloc(in->rank*sizeof(*out->dims));
853 if ( out->dims != NULL )
854 memcpy(out->dims,in->dims,in->rank*sizeof(*out->dims));
855 #if defined(HAVE_ZLIB)
856 if ( (in->internal->z != NULL) && (NULL != (out->internal->z = malloc(sizeof(z_stream)))) )
857 inflateCopy(out->internal->z,in->internal->z);
858 #endif
859
860 if ( !opt ) {
861 out->data = in->data;
862 } else if ( (in->data != NULL) && (in->class_type == MAT_C_STRUCT) ) {
863 matvar_t **infields, **outfields;
864 int nfields = 0;
865
866 out->data = malloc(in->nbytes);
867 if ( out->data != NULL && in->data_size > 0 ) {
868 nfields = in->nbytes / in->data_size;
869 infields = (matvar_t **)in->data;
870 outfields = (matvar_t **)out->data;
871 for ( i = 0; i < nfields; i++ ) {
872 outfields[i] = Mat_VarDuplicate(infields[i],opt);
873 }
874 }
875 } else if ( (in->data != NULL) && (in->class_type == MAT_C_CELL) ) {
876 matvar_t **incells, **outcells;
877 int ncells = 0;
878
879 out->data = malloc(in->nbytes);
880 if ( out->data != NULL && in->data_size > 0 ) {
881 ncells = in->nbytes / in->data_size;
882 incells = (matvar_t **)in->data;
883 outcells = (matvar_t **)out->data;
884 for ( i = 0; i < ncells; i++ ) {
885 outcells[i] = Mat_VarDuplicate(incells[i],opt);
886 }
887 }
888 } else if ( in->data != NULL ) {
889 if ( out->isComplex ) {
890 out->data = malloc(sizeof(mat_complex_split_t));
891 if ( out->data != NULL ) {
892 mat_complex_split_t *out_data = out->data;
893 mat_complex_split_t *in_data = in->data;
894 out_data->Re = malloc(out->nbytes);
895 if ( NULL != out_data->Re )
896 memcpy(out_data->Re,in_data->Re,out->nbytes);
897 out_data->Im = malloc(out->nbytes);
898 if ( NULL != out_data->Im )
899 memcpy(out_data->Im,in_data->Im,out->nbytes);
900 }
901 } else {
902 out->data = malloc(in->nbytes);
903 if ( out->data != NULL )
904 memcpy(out->data,in->data,in->nbytes);
905 }
906 }
907 return out;
908 }
909
910 /** @brief Frees all the allocated memory associated with the structure
911 *
912 * Frees memory used by a MAT variable. Frees the data associated with a
913 * MAT variable if it's non-NULL and MAT_F_DONT_COPY_DATA was not used.
914 * @ingroup MAT
915 * @param matvar Pointer to the matvar_t structure
916 */
917 void
918 Mat_VarFree(matvar_t *matvar)
919 {
920 size_t nmemb = 0, i;
921 if ( !matvar )
922 return;
923 if ( matvar->dims ) {
924 nmemb = 1;
925 for ( i = 0; i < matvar->rank; i++ )
926 nmemb *= matvar->dims[i];
927 free(matvar->dims);
928 }
929 if ( matvar->name )
930 free(matvar->name);
931 if ( matvar->data != NULL) {
932 switch (matvar->class_type ) {
933 case MAT_C_STRUCT:
934 if ( !matvar->mem_conserve && NULL != matvar->data ) {
935 matvar_t **fields = matvar->data;
936 int nfields = matvar->internal->num_fields;
937 for ( i = 0; i < nmemb*nfields; i++ )
938 Mat_VarFree(fields[i]);
939
940 free(matvar->data);
941 break;
942 }
943 case MAT_C_CELL:
944 if ( !matvar->mem_conserve && NULL != matvar->data ) {
945 matvar_t **cells = matvar->data;
946 for ( i = 0; i < nmemb; i++ )
947 Mat_VarFree(cells[i]);
948
949 free(matvar->data);
950 }
951 break;
952 case MAT_C_SPARSE:
953 if ( !matvar->mem_conserve ) {
954 mat_sparse_t *sparse;
955 sparse = matvar->data;
956 if ( sparse->ir != NULL )
957 free(sparse->ir);
958 if ( sparse->jc != NULL )
959 free(sparse->jc);
960 if ( matvar->isComplex && NULL != sparse->data ) {
961 mat_complex_split_t *complex_data = sparse->data;
962 free(complex_data->Re);
963 free(complex_data->Im);
964 free(complex_data);
965 } else if ( sparse->data != NULL ) {
966 free(sparse->data);
967 }
968 free(sparse);
969 }
970 break;
971 case MAT_C_DOUBLE:
972 case MAT_C_SINGLE:
973 case MAT_C_INT64:
974 case MAT_C_UINT64:
975 case MAT_C_INT32:
976 case MAT_C_UINT32:
977 case MAT_C_INT16:
978 case MAT_C_UINT16:
979 case MAT_C_INT8:
980 case MAT_C_UINT8:
981 case MAT_C_CHAR:
982 if ( !matvar->mem_conserve && NULL != matvar->data ) {
983 if ( matvar->isComplex ) {
984 mat_complex_split_t *complex_data = matvar->data;
985 free(complex_data->Re);
986 free(complex_data->Im);
987 free(complex_data);
988 } else {
989 free(matvar->data);
990 }
991 }
992 break;
993 }
994 }
995
996 if ( NULL != matvar->internal ) {
997 #if defined(HAVE_ZLIB)
998 if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
999 inflateEnd(matvar->internal->z);
1000 free(matvar->internal->z);
1001 }
1002 #endif
1003 #if defined(MAT73) && MAT73
1004 if ( -1 < matvar->internal->id ) {
1005 switch ( H5Iget_type(matvar->internal->id) ) {
1006 case H5I_GROUP:
1007 H5Gclose(matvar->internal->id);
1008 matvar->internal->id = -1;
1009 break;
1010 case H5I_DATASET:
1011 H5Dclose(matvar->internal->id);
1012 matvar->internal->id = -1;
1013 break;
1014 default:
1015 break;
1016 }
1017 }
1018 if ( 0 < matvar->internal->hdf5_ref ) {
1019 switch ( H5Iget_type(matvar->internal->id) ) {
1020 case H5I_GROUP:
1021 H5Gclose(matvar->internal->id);
1022 matvar->internal->hdf5_ref = -1;
1023 break;
1024 case H5I_DATASET:
1025 H5Dclose(matvar->internal->id);
1026 matvar->internal->hdf5_ref = -1;
1027 break;
1028 default:
1029 break;
1030 }
1031 }
1032 if ( NULL != matvar->internal->hdf5_name ) {
1033 free(matvar->internal->hdf5_name);
1034 matvar->internal->hdf5_name = NULL;
1035 }
1036 #endif
1037 if ( NULL != matvar->internal->fieldnames &&
1038 matvar->internal->num_fields > 0 ) {
1039 size_t i;
1040 for ( i = 0; i < matvar->internal->num_fields; i++ ) {
1041 if ( NULL != matvar->internal->fieldnames[i] )
1042 free(matvar->internal->fieldnames[i]);
1043 }
1044 free(matvar->internal->fieldnames);
1045 }
1046 free(matvar->internal);
1047 matvar->internal = NULL;
1048 }
1049 /* FIXME: Why does this cause a SEGV? */
1050 #if 0
1051 memset(matvar,0,sizeof(matvar_t));
1052 #endif
1053 free(matvar);
1054 }
1055
1056 void
1057 Mat_VarFree2(matvar_t *matvar)
1058 {
1059 if ( !matvar )
1060 return;
1061 if ( matvar->dims )
1062 free(matvar->dims);
1063 if ( matvar->name )
1064 free(matvar->name);
1065 if ( (matvar->data != NULL) && (matvar->class_type == MAT_C_STRUCT ||
1066 matvar->class_type == MAT_C_CELL) && matvar->data_size > 0 ) {
1067 int i;
1068 matvar_t **fields = (matvar_t **)matvar->data;
1069 int nfields = matvar->nbytes / matvar->data_size;
1070 for ( i = 0; i < nfields; i++ )
1071 Mat_VarFree(fields[i]);
1072 free(matvar->data);
1073 } else if ( (matvar->data != NULL) && (!matvar->mem_conserve) &&
1074 (matvar->class_type == MAT_C_SPARSE) ) {
1075 mat_sparse_t *sparse;
1076 sparse = matvar->data;
1077 if ( sparse->ir != NULL )
1078 free(sparse->ir);
1079 if ( sparse->jc != NULL )
1080 free(sparse->jc);
1081 if ( sparse->data != NULL )
1082 free(sparse->data);
1083 free(sparse);
1084 } else {
1085 if ( matvar->data && !matvar->mem_conserve )
1086 free(matvar->data);
1087 }
1088 #if defined(HAVE_ZLIB)
1089 if ( matvar->compression == MAT_COMPRESSION_ZLIB )
1090 inflateEnd(matvar->internal->z);
1091 #endif
1092 /* FIXME: Why does this cause a SEGV? */
1093 #if 0
1094 memset(matvar,0,sizeof(matvar_t));
1095 #endif
1096 }
1097
1098 /** @brief Calculate a single subscript from a set of subscript values
1099 *
1100 * Calculates a single linear subscript (0-relative) given a 1-relative
1101 * subscript for each dimension. The calculation uses the formula below where
1102 * index is the linear index, s is an array of length RANK where each element
1103 * is the subscript for the correspondind dimension, D is an array whose
1104 * elements are the dimensions of the variable.
1105 * \f[
1106 * index = \sum\limits_{k=0}^{RANK-1} [(s_k - 1) \prod\limits_{l=0}^{k} D_l ]
1107 * \f]
1108 * @ingroup MAT
1109 * @param rank Rank of the variable
1110 * @param dims dimensions of the variable
1111 * @param subs Dimension subscripts
1112 * @return Single (linear) subscript
1113 */
1114 int
1115 Mat_CalcSingleSubscript(int rank,int *dims,int *subs)
1116 {
1117 int index = 0, i, j, k, err = 0;
1118
1119 for ( i = 0; i < rank; i++ ) {
1120 k = subs[i];
1121 if ( k > dims[i] ) {
1122 err = 1;
1123 Mat_Critical("Mat_CalcSingleSubscript: index out of bounds");
1124 break;
1125 } else if ( k < 1 ) {
1126 err = 1;
1127 break;
1128 }
1129 k--;
1130 for ( j = i; j--; )
1131 k *= dims[j];
1132 index += k;
1133 }
1134 if ( err )
1135 index = -1;
1136
1137 return index;
1138 }
1139
1140
1141 /** @brief Calculate a set of subscript values from a single(linear) subscript
1142 *
1143 * Calculates 1-relative subscripts for each dimension given a 0-relative
1144 * linear index. Subscripts are calculated as follows where s is the array
1145 * of dimension subscripts, D is the array of dimensions, and index is the
1146 * linear index.
1147 * \f[
1148 * s_k = \lfloor\frac{1}{L} \prod\limits_{l = 0}^{k} D_l\rfloor + 1
1149 * \f]
1150 * \f[
1151 * L = index - \sum\limits_{l = k}^{RANK - 1} s_k \prod\limits_{m = 0}^{k} D_m
1152 * \f]
1153 * @ingroup MAT
1154 * @param rank Rank of the variable
1155 * @param dims dimensions of the variable
1156 * @param index linear index
1157 * @return Array of dimension subscripts
1158 */
1159 int *
1160 Mat_CalcSubscripts(int rank,int *dims,int index)
1161 {
1162 int i, j, k, *subs;
1163 double l;
1164
1165 subs = malloc(rank*sizeof(int));
1166 l = index;
1167 for ( i = rank; i--; ) {
1168 k = 1;
1169 for ( j = i; j--; )
1170 k *= dims[j];
1171 subs[i] = floor(l / (double)k);
1172 l -= subs[i]*k;
1173 subs[i]++;
1174 }
1175
1176 return subs;
1177 }
1178
1179 /** @brief Calculates the size of a matlab variable in bytes
1180 *
1181 * @ingroup MAT
1182 * @param matvar matlab variable
1183 * @returns size of the variable in bytes
1184 */
1185 size_t
1186 Mat_VarGetSize(matvar_t *matvar)
1187 {
1188 int nmemb, i;
1189 size_t bytes = 0;
1190
1191 if ( matvar->class_type == MAT_C_STRUCT ) {
1192 int nfields;
1193 matvar_t **fields;
1194 /* This is really nmemb*nfields, but we'll get a
1195 * more accurate count of the bytes by loopoing over all of them
1196 */
1197 nfields = matvar->internal->num_fields;
1198 fields = matvar->data;
1199 for ( i = 0; i < nfields; i++ )
1200 bytes += Mat_VarGetSize(fields[i]);
1201 } else if ( matvar->class_type == MAT_C_CELL ) {
1202 int ncells;
1203 matvar_t **cells;
1204
1205 ncells = matvar->nbytes / matvar->data_size;
1206 cells = matvar->data;
1207 for ( i = 0; i < ncells; i++ )
1208 bytes += Mat_VarGetSize(cells[i]);
1209 } else {
1210 nmemb = 1;
1211 for ( i = 0; i < matvar->rank; i++ )
1212 nmemb *= matvar->dims[i];
1213 bytes += nmemb*Mat_SizeOfClass(matvar->class_type);
1214 }
1215 return bytes;
1216 }
1217
1218 /** @brief Prints the variable information
1219 *
1220 * Prints to stdout the values of the @ref matvar_t structure
1221 * @ingroup MAT
1222 * @param matvar Pointer to the matvar_t structure
1223 * @param printdata set to 1 if the Variables data should be printed, else 0
1224 */
1225 void
1226 Mat_VarPrint( matvar_t *matvar, int printdata )
1227 {
1228 size_t nmemb;
1229 int i, j;
1230 const char *class_type_desc[16] = {"Undefined","Cell Array","Structure",
1231 "Object","Character Array","Sparse Array","Double Precision Array",
1232 "Single Precision Array", "8-bit, signed integer array",
1233 "8-bit, unsigned integer array","16-bit, signed integer array",
1234 "16-bit, unsigned integer array","32-bit, signed integer array",
1235 "32-bit, unsigned integer array","64-bit, signed integer array",
1236 "64-bit, unsigned integer array"};
1237 const char *data_type_desc[23] = {"Unknown","8-bit, signed integer",
1238 "8-bit, unsigned integer","16-bit, signed integer",
1239 "16-bit, unsigned integer","32-bit, signed integer",
1240 "32-bit, unsigned integer","IEEE 754 single-precision","RESERVED",
1241 "IEEE 754 double-precision","RESERVED","RESERVED",
1242 "64-bit, signed integer","64-bit, unsigned integer", "Matlab Array",
1243 "Compressed Data","Unicode UTF-8 Encoded Character Data",
1244 "Unicode UTF-16 Encoded Character Data",
1245 "Unicode UTF-32 Encoded Character Data","","String","Cell Array",
1246 "Structure"};
1247
1248 if ( matvar == NULL )
1249 return;
1250 if ( matvar->name )
1251 printf(" Name: %s\n", matvar->name);
1252 printf(" Rank: %d\n", matvar->rank);
1253 if ( matvar->rank == 0 )
1254 return;
1255 printf("Dimensions: %zu",matvar->dims[0]);
1256 nmemb = matvar->dims[0];
1257 for ( i = 1; i < matvar->rank; i++ ) {
1258 printf(" x %zu",matvar->dims[i]);
1259 nmemb *= matvar->dims[i];
1260 }
1261 printf("\n");
1262 printf("Class Type: %s",class_type_desc[matvar->class_type]);
1263 if ( matvar->isComplex )
1264 printf(" (complex)");
1265 printf("\n");
1266 printf("Compresson: "); /* gjl march 2013 */
1267 if ( matvar->compression == MAT_COMPRESSION_NONE ) printf("none");
1268 else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) printf("zlib");
1269 else printf("unknown");
1270 printf("\n");
1271 if ( matvar->data_type )
1272 printf(" Data Type: %s\n", data_type_desc[matvar->data_type]);
1273
1274 if ( MAT_C_STRUCT == matvar->class_type ) {
1275 matvar_t **fields = (matvar_t **)matvar->data;
1276 int nfields = matvar->internal->num_fields;
1277 if ( nmemb*nfields > 0 ) {
1278 printf("Fields[%zu] {\n", nfields*nmemb);
1279 for ( i = 0; i < nfields*nmemb; i++ ) {
1280 if ( NULL == fields[i] ) {
1281 printf(" Name: %s\n Rank: %d\n",
1282 matvar->internal->fieldnames[i%nfields],0);
1283 } else {
1284 Mat_VarPrint(fields[i],printdata);
1285 }
1286 }
1287 printf("}\n");
1288 } else {
1289 printf("Fields[%d] {\n", nfields);
1290 for ( i = 0; i < nfields; i++ )
1291 printf(" Name: %s\n Rank: %d\n",
1292 matvar->internal->fieldnames[i],0);
1293 printf("}\n");
1294 }
1295 return;
1296 } else if ( matvar->data == NULL || matvar->data_size < 1 ) {
1297 return;
1298 } else if ( MAT_C_CELL == matvar->class_type ) {
1299 matvar_t **cells = (matvar_t **)matvar->data;
1300 int ncells = matvar->nbytes / matvar->data_size;
1301 printf("{\n");
1302 for ( i = 0; i < ncells; i++ )
1303 Mat_VarPrint(cells[i],printdata);
1304 printf("}\n");
1305 return;
1306 } else if ( !printdata ) {
1307 return;
1308 }
1309
1310 printf("{\n");
1311
1312 if ( matvar->rank > 2 ) {
1313 printf("I can't print more than 2 dimensions\n");
1314 } else if ( matvar->rank == 1 && matvar->dims[0] > 15 ) {
1315 printf("I won't print more than 15 elements in a vector\n");
1316 } else if ( matvar->rank==2 ) {
1317 switch( matvar->class_type ) {
1318 case MAT_C_DOUBLE:
1319 case MAT_C_SINGLE:
1320 #ifdef HAVE_MAT_INT64_T
1321 case MAT_C_INT64:
1322 #endif
1323 #ifdef HAVE_MAT_UINT64_T
1324 case MAT_C_UINT64:
1325 #endif
1326 case MAT_C_INT32:
1327 case MAT_C_UINT32:
1328 case MAT_C_INT16:
1329 case MAT_C_UINT16:
1330 case MAT_C_INT8:
1331 case MAT_C_UINT8:
1332 {
1333 size_t stride = Mat_SizeOf(matvar->data_type);
1334 if ( matvar->isComplex ) {
1335 mat_complex_split_t *complex_data = matvar->data;
1336 char *rp = complex_data->Re;
1337 char *ip = complex_data->Im;
1338 for ( i = 0; i < matvar->dims[0] && i < 15; i++ ) {
1339 for ( j = 0; j < matvar->dims[1] && j < 15; j++ ) {
1340 size_t idx = matvar->dims[0]*j+i;
1341 Mat_PrintNumber(matvar->data_type,rp+idx*stride);
1342 printf(" + ");
1343 Mat_PrintNumber(matvar->data_type,ip+idx*stride);
1344 printf("i ");
1345 }
1346 if ( j < matvar->dims[1] )
1347 printf("...");
1348 printf("\n");
1349 }
1350 if ( i < matvar->dims[0] )
1351 printf(".\n.\n.\n");
1352 } else {
1353 char *data = matvar->data;
1354 for ( i = 0; i < matvar->dims[0] && i < 15; i++ ) {
1355 for ( j = 0; j < matvar->dims[1] && j < 15; j++ ) {
1356 size_t idx = matvar->dims[0]*j+i;
1357 Mat_PrintNumber(matvar->data_type,
1358 data+idx*stride);
1359 printf(" ");
1360 }
1361 if ( j < matvar->dims[1] )
1362 printf("...");
1363 printf("\n");
1364 }
1365 if ( i < matvar->dims[0] )
1366 printf(".\n.\n.\n");
1367 }
1368 break;
1369 }
1370 case MAT_C_CHAR:
1371 {
1372 char *data = matvar->data;
1373 if ( !printdata )
1374 break;
1375 for ( i = 0; i < matvar->dims[0]; i++ ) {
1376 j = 0;
1377 for ( j = 0; j < matvar->dims[1]; j++ )
1378 printf("%c",data[j*matvar->dims[0]+i]);
1379 printf("\n");
1380 }
1381 break;
1382 }
1383 case MAT_C_SPARSE:
1384 {
1385 mat_sparse_t *sparse;
1386 size_t stride = Mat_SizeOf(matvar->data_type);
1387 #if !defined(EXTENDED_SPARSE)
1388 if ( MAT_T_DOUBLE != matvar->data_type )
1389 break;
1390 #endif
1391 sparse = matvar->data;
1392 if ( matvar->isComplex ) {
1393 mat_complex_split_t *complex_data = sparse->data;
1394 char *re,*im;
1395 re = complex_data->Re;
1396 im = complex_data->Im;
1397 for ( i = 0; i < sparse->njc-1; i++ ) {
1398 for (j = sparse->jc[i];
1399 j<sparse->jc[i+1] && j<sparse->ndata;j++ ) {
1400 printf(" (%d,%d) ",sparse->ir[j]+1,i+1);
1401 Mat_PrintNumber(matvar->data_type,re+j*stride);
1402 printf(" + ");
1403 Mat_PrintNumber(matvar->data_type,im+j*stride);
1404 printf("i\n");
1405 }
1406 }
1407 } else {
1408 char *data;
1409 data = sparse->data;
1410 for ( i = 0; i < sparse->njc-1; i++ ) {
1411 for (j = sparse->jc[i];
1412 j<sparse->jc[i+1] && j<sparse->ndata;j++ ){
1413 printf(" (%d,%d) ",sparse->ir[j]+1,i+1);
1414 Mat_PrintNumber(matvar->data_type,data+j*stride);
1415 printf("\n");
1416 }
1417 }
1418 }
1419 break;
1420 } /* case MAT_C_SPARSE: */
1421 } /* switch( matvar->class_type ) */
1422 }
1423
1424 printf("}\n");
1425
1426 return;
1427 }
1428
1429 /** @brief Reads MAT variable data from a file
1430 *
1431 * Reads data from a MAT variable. The variable must have been read by
1432 * Mat_VarReadInfo.
1433 * @ingroup MAT
1434 * @param mat MAT file to read data from
1435 * @param matvar MAT variable information
1436 * @param data pointer to store data in (must be pre-allocated)
1437 * @param start array of starting indeces
1438 * @param stride stride of data
1439 * @param edge array specifying the number to read in each direction
1440 * @retval 0 on success
1441 */
1442 int
1443 Mat_VarReadData(mat_t *mat,matvar_t *matvar,void *data,
1444 int *start,int *stride,int *edge)
1445 {
1446 int err = 0;
1447
1448 if ( mat->version != MAT_FT_MAT4 )
1449 err = ReadData5(mat,matvar,data,start,stride,edge);
1450 else
1451 err = ReadData4(mat,matvar,data,start,stride,edge);
1452 return err;
1453 }
1454
1455 /** @brief Reads all the data for a matlab variable
1456 *
1457 * Allocates memory for an reads the data for a given matlab variable.
1458 * @ingroup MAT
1459 * @param mat Matlab MAT file structure pointer
1460 * @param matvar Variable whose data is to be read
1461 * @returns non-zero on error
1462 */
1463 int
1464 Mat_VarReadDataAll(mat_t *mat,matvar_t *matvar)
1465 {
1466 int err = 0;
1467
1468 if ( (mat == NULL) || (matvar == NULL) )
1469 err = 1;
1470 else
1471 ReadData(mat,matvar);
1472
1473 return err;
1474 }
1475
1476 /** @brief Reads MAT variable data from a file
1477 *
1478 * Reads data from a MAT variable using a linear indexingmode. The variable
1479 * must have been read by Mat_VarReadInfo.
1480 * @ingroup MAT
1481 * @param mat MAT file to read data from
1482 * @param matvar MAT variable information
1483 * @param data pointer to store data in (must be pre-allocated)
1484 * @param start starting index
1485 * @param stride stride of data
1486 * @param edge number of elements to read
1487 * @retval 0 on success
1488 */
1489 int
1490 Mat_VarReadDataLinear(mat_t *mat,matvar_t *matvar,void *data,int start,
1491 int stride,int edge)
1492 {
1493 int err = 0, nmemb = 1, i, real_bytes = 0;
1494 mat_int32_t tag[2];
1495 #if defined(HAVE_ZLIB)
1496 z_stream z;
1497 #endif
1498
1499 if ( mat->version == MAT_FT_MAT4 )
1500 return -1;
1501 fseek(mat->fp,matvar->internal->datapos,SEEK_SET);
1502 if ( matvar->compression == MAT_COMPRESSION_NONE ) {
1503 fread(tag,4,2,mat->fp);
1504 if ( mat->byteswap ) {
1505 Mat_int32Swap(tag);
1506 Mat_int32Swap(tag+1);
1507 }
1508 matvar->data_type = tag[0] & 0x000000ff;
1509 if ( tag[0] & 0xffff0000 ) { /* Data is packed in the tag */
1510 fseek(mat->fp,-4,SEEK_CUR);
1511 real_bytes = 4+(tag[0] >> 16);
1512 } else {
1513 real_bytes = 8+tag[1];
1514 }
1515 #if defined(HAVE_ZLIB)
1516 } else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
1517 matvar->internal->z->avail_in = 0;
1518 err = inflateCopy(&z,matvar->internal->z);
1519 InflateDataType(mat,&z,tag);
1520 if ( mat->byteswap ) {
1521 Mat_int32Swap(tag);
1522 Mat_int32Swap(tag+1);
1523 }
1524 matvar->data_type = tag[0] & 0x000000ff;
1525 if ( !(tag[0] & 0xffff0000) ) {/* Data is NOT packed in the tag */
1526 /* We're cheating, but InflateDataType just inflates 4 bytes */
1527 InflateDataType(mat,&z,tag+1);
1528 if ( mat->byteswap ) {
1529 Mat_int32Swap(tag+1);
1530 }
1531 real_bytes = 8+tag[1];
1532 } else {
1533 real_bytes = 4+(tag[0] >> 16);
1534 }
1535 #endif
1536 }
1537 if ( real_bytes % 8 )
1538 real_bytes += (8-(real_bytes % 8));
1539
1540 for ( i = 0; i < matvar->rank; i++ )
1541 nmemb *= matvar->dims[i];
1542
1543 if ( stride*(edge-1)+start+1 > nmemb ) {
1544 err = 1;
1545 } else if ( matvar->compression == MAT_COMPRESSION_NONE ) {
1546 if ( matvar->isComplex ) {
1547 mat_complex_split_t *complex_data = data;
1548
1549 ReadDataSlab1(mat,complex_data->Re,matvar->class_type,
1550 matvar->data_type,start,stride,edge);
1551 fseek(mat->fp,matvar->internal->datapos+real_bytes,SEEK_SET);
1552 fread(tag,4,2,mat->fp);
1553 if ( mat->byteswap ) {
1554 Mat_int32Swap(tag);
1555 Mat_int32Swap(tag+1);
1556 }
1557 matvar->data_type = tag[0] & 0x000000ff;
1558 if ( tag[0] & 0xffff0000 ) { /* Data is packed in the tag */
1559 fseek(mat->fp,-4,SEEK_CUR);
1560 }
1561 ReadDataSlab1(mat,complex_data->Im,matvar->class_type,
1562 matvar->data_type,start,stride,edge);
1563 } else {
1564 ReadDataSlab1(mat,data,matvar->class_type,
1565 matvar->data_type,start,stride,edge);
1566 }
1567 #if defined(HAVE_ZLIB)
1568 } else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
1569 if ( matvar->isComplex ) {
1570 mat_complex_split_t *complex_data = data;
1571
1572 ReadCompressedDataSlab1(mat,&z,complex_data->Re,
1573 matvar->class_type,matvar->data_type,start,stride,edge);
1574
1575 fseek(mat->fp,matvar->internal->datapos,SEEK_SET);
1576
1577 /* Reset zlib knowledge to before reading real tag */
1578 inflateEnd(&z);
1579 err = inflateCopy(&z,matvar->internal->z);
1580 InflateSkip(mat,&z,real_bytes);
1581 z.avail_in = 0;
1582 InflateDataType(mat,&z,tag);
1583 if ( mat->byteswap ) {
1584 Mat_int32Swap(tag);
1585 }
1586 matvar->data_type = tag[0] & 0x000000ff;
1587 if ( !(tag[0] & 0xffff0000) ) {/*Data is NOT packed in the tag*/
1588 InflateSkip(mat,&z,4);
1589 }
1590 ReadCompressedDataSlab1(mat,&z,complex_data->Im,
1591 matvar->class_type,matvar->data_type,start,stride,edge);
1592 inflateEnd(&z);
1593 } else {
1594 ReadCompressedDataSlab1(mat,&z,data,matvar->class_type,
1595 matvar->data_type,start,stride,edge);
1596 inflateEnd(&z);
1597 }
1598 #endif
1599 }
1600
1601 switch(matvar->class_type) {
1602 case MAT_C_DOUBLE:
1603 matvar->data_type = MAT_T_DOUBLE;
1604 matvar->data_size = sizeof(double);
1605 break;
1606 case MAT_C_SINGLE:
1607 matvar->data_type = MAT_T_SINGLE;
1608 matvar->data_size = sizeof(float);
1609 break;
1610 #ifdef HAVE_MAT_INT64_T
1611 case MAT_C_INT64:
1612 matvar->data_type = MAT_T_INT64;
1613 matvar->data_size = sizeof(mat_int64_t);
1614 break;
1615 #endif /* HAVE_MAT_INT64_T */
1616 #ifdef HAVE_MAT_UINT64_T
1617 case MAT_C_UINT64:
1618 matvar->data_type = MAT_T_UINT64;
1619 matvar->data_size = sizeof(mat_uint64_t);
1620 break;
1621 #endif /* HAVE_MAT_UINT64_T */
1622 case MAT_C_INT32:
1623 matvar->data_type = MAT_T_INT32;
1624 matvar->data_size = sizeof(mat_int32_t);
1625 break;
1626 case MAT_C_UINT32:
1627 matvar->data_type = MAT_T_UINT32;
1628 matvar->data_size = sizeof(mat_uint32_t);
1629 break;
1630 case MAT_C_INT16:
1631 matvar->data_type = MAT_T_INT16;
1632 matvar->data_size = sizeof(mat_int16_t);
1633 break;
1634 case MAT_C_UINT16:
1635 matvar->data_type = MAT_T_UINT16;
1636 matvar->data_size = sizeof(mat_uint16_t);
1637 break;
1638 case MAT_C_INT8:
1639 matvar->data_type = MAT_T_INT8;
1640 matvar->data_size = sizeof(mat_int8_t);
1641 break;
1642 case MAT_C_UINT8:
1643 matvar->data_type = MAT_T_UINT8;
1644 matvar->data_size = sizeof(mat_uint8_t);
1645 break;
1646 }
1647
1648 return err;
1649 }
1650
1651 /** @brief Reads the information of the next variable in a MAT file
1652 *
1653 * Reads the next variable's information (class,flags-complex/global/logical,
1654 * rank,dimensions, name, etc) from the Matlab MAT file. After reading, the MAT
1655 * file is positioned past the current variable.
1656 * @ingroup MAT
1657 * @param mat Pointer to the MAT file
1658 * @return Pointer to the @ref matvar_t structure containing the MAT
1659 * variable information
1660 */
1661 matvar_t *
1662 Mat_VarReadNextInfo( mat_t *mat )
1663 {
1664 if( mat == NULL )
1665 return NULL;
1666 else if ( mat->version == MAT_FT_MAT5 )
1667 return Mat_VarReadNextInfo5(mat);
1668 #if defined(MAT73) && MAT73
1669 else if ( mat->version == MAT_FT_MAT73 )
1670 return Mat_VarReadNextInfo73(mat);
1671 #endif
1672 else
1673 return Mat_VarReadNextInfo4(mat);
1674
1675 return NULL;
1676 }
1677
1678 /** @brief Reads the information of a variable with the given name from a MAT file
1679 *
1680 * Reads the named variable (or the next variable if name is NULL) information
1681 * (class,flags-complex/global/logical,rank,dimensions,and name) from the
1682 * Matlab MAT file
1683 * @ingroup MAT
1684 * @param mat Pointer to the MAT file
1685 * @param name Name of the variable to read
1686 * @return Pointer to the @ref matvar_t structure containing the MAT
1687 * variable information
1688 */
1689 matvar_t *
1690 Mat_VarReadInfo( mat_t *mat, const char *name )
1691 {
1692
1693 long fpos;
1694 matvar_t *matvar = NULL;
1695
1696 if ( (mat == NULL) || (name == NULL) )
1697 return NULL;
1698
1699 if ( mat->version == MAT_FT_MAT73 ) {
1700 do {
1701 matvar = Mat_VarReadNextInfo(mat);
1702 if ( matvar != NULL ) {
1703 if ( !matvar->name ) {
1704 Mat_VarFree(matvar);
1705 matvar = NULL;
1706 } else if ( strcmp(matvar->name,name) ) {
1707 Mat_VarFree(matvar);
1708 matvar = NULL;
1709 }
1710 } else {
1711 Mat_Critical("An error occurred in reading the MAT file");
1712 break;
1713 }
1714 } while ( NULL == matvar && mat->next_index < mat->num_datasets);
1715 } else {
1716 fpos = ftell(mat->fp);
1717 fseek(mat->fp,mat->bof,SEEK_SET);
1718 do {
1719 matvar = Mat_VarReadNextInfo(mat);
1720 if ( matvar != NULL ) {
1721 if ( !matvar->name ) {
1722 Mat_VarFree(matvar);
1723 matvar = NULL;
1724 } else if ( strcmp(matvar->name,name) ) {
1725 Mat_VarFree(matvar);
1726 matvar = NULL;
1727 }
1728 } else {
1729 Mat_Critical("An error occurred in reading the MAT file");
1730 break;
1731 }
1732 } while ( !matvar && !feof(mat->fp) );
1733
1734 fseek(mat->fp,fpos,SEEK_SET);
1735 }
1736 return matvar;
1737 }
1738
1739 /** @brief Reads the variable with the given name from a MAT file
1740 *
1741 * Reads the next variable in the Matlab MAT file
1742 * @ingroup MAT
1743 * @param mat Pointer to the MAT file
1744 * @param name Name of the variable to read
1745 * @return Pointer to the @ref matvar_t structure containing the MAT
1746 * variable information
1747 */
1748 matvar_t *
1749 Mat_VarRead( mat_t *mat, const char *name )
1750 {
1751 long fpos;
1752 matvar_t *matvar = NULL;;
1753
1754 if ( (mat == NULL) || (name == NULL) )
1755 return NULL;
1756
1757 if ( MAT_FT_MAT73 != mat->version )
1758 fpos = ftell(mat->fp);
1759
1760 matvar = Mat_VarReadInfo(mat,name);
1761 if ( matvar )
1762 ReadData(mat,matvar);
1763
1764 if ( MAT_FT_MAT73 != mat->version )
1765 fseek(mat->fp,fpos,SEEK_SET);
1766 return matvar;
1767 }
1768
1769 /** @brief Reads the next variable in a MAT file
1770 *
1771 * Reads the next variable in the Matlab MAT file
1772 * @ingroup MAT
1773 * @param mat Pointer to the MAT file
1774 * @return Pointer to the @ref matvar_t structure containing the MAT
1775 * variable information
1776 */
1777 matvar_t *
1778 Mat_VarReadNext( mat_t *mat )
1779 {
1780 long fpos;
1781 matvar_t *matvar = NULL;
1782
1783 if ( mat->version != MAT_FT_MAT73 ) {
1784 if ( feof(mat->fp) )
1785 return NULL;
1786 /* Read position so we can reset the file position if an error occurs */
1787 fpos = ftell(mat->fp);
1788 }
1789 matvar = Mat_VarReadNextInfo(mat);
1790 if ( matvar )
1791 ReadData(mat,matvar);
1792 else if (mat->version != MAT_FT_MAT73 )
1793 fseek(mat->fp,fpos,SEEK_SET);
1794 return matvar;
1795 }
1796
1797 /** @brief Writes the given MAT variable to a MAT file
1798 *
1799 * Writes the MAT variable information stored in matvar to the given MAT file.
1800 * The variable will be written to the end of the file.
1801 * @ingroup MAT
1802 * @param mat MAT file to write to
1803 * @param matvar MAT variable information to write
1804 * @retval 0 on success
1805 */
1806 int
1807 Mat_VarWriteInfo(mat_t *mat, matvar_t *matvar )
1808 {
1809 if ( mat == NULL || matvar == NULL || mat->fp == NULL )
1810 return -1;
1811 else if ( mat->version != MAT_FT_MAT4 )
1812 WriteInfo5(mat,matvar);
1813 #if 0
1814 else if ( mat->version == MAT_FT_MAT4 )
1815 WriteInfo4(mat,matvar);
1816 #endif
1817
1818 return 0;
1819 }
1820
1821 /** @brief Writes the given data to the MAT variable
1822 *
1823 * Writes data to a MAT variable. The variable must have previously been
1824 * written with Mat_VarWriteInfo.
1825 * @ingroup MAT
1826 * @param mat MAT file to write to
1827 * @param matvar MAT variable information to write
1828 * @param data pointer to the data to write
1829 * @param start array of starting indeces
1830 * @param stride stride of data
1831 * @param edge array specifying the number to read in each direction
1832 * @retval 0 on success
1833 */
1834 int
1835 Mat_VarWriteData(mat_t *mat,matvar_t *matvar,void *data,
1836 int *start,int *stride,int *edge)
1837 {
1838 int err = 0, k, N = 1;
1839
1840 fseek(mat->fp,matvar->internal->datapos+8,SEEK_SET);
1841
1842 if ( mat == NULL || matvar == NULL || data == NULL ) {
1843 err = -1;
1844 } else if ( start == NULL && stride == NULL && edge == NULL ) {
1845 for ( k = 0; k < matvar->rank; k++ )
1846 N *= matvar->dims[k];
1847 if ( matvar->compression == MAT_COMPRESSION_NONE )
1848 WriteData(mat,data,N,matvar->data_type);
1849 #if 0
1850 else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
1851 WriteCompressedData(mat,matvar->internal->z,data,N,matvar->data_type);
1852 (void)deflateEnd(matvar->internal->z);
1853 free(matvar->internal->z);
1854 matvar->internal->z = NULL;
1855 }
1856 #endif
1857 } else if ( matvar->rank == 2 ) {
1858 if ( stride[0]*(edge[0]-1)+start[0]+1 > matvar->dims[0] ) {
1859 err = 1;
1860 } else if ( stride[1]*(edge[1]-1)+start[1]+1 > matvar->dims[1] ) {
1861 err = 1;
1862 } else {
1863 switch ( matvar->class_type ) {
1864 case MAT_C_DOUBLE:
1865 case MAT_C_SINGLE:
1866 case MAT_C_INT64:
1867 case MAT_C_UINT64:
1868 case MAT_C_INT32:
1869 case MAT_C_UINT32:
1870 case MAT_C_INT16:
1871 case MAT_C_UINT16:
1872 case MAT_C_INT8:
1873 case MAT_C_UINT8:
1874 WriteDataSlab2(mat,data,matvar->data_type,matvar->dims,
1875 start,stride,edge);
1876 break;
1877 case MAT_C_CHAR:
1878 WriteCharDataSlab2(mat,data,matvar->data_type,matvar->dims,
1879 start,stride,edge);
1880 break;
1881 }
1882 }
1883 }
1884
1885 return err;
1886 }
1887
1888 /** @brief Writes the given MAT variable to a MAT file
1889 *
1890 * Writes the MAT variable information stored in matvar to the given MAT file.
1891 * The variable will be written to the end of the file.
1892 * @ingroup MAT
1893 * @param mat MAT file to write to
1894 * @param matvar MAT variable information to write
1895 * @param compress Whether or not to compress the data
1896 * (Only valid for version 5 MAT files and variables with numeric data)
1897 * @retval 0 on success
1898 */
1899 int
1900 Mat_VarWrite(mat_t *mat,matvar_t *matvar,enum matio_compression compress)
1901 {
1902 if ( mat == NULL || matvar == NULL )
1903 return -1;
1904 else if ( mat->version == MAT_FT_MAT5 )
1905 Mat_VarWrite5(mat,matvar,compress);
1906 #if defined(MAT73) && MAT73
1907 else if ( mat->version == MAT_FT_MAT73 )
1908 Mat_VarWrite73(mat,matvar,compress);
1909 #endif
1910
1911 return 0;
1912 }