Skip to content

Commit

Permalink
Remove duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
tbeu committed May 28, 2017
1 parent 012d5a6 commit 5a3cae8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 244 deletions.
289 changes: 45 additions & 244 deletions src/mat5.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static mat_complex_split_t null_complex_data = {NULL,NULL};
* -------------------------------------------------------------
*/

/** @brief determines the number of bytes common to struct, cell or array
/** @brief determines the number of bytes for a given class type
*
* @ingroup mat_internal
* @param matvar MAT variable
Expand Down Expand Up @@ -2430,15 +2430,15 @@ WriteCellArrayField(mat_t *mat,matvar_t *matvar )
}

#if defined(HAVE_ZLIB)
/** @brief Writes the header and data for a field of a compressed cell array
/** @brief Writes the header and data for a given class type
*
* @ingroup mat_internal
* @param mat MAT file pointer
* @param matvar pointer to the mat variable
* @return number of bytes written to the MAT file
*/
static size_t
WriteCompressedCellArrayField(mat_t *mat,matvar_t *matvar,z_streamp z)
WriteCompressedType(mat_t *mat,matvar_t *matvar,z_streamp z)
{
mat_uint32_t array_flags = 0x0;
mat_int16_t array_name_type = MAT_T_INT8;
Expand All @@ -2451,25 +2451,6 @@ WriteCompressedCellArrayField(mat_t *mat,matvar_t *matvar,z_streamp z)
int buf_size = 512;
size_t byteswritten = 0;

if ( NULL == matvar || NULL == mat || NULL == z)
return 0;

uncomp_buf[0] = MAT_T_MATRIX;
if ( MAT_C_EMPTY != matvar->class_type || 0 != matvar->nbytes ) {
uncomp_buf[1] = (int)GetCellArrayFieldBufSize(matvar);
} else {
uncomp_buf[1] = 0;
}
z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
z->avail_in = 8;
do {
z->next_out = ZLIB_BYTE_PTR(comp_buf);
z->avail_out = buf_size*sizeof(*comp_buf);
deflate(z,Z_NO_FLUSH);
byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-z->avail_out,
(FILE*)mat->fp);
} while ( z->avail_out == 0 );

if ( MAT_C_EMPTY == matvar->class_type ) {
/* exit early if this is an empty data */
return byteswritten;
Expand Down Expand Up @@ -2636,9 +2617,9 @@ WriteCompressedCellArrayField(mat_t *mat,matvar_t *matvar,z_streamp z)
buf_size*sizeof(*comp_buf)-z->avail_out,(FILE*)mat->fp);
} while ( z->avail_out == 0 );
for ( i = 0; i < nfields; i++ ) {
size_t len = strlen(matvar->internal->fieldnames[i]);
memset(padzero,'\0',fieldname_size);
memcpy(padzero,matvar->internal->fieldnames[i],
strlen(matvar->internal->fieldnames[i]));
memcpy(padzero,matvar->internal->fieldnames[i],len);
z->next_in = ZLIB_BYTE_PTR(padzero);
z->avail_in = fieldname_size;
do {
Expand Down Expand Up @@ -2681,6 +2662,43 @@ WriteCompressedCellArrayField(mat_t *mat,matvar_t *matvar,z_streamp z)
case MAT_C_OPAQUE:
break;
}
}

/** @brief Writes the header and data for a field of a compressed cell array
*
* @ingroup mat_internal
* @param mat MAT file pointer
* @param matvar pointer to the mat variable
* @return number of bytes written to the MAT file
*/
static size_t
WriteCompressedCellArrayField(mat_t *mat,matvar_t *matvar,z_streamp z)
{
mat_uint32_t comp_buf[512];
mat_uint32_t uncomp_buf[512] = {0,};
int buf_size = 512;
size_t byteswritten = 0;

if ( NULL == matvar || NULL == mat || NULL == z)
return 0;

uncomp_buf[0] = MAT_T_MATRIX;
if ( MAT_C_EMPTY != matvar->class_type ) {
uncomp_buf[1] = (int)GetCellArrayFieldBufSize(matvar);
} else {
uncomp_buf[1] = 0;
}
z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
z->avail_in = 8;
do {
z->next_out = ZLIB_BYTE_PTR(comp_buf);
z->avail_out = buf_size*sizeof(*comp_buf);
deflate(z,Z_NO_FLUSH);
byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-z->avail_out,
(FILE*)mat->fp);
} while ( z->avail_out == 0 );

byteswritten += WriteCompressedType(mat,matvar,z);
return byteswritten;
}
#endif
Expand Down Expand Up @@ -2916,19 +2934,13 @@ WriteStructField(mat_t *mat,matvar_t *matvar)
static size_t
WriteCompressedStructField(mat_t *mat,matvar_t *matvar,z_streamp z)
{
mat_uint32_t array_flags = 0x0;
mat_int16_t array_name_type = MAT_T_INT8;
int array_flags_type = MAT_T_UINT32, dims_array_type = MAT_T_INT32;
int array_flags_size = 8;
int nBytes, i, nmemb = 1, nzmax = 0;

mat_uint32_t comp_buf[512];
mat_uint32_t uncomp_buf[512] = {0,};
int buf_size = 512;
size_t byteswritten = 0;

if ( NULL == mat || NULL == z)
return 1;
return 0;

if ( NULL == matvar ) {
size_t dims[2] = {0,0};
Expand All @@ -2951,219 +2963,8 @@ WriteCompressedStructField(mat_t *mat,matvar_t *matvar,z_streamp z)
byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-z->avail_out,
(FILE*)mat->fp);
} while ( z->avail_out == 0 );

if ( MAT_C_EMPTY == matvar->class_type ) {
/* exit early if this is an empty data */
return byteswritten;
}

/* Array Flags */
array_flags = matvar->class_type & CLASS_TYPE_MASK;
if ( matvar->isComplex )
array_flags |= MAT_F_COMPLEX;
if ( matvar->isGlobal )
array_flags |= MAT_F_GLOBAL;
if ( matvar->isLogical )
array_flags |= MAT_F_LOGICAL;
if ( matvar->class_type == MAT_C_SPARSE )
nzmax = ((mat_sparse_t *)matvar->data)->nzmax;
uncomp_buf[0] = array_flags_type;
uncomp_buf[1] = array_flags_size;
uncomp_buf[2] = array_flags;
uncomp_buf[3] = nzmax;
/* Rank and Dimension */
nBytes = matvar->rank * 4;
uncomp_buf[4] = dims_array_type;
uncomp_buf[5] = nBytes;
for ( i = 0; i < matvar->rank; i++ ) {
mat_int32_t dim;
dim = matvar->dims[i];
nmemb *= dim;
uncomp_buf[6+i] = dim;
}
if ( matvar->rank % 2 != 0 ) {
int pad4 = 0;
uncomp_buf[6+i] = pad4;
i++;
}

z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
z->avail_in = (6+i)*sizeof(*uncomp_buf);
do {
z->next_out = ZLIB_BYTE_PTR(comp_buf);
z->avail_out = buf_size*sizeof(*comp_buf);
deflate(z,Z_NO_FLUSH);
byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-z->avail_out,
(FILE*)mat->fp);
} while ( z->avail_out == 0 );
/* Name of variable */
uncomp_buf[0] = array_name_type;
uncomp_buf[1] = 0;
z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
z->avail_in = 8;
do {
z->next_out = ZLIB_BYTE_PTR(comp_buf);
z->avail_out = buf_size*sizeof(*comp_buf);
deflate(z,Z_NO_FLUSH);
byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-z->avail_out,
(FILE*)mat->fp);
} while ( z->avail_out == 0 );

matvar->internal->datapos = ftell((FILE*)mat->fp);
if ( matvar->internal->datapos == -1L ) {
Mat_Critical("Couldn't determine file position");
}
switch ( matvar->class_type ) {
case MAT_C_DOUBLE:
case MAT_C_SINGLE:
case MAT_C_INT64:
case MAT_C_UINT64:
case MAT_C_INT32:
case MAT_C_UINT32:
case MAT_C_INT16:
case MAT_C_UINT16:
case MAT_C_INT8:
case MAT_C_UINT8:
{
/* WriteCompressedData makes sure uncompressed data is aligned
* on an 8-byte boundary */
if ( matvar->isComplex ) {
mat_complex_split_t *complex_data = (mat_complex_split_t*)matvar->data;

if ( NULL == matvar->data )
complex_data = &null_complex_data;

byteswritten += WriteCompressedData(mat,z,
complex_data->Re,nmemb,matvar->data_type);
byteswritten += WriteCompressedData(mat,z,
complex_data->Im,nmemb,matvar->data_type);
} else {
byteswritten += WriteCompressedData(mat,z,
matvar->data,nmemb,matvar->data_type);
}
break;
}
case MAT_C_CHAR:
{
byteswritten += WriteCompressedCharData(mat,z,matvar->data,
nmemb,matvar->data_type);
break;
}
case MAT_C_CELL:
{
int ncells;
matvar_t **cells = (matvar_t **)matvar->data;

/* Check for an empty cell array */
if ( matvar->nbytes == 0 || matvar->data_size == 0 ||
matvar->data == NULL )
break;
ncells = matvar->nbytes / matvar->data_size;
for ( i = 0; i < ncells; i++ )
WriteCompressedCellArrayField(mat,cells[i],z);
break;
}
case MAT_C_STRUCT:
{
mat_int16_t fieldname_type = MAT_T_INT32;
mat_int16_t fieldname_data_size = 4;
unsigned char *padzero;
int fieldname_size, nfields;
size_t maxlen = 0;
mat_int32_t array_name_type = MAT_T_INT8;
matvar_t **fields = (matvar_t **)matvar->data;

nfields = matvar->internal->num_fields;
/* Check for a structure with no fields */
if ( nfields < 1 ) {
fieldname_size = 1;
uncomp_buf[0] = (fieldname_data_size << 16) | fieldname_type;
uncomp_buf[1] = fieldname_size;
uncomp_buf[2] = array_name_type;
uncomp_buf[3] = 0;
z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
z->avail_in = 16;
do {
z->next_out = ZLIB_BYTE_PTR(comp_buf);
z->avail_out = buf_size*sizeof(*comp_buf);
deflate(z,Z_NO_FLUSH);
byteswritten += fwrite(comp_buf,1,buf_size*
sizeof(*comp_buf)-z->avail_out,(FILE*)mat->fp);
} while ( z->avail_out == 0 );
break;
}

for ( i = 0; i < nfields; i++ ) {
size_t len = strlen(matvar->internal->fieldnames[i]);
if ( len > maxlen )
maxlen = len;
}
maxlen++;
fieldname_size = maxlen;
while ( nfields*fieldname_size % 8 != 0 )
fieldname_size++;
uncomp_buf[0] = (fieldname_data_size << 16) | fieldname_type;
uncomp_buf[1] = fieldname_size;
uncomp_buf[2] = array_name_type;
uncomp_buf[3] = nfields*fieldname_size;

padzero = (unsigned char*)calloc(fieldname_size,1);
z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
z->avail_in = 16;
do {
z->next_out = ZLIB_BYTE_PTR(comp_buf);
z->avail_out = buf_size*sizeof(*comp_buf);
deflate(z,Z_NO_FLUSH);
byteswritten += fwrite(comp_buf,1,
buf_size*sizeof(*comp_buf)-z->avail_out,(FILE*)mat->fp);
} while ( z->avail_out == 0 );
for ( i = 0; i < nfields; i++ ) {
size_t len = strlen(matvar->internal->fieldnames[i]);
memset(padzero,'\0',fieldname_size);
memcpy(padzero,matvar->internal->fieldnames[i],len);
z->next_in = ZLIB_BYTE_PTR(padzero);
z->avail_in = fieldname_size;
do {
z->next_out = ZLIB_BYTE_PTR(comp_buf);
z->avail_out = buf_size*sizeof(*comp_buf);
deflate(z,Z_NO_FLUSH);
byteswritten += fwrite(comp_buf,1,
buf_size*sizeof(*comp_buf)-z->avail_out,(FILE*)mat->fp);
} while ( z->avail_out == 0 );
}
free(padzero);
for ( i = 0; i < nmemb*nfields; i++ )
byteswritten +=
WriteCompressedStructField(mat,fields[i],z);
break;
}
case MAT_C_SPARSE:
{
mat_sparse_t *sparse = (mat_sparse_t*)matvar->data;

byteswritten += WriteCompressedData(mat,z,sparse->ir,
sparse->nir,MAT_T_INT32);
byteswritten += WriteCompressedData(mat,z,sparse->jc,
sparse->njc,MAT_T_INT32);
if ( matvar->isComplex ) {
mat_complex_split_t *complex_data = (mat_complex_split_t*)sparse->data;
byteswritten += WriteCompressedData(mat,z,
complex_data->Re,sparse->ndata,matvar->data_type);
byteswritten += WriteCompressedData(mat,z,
complex_data->Im,sparse->ndata,matvar->data_type);
} else {
byteswritten += WriteCompressedData(mat,z,
sparse->data,sparse->ndata,matvar->data_type);
}
break;
}
case MAT_C_FUNCTION:
case MAT_C_OBJECT:
case MAT_C_EMPTY:
case MAT_C_OPAQUE:
break;
}


byteswritten += WriteCompressedType(mat,matvar,z);
return byteswritten;
}
#endif
Expand Down
2 changes: 2 additions & 0 deletions src/mat5.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
# define EXTERN extern
#endif

static size_t GetTypeBufSize(matvar_t *matvar);
static size_t GetStructFieldBufSize(matvar_t *matvar);
static size_t GetCellArrayFieldBufSize(matvar_t *matvar);
static size_t GetMatrixMaxBufSize(matvar_t *matvar);
Expand All @@ -56,6 +57,7 @@ static size_t WriteCompressedEmptyData(mat_t *mat,z_streamp z,int N,
enum matio_types data_type);
static size_t WriteCompressedData(mat_t *mat,z_streamp z,void *data,int N,
enum matio_types data_type);
static size_t WriteCompressedType(mat_t *mat,matvar_t *matvar,z_streamp z);
static size_t WriteCompressedCellArrayField(mat_t *mat,matvar_t *matvar,
z_streamp z);
static size_t WriteCompressedStructField(mat_t *mat,matvar_t *matvar,
Expand Down

0 comments on commit 5a3cae8

Please sign in to comment.