| 1 |
/* |
| 2 |
* Copyright (C) 2005-2012 Christopher C. Hulbert |
| 3 |
* |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions are met: |
| 8 |
* |
| 9 |
* 1. Redistributions of source code must retain the above copyright notice, |
| 10 |
* this list of conditions and the following disclaimer. |
| 11 |
* |
| 12 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer in the |
| 14 |
* documentation and/or other materials provided with the distribution. |
| 15 |
* |
| 16 |
* THIS SOFTWARE IS PROVIDED BY CHRISTOPHER C. HULBERT ``AS IS'' AND ANY |
| 17 |
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 |
* DISCLAIMED. IN NO EVENT SHALL CHRISTOPHER C. HULBERT OR CONTRIBUTORS BE |
| 20 |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 |
* POSSIBILITY OF SUCH DAMAGE. |
| 27 |
*/ |
| 28 |
#include <stdlib.h> |
| 29 |
#include <stdarg.h> |
| 30 |
#include <stdio.h> |
| 31 |
#include <string.h> |
| 32 |
#include <math.h> |
| 33 |
#include <getopt.h> |
| 34 |
#include "matio_private.h" |
| 35 |
#if !defined(HAVE_STRCASECMP) |
| 36 |
# define strcasecmp(a,b) strcmp(a,b) |
| 37 |
#endif |
| 38 |
|
| 39 |
static const char *optstring = "df:hvHV"; |
| 40 |
static struct option options[] = { |
| 41 |
{"data", no_argument, NULL,'d'}, |
| 42 |
{"format", required_argument,NULL,'f'}, |
| 43 |
{"human", no_argument, NULL,'h'}, |
| 44 |
{"verbose", optional_argument,NULL,'v'}, |
| 45 |
{"help", no_argument, NULL,'H'}, |
| 46 |
{"version", no_argument, NULL,'V'}, |
| 47 |
{NULL,0,NULL,0} |
| 48 |
}; |
| 49 |
|
| 50 |
static const char *helpstr[] = { |
| 51 |
"", |
| 52 |
"Usage: matdump [OPTIONS] mat_file [var1 var2 ...]", |
| 53 |
"", |
| 54 |
"Runs various test on the Matlab I/O library libmatio", |
| 55 |
"", |
| 56 |
"OPTIONS", |
| 57 |
"-d,--data Print data with header information", |
| 58 |
"-h,--human Human readable sizes in 'whos' display mode", |
| 59 |
"-v,--verbose Turn on verbose messages", |
| 60 |
"-H,--help This output", |
| 61 |
"-V,--version version information", |
| 62 |
"", |
| 63 |
"mat_file name of the MAT file to dump", |
| 64 |
"var1 var2 ... If specified, dumps only listed variables", |
| 65 |
"", |
| 66 |
"Report bugs to <[email protected]>.", |
| 67 |
NULL |
| 68 |
}; |
| 69 |
|
| 70 |
static char *byteswapping[2] = {"No","Yes"}; |
| 71 |
static char *mxclass[15] = { "mxCELL_CLASS", "mxSTRUCT_CLASS", "mxOBJECT_CLASS", |
| 72 |
"mxCHAR_CLASS", "mxSPARSE_CLASS", "mxDOUBLE_CLASS", |
| 73 |
"mxSINGLE_CLASS", "mxINT8_CLASS", "mxUINT8_CLASS", |
| 74 |
"mxINT16_CLASS", "mxUINT16_CLASS", "mxINT32_CLASS", |
| 75 |
"mxUINT32_CLASS","mxINT64_CLASS","mxUINT64_CLASS", |
| 76 |
}; |
| 77 |
static int printdata = 0; |
| 78 |
static int human_readable = 0; |
| 79 |
static int print_whos_first = 1; |
| 80 |
|
| 81 |
/* Print Functions */ |
| 82 |
static void print_whos(matvar_t *matvar); |
| 83 |
static void print_default(matvar_t *matvar); |
| 84 |
|
| 85 |
static void (*printfunc)(matvar_t *matvar) = NULL; |
| 86 |
|
| 87 |
static void |
| 88 |
print_whos(matvar_t *matvar) |
| 89 |
{ |
| 90 |
int i; |
| 91 |
int nbytes; |
| 92 |
char size[32] = {'\0',}; |
| 93 |
|
| 94 |
if ( print_whos_first ) { |
| 95 |
printf("%-20s %-10s %-10s %-18s\n\n","Name","Size","Bytes","Class"); |
| 96 |
print_whos_first = 0; |
| 97 |
} |
| 98 |
printf("%-20s", matvar->name); |
| 99 |
if ( matvar->rank > 0 ) { |
| 100 |
int cnt = 0; |
| 101 |
printf("%8zu", matvar->dims[0]); |
| 102 |
nbytes = matvar->dims[0]; |
| 103 |
for ( i = 1; i < matvar->rank; i++ ) { |
| 104 |
if ( ceil(log10(matvar->dims[i]))+1 < 32 ) |
| 105 |
cnt += sprintf(size+cnt,"x%zu", matvar->dims[i]); |
| 106 |
nbytes *= matvar->dims[i]; |
| 107 |
} |
| 108 |
printf("%-10s",size); |
| 109 |
nbytes *= Mat_SizeOfClass(matvar->class_type); |
| 110 |
} else { |
| 111 |
printf(" "); |
| 112 |
} |
| 113 |
if ( human_readable ) { |
| 114 |
if ( nbytes > 1073741824L ) |
| 115 |
printf(" %10.1fG",(double)nbytes/1073741824.0); |
| 116 |
else if ( nbytes > 1048576 ) |
| 117 |
printf(" %10.1fM",(double)nbytes/1048576.0); |
| 118 |
else if ( nbytes > 1024 ) |
| 119 |
printf(" %10.1fK",(double)nbytes/1024.0); |
| 120 |
else |
| 121 |
printf(" %10dB",nbytes); |
| 122 |
} else { |
| 123 |
printf(" %10d",nbytes); |
| 124 |
} |
| 125 |
printf(" %-18s\n",mxclass[matvar->class_type-1]); |
| 126 |
|
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
static int indent = 0; |
| 131 |
|
| 132 |
static void |
| 133 |
default_printf_func(int log_level,char *message) |
| 134 |
{ |
| 135 |
int i; |
| 136 |
|
| 137 |
for ( i = 0; i < indent; i++ ) |
| 138 |
printf(" "); |
| 139 |
printf("%s\n",message); |
| 140 |
} |
| 141 |
|
| 142 |
static void |
| 143 |
print_default(matvar_t *matvar) |
| 144 |
{ |
| 145 |
if ( NULL == matvar ) |
| 146 |
return; |
| 147 |
|
| 148 |
switch ( matvar->class_type ) { |
| 149 |
case MAT_C_DOUBLE: |
| 150 |
case MAT_C_SINGLE: |
| 151 |
case MAT_C_INT64: |
| 152 |
case MAT_C_UINT64: |
| 153 |
case MAT_C_INT32: |
| 154 |
case MAT_C_UINT32: |
| 155 |
case MAT_C_INT16: |
| 156 |
case MAT_C_UINT16: |
| 157 |
case MAT_C_INT8: |
| 158 |
case MAT_C_UINT8: |
| 159 |
case MAT_C_CHAR: |
| 160 |
case MAT_C_SPARSE: |
| 161 |
Mat_VarPrint(matvar, printdata); |
| 162 |
break; |
| 163 |
case MAT_C_STRUCT: |
| 164 |
{ |
| 165 |
matvar_t **fields = (matvar_t **)matvar->data; |
| 166 |
int nfields; |
| 167 |
int i; |
| 168 |
size_t nmemb; |
| 169 |
|
| 170 |
if ( matvar->name ) |
| 171 |
Mat_Message(" Name: %s", matvar->name); |
| 172 |
Mat_Message(" Rank: %d", matvar->rank); |
| 173 |
if ( matvar->rank == 0 ) |
| 174 |
return; |
| 175 |
Mat_Message("Class Type: Structure"); |
| 176 |
nfields = Mat_VarGetNumberOfFields(matvar); |
| 177 |
nmemb = matvar->dims[0]; |
| 178 |
for ( i = 1; i < matvar->rank; i++ ) |
| 179 |
nmemb *= matvar->dims[i]; |
| 180 |
if ( nfields > 0 && nmemb < 1 ) { |
| 181 |
char * const *fieldnames = Mat_VarGetStructFieldnames(matvar); |
| 182 |
Mat_Message("Fields[%d] {", nfields); |
| 183 |
indent++; |
| 184 |
for ( i = 0; i < nfields; i++ ) |
| 185 |
Mat_Message(" Name: %s", matvar->name); |
| 186 |
indent--; |
| 187 |
Mat_Message("}"); |
| 188 |
} else if ( nfields > 0 && nmemb > 0 ) { |
| 189 |
Mat_Message("Fields[%d] {", nfields); |
| 190 |
indent++; |
| 191 |
for ( i = 0; i < nfields*nmemb; i++ ) |
| 192 |
print_default(fields[i]); |
| 193 |
indent--; |
| 194 |
Mat_Message("}"); |
| 195 |
} |
| 196 |
break; |
| 197 |
} |
| 198 |
case MAT_C_CELL: |
| 199 |
{ |
| 200 |
matvar_t **cells = (matvar_t **)matvar->data; |
| 201 |
size_t ncells; |
| 202 |
int i; |
| 203 |
|
| 204 |
if ( matvar->name ) |
| 205 |
Mat_Message(" Name: %s", matvar->name); |
| 206 |
Mat_Message(" Rank: %d", matvar->rank); |
| 207 |
if ( matvar->rank == 0 ) |
| 208 |
return; |
| 209 |
ncells = matvar->dims[0]; |
| 210 |
for ( i = 1; i < matvar->rank; i++ ) |
| 211 |
ncells *= matvar->dims[i]; |
| 212 |
Mat_Message("Class Type: Cell Array"); |
| 213 |
Mat_Message("{"); |
| 214 |
indent++; |
| 215 |
for ( i = 0; i < ncells; i++ ) |
| 216 |
print_default(cells[i]); |
| 217 |
indent--; |
| 218 |
Mat_Message("}"); |
| 219 |
break; |
| 220 |
} |
| 221 |
default: |
| 222 |
Mat_Message("Empty"); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
int |
| 227 |
main (int argc, char *argv[]) |
| 228 |
{ |
| 229 |
char *prog_name = "matdump"; |
| 230 |
int i, k, c, err = EXIT_SUCCESS; |
| 231 |
mat_t *mat; |
| 232 |
matvar_t *matvar; |
| 233 |
int version[3]; |
| 234 |
|
| 235 |
Mat_GetLibraryVersion(version, version+1, version+2); |
| 236 |
if ( MATIO_MAJOR_VERSION != version[0] || |
| 237 |
MATIO_MINOR_VERSION != version[1] || |
| 238 |
MATIO_RELEASE_LEVEL != version[2] ) { |
| 239 |
fprintf(stderr,"matio version in header does not match runtime " |
| 240 |
"version\n"); |
| 241 |
return EXIT_FAILURE; |
| 242 |
} |
| 243 |
|
| 244 |
Mat_LogInitFunc(prog_name,default_printf_func); |
| 245 |
|
| 246 |
printfunc = print_default; |
| 247 |
|
| 248 |
while ((c = getopt_long(argc,argv,optstring,options,NULL)) != EOF) { |
| 249 |
switch (c) { |
| 250 |
case 'd': |
| 251 |
printdata = 1; |
| 252 |
Mat_VerbMessage(1,"Printing data\n"); |
| 253 |
break; |
| 254 |
case 'f': |
| 255 |
if ( NULL != optarg && !strcmp(optarg,"whos") ) { |
| 256 |
printfunc = print_whos; |
| 257 |
break; |
| 258 |
} |
| 259 |
Mat_Warning("%s is not a recognized output format. " |
| 260 |
"Using default\n", optarg); |
| 261 |
break; |
| 262 |
case 'h': |
| 263 |
human_readable = 1; |
| 264 |
break; |
| 265 |
case 'v': |
| 266 |
Mat_SetVerbose(1,0); |
| 267 |
break; |
| 268 |
case 'H': |
| 269 |
Mat_Help(helpstr); |
| 270 |
exit(EXIT_SUCCESS); |
| 271 |
case 'V': |
| 272 |
printf("%s %s\nWritten by Christopher Hulbert\n\n" |
| 273 |
"Copyright(C) 2006-2012 Christopher C. Hulbert\n", |
| 274 |
prog_name,PACKAGE_VERSION); |
| 275 |
exit(EXIT_SUCCESS); |
| 276 |
default: |
| 277 |
printf("%c not a valid option\n", c); |
| 278 |
break; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
if ( (argc-optind) < 1 ) |
| 283 |
Mat_Error("Must specify at least one argument"); |
| 284 |
|
| 285 |
mat = Mat_Open( argv[optind],MAT_ACC_RDONLY ); |
| 286 |
if ( NULL == mat ) { |
| 287 |
Mat_Error("Error opening %s\n", argv[optind]); |
| 288 |
return EXIT_FAILURE; |
| 289 |
} |
| 290 |
|
| 291 |
optind++; |
| 292 |
|
| 293 |
if ( optind < argc ) { |
| 294 |
/* variables specified on the command line */ |
| 295 |
if ( printdata ) { |
| 296 |
for ( i = optind; i < argc; i++ ) { |
| 297 |
matvar = Mat_VarRead(mat,argv[i]); |
| 298 |
if ( matvar ) { |
| 299 |
(*printfunc)(matvar); |
| 300 |
Mat_VarFree(matvar); |
| 301 |
matvar = NULL; |
| 302 |
} else { |
| 303 |
Mat_Warning("Couldn't find variable %s in the MAT file", |
| 304 |
argv[i]); |
| 305 |
} |
| 306 |
} |
| 307 |
} else { |
| 308 |
for ( i = optind; i < argc; i++ ) { |
| 309 |
matvar = Mat_VarReadInfo(mat,argv[i]); |
| 310 |
if ( matvar ) { |
| 311 |
(*printfunc)(matvar); |
| 312 |
Mat_VarFree(matvar); |
| 313 |
matvar = NULL; |
| 314 |
} else { |
| 315 |
Mat_Warning("Couldn't find variable %s in the MAT file", |
| 316 |
argv[i]); |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
} else { |
| 321 |
/* print all variables */ |
| 322 |
if ( printdata ) { |
| 323 |
while ( (matvar = Mat_VarReadNext(mat)) != NULL ) { |
| 324 |
(*printfunc)(matvar); |
| 325 |
Mat_VarFree(matvar); |
| 326 |
matvar = NULL; |
| 327 |
} |
| 328 |
} else { |
| 329 |
while ( (matvar = Mat_VarReadNextInfo(mat)) != NULL ) { |
| 330 |
(*printfunc)(matvar); |
| 331 |
Mat_VarFree(matvar); |
| 332 |
matvar = NULL; |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
Mat_Close(mat); |
| 338 |
|
| 339 |
Mat_LogClose(); |
| 340 |
|
| 341 |
return err; |
| 342 |
} |