tbeu/matio

Not really an issue: Add a function Mat_GetFileMode to the library.

Closed this issue · 6 comments

Please add this simple function, because struct's elements are private to the library.

-> to mat.c; and its header to mat.h
int Mat_GetFileMode(mat_t *mat)
{
int rval=-1;
if ( NULL != mat )
rval = mat->mode;

return rval;
}

Another comment:
could you modify mat.c/L.481 to:
mat = Mat_CreateVer(matname, NULL, (enum mat_ft)((MAT_FT_DEFAULT|mode) & 0xfffffffe));
so that Mat_Open(filename,mode) does not fail if the file does not exist, and user requested RDWR access to it.

Along the same lines, can the file mode flags be expanded to:

  1. append mode (if file does not exist, create it; start writing at the end of it)
  2. write mode (if file exist, delete its previous content and start new one).
    see https://stackoverflow.com/questions/21113919/difference-between-r-and-w-in-fopen: flag "r+" will not create a file for RDWR if it does not exist.
  3. read mode (if file does not exist, report an error - already done with MAT_ACC_RDONLY)

write mode is needed for repeated execution of a script to create the same mat file with certain data in it.

tbeu commented

Sure, can do. Feel free to send in a PR if you like.

tbeu commented

Pardon my ignorance, but what is PR?

Never mind, PR is for pull request to propose changes.

I have a question for my rlab interface to matio: I crate mat file and put some variables in it, then close it. Can I append to that file and add more content, or not? Consider rlab example: matopen(fn=“myfile.mat”,”rw”); matwritem(fn,x=rand(3,8)); matclose(fn); If I repeat code above with another variable, say y, then only the last data will be in the file.

See #60.

tbeu commented

Please add this simple function, because struct's elements are private to the library.

-> to mat.c; and its header to mat.h int Mat_GetFileMode(mat_t *mat) { int rval=-1; if ( NULL != mat ) rval = mat->mode;

return rval; }

Thinking once again I'd rather not expose the file open mode and keep it an internal flag. Even worse, it is used inconsistently within the library and seems to be broken:

  1. Mat_Create4 sets mode to MAT_ACC_RDONLY, whereas Mat_Create5 and Mat_Create73 set it to MAT_ACC_RDWR.
  2. Mat_Close does not reset the mode. Having MAT_ACC_RDONLY set to zero as default seems unfortunate.
  3. Mat_VarDelete does not respect the mode, i.e. it is possible to delete a variable from a file that is opened as MAT_ACC_RDONLY. Mat_VarDelete seems to be the only location where mode is utilized at all, but only when copying the mode flag from the existing file to the new file.

Another comment:
could you modify mat.c/L.481 to:
mat = Mat_CreateVer(matname, NULL, (enum mat_ft)((MAT_FT_DEFAULT|mode) & 0xfffffffe));
so that Mat_Open(filename,mode) does not fail if the file does not exist, and user requested RDWR access to it.

Mat_Open is documented to only work on existing files. I'd rather keep it as is.

tbeu commented

I added Mat_GetFileAccessMode to the public API and fixed the above mentioned issued by ad9cc50.