/** @file mat5.c
* Matlab MAT version 5 file functions
* @ingroup MAT
*/
/*
* Copyright (c) 2005-2021, Christopher C. Hulbert
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* FIXME: Implement Unicode support */
#include "matio_private.h"
#include "mat5.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#if defined(_MSC_VER) || defined(__MINGW32__)
# define strdup _strdup
#endif
/** Get type from tag */
#define TYPE_FROM_TAG(a) ( ((a) & 0x000000ff) <= MAT_T_FUNCTION ) ? (enum matio_types)((a) & 0x000000ff) : MAT_T_UNKNOWN
/** Get class from array flag */
#define CLASS_FROM_ARRAY_FLAGS(a) ( ((a) & 0x000000ff) <= MAT_C_OPAQUE ) ? ((enum matio_classes)((a) & 0x000000ff)) : MAT_C_EMPTY
/** Class type mask */
#define CLASS_TYPE_MASK 0x000000ff
static mat_complex_split_t null_complex_data = {NULL,NULL};
/*===========================================================================
* Private functions
*===========================================================================
*/
static int GetTypeBufSize(matvar_t *matvar, size_t *size);
static int GetStructFieldBufSize(matvar_t *matvar, size_t *size);
static int GetCellArrayFieldBufSize(matvar_t *matvar, size_t *size);
static void SetFieldNames(matvar_t *matvar, char *buf, size_t nfields,
mat_uint32_t fieldname_length);
static size_t ReadSparse(mat_t *mat, matvar_t *matvar, mat_uint32_t *n, mat_uint32_t **v);
#if HAVE_ZLIB
static int GetMatrixMaxBufSize(matvar_t *matvar, size_t *size);
#endif
static int GetEmptyMatrixMaxBufSize(const char *name, int rank, size_t *size);
static size_t WriteCharData(mat_t *mat, void *data, int N,enum matio_types data_type);
static size_t ReadNextCell( mat_t *mat, matvar_t *matvar );
static size_t ReadNextStructField( mat_t *mat, matvar_t *matvar );
static size_t ReadNextFunctionHandle(mat_t *mat, matvar_t *matvar);
static int ReadRankDims(mat_t *mat, matvar_t *matvar, enum matio_types data_type,
mat_uint32_t nbytes, size_t *read_bytes);
static int WriteType(mat_t *mat,matvar_t *matvar);
static int WriteCellArrayField(mat_t *mat,matvar_t *matvar );
static int WriteStructField(mat_t *mat,matvar_t *matvar);
static int WriteData(mat_t *mat,void *data,size_t N,enum matio_types data_type);
static size_t Mat_WriteEmptyVariable5(mat_t *mat,const char *name,int rank,
size_t *dims);
static int Mat_VarReadNumeric5(mat_t *mat,matvar_t *matvar,void *data,size_t N);
#if HAVE_ZLIB
static size_t WriteCompressedCharData(mat_t *mat,z_streamp z,void *data,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 WriteCompressedTypeArrayFlags(mat_t *mat,matvar_t *matvar,
z_streamp z);
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,
z_streamp z);
static size_t Mat_WriteCompressedEmptyVariable5(mat_t *mat,const char *name,
int rank,size_t *dims,z_streamp z);
#endif
/** @brief determines the number of bytes for a given class type
*
* @ingroup mat_internal
* @param matvar MAT variable
* @param size the number of bytes needed to store the MAT variable
* @return 0 on success
*/
static int
GetTypeBufSize(matvar_t *matvar, size_t *size)
{
int err;
size_t nBytes, data_bytes;
size_t tag_size = 8;
size_t nelems = 1;
size_t rank_size;
*size = 0;
err = Mat_MulDims(matvar, &nelems);
if ( err )
return err;
/* Add rank and dimensions, padded to an 8 byte block */
err = Mul(&rank_size, matvar->rank, 4);
if ( err )
return err;
if ( matvar->rank % 2 )
nBytes = tag_size + 4;
else
nBytes = tag_size;
err = Add(&nBytes, nBytes, rank_size);
if ( err )
return err;
switch ( matvar->class_type ) {
case MAT_C_STRUCT:
{
matvar_t **fields = (matvar_t**)matvar->data;
size_t nfields = matvar->internal->num_fields;
size_t maxlen = 0, i, field_buf_size;
for ( i = 0; i < nfields; i++ ) {
char *fieldname = matvar->internal->fieldnames[i];
if ( NULL != fieldname && strlen(fieldname) > maxlen )
maxlen = strlen(fieldname);
}
maxlen++;
while ( nfields*maxlen % 8 != 0 )
maxlen++;
err = Mul(&field_buf_size, maxlen, nfields);
if ( err )
return err;
err = Add(&nBytes, nBytes, tag_size + tag_size);
if ( err )
return err;
err = Add(&nBytes, nBytes, field_buf_size);
if ( err )
return err;
/* FIXME: Add bytes for the fieldnames */
if ( NULL != fields && nfields > 0 ) {
size_t nelems_x_nfields = 1;
err = Mul(&nelems_x_nfields, nelems, nfields);
if ( err )
return err;
for ( i = 0; i < nelems_x_nfields; i++ ) {
err = GetStructFieldBufSize(fields[i], &field_buf_size);
if ( err )
return err;
err = Add(&nBytes, nBytes, tag_size);
if ( err )
return err;
err = Add(&nBytes, nBytes, field_buf_size);
if ( err )
return err;
}
}
break;
}
case MAT_C_CELL:
{
matvar_t **cells = (matvar_t**)matvar->data;
if ( matvar->nbytes == 0 || matvar->data_size == 0 )
break;
nelems = matvar->nbytes / matvar->data_size;
if ( NULL != cells && nelems > 0 ) {
size_t i, field_buf_size;
for ( i = 0; i < nelems; i++ ) {
err = GetCellArrayFieldBufSize(cells[i], &field_buf_size);
if ( err )
return err;
err = Add(&nBytes, nBytes, tag_size);
if ( err )
return err;
err = Add(&nBytes, nBytes, field_buf_size);
if ( err )
return err;
}
}
break;
}
case MAT_C_SPARSE:
{
没有合适的资源?快使用搜索试试~ 我知道了~
Winodows MSCV 2015 64位编译好的libmatio库,拿来即用,不需要重新编译源码
共37个文件
c:12个
h:10个
f90:5个
需积分: 5 2 下载量 149 浏览量
2024-05-20
16:09:25
上传
评论
收藏 1.72MB ZIP 举报
温馨提示
libmatio是一个开源的MAT文件I/O库,用于读写MATLAB格式的数据文件。MAT文件是MATLAB程序中常用的数据格式之一,通常包含矩阵、向量、结构体等多种类型的数据。libmatio提供了一种方便的方法来读取和写入MAT文件,使得它成为许多科学和工程应用程序中必不可少的工具之一。 使用libmatio,您可以轻松地从MAT文件中读取数据,并将其存储在C语言中的数组或其他数据结构中。您还可以将C语言中的数据写入MAT文件,以便在MATLAB程序中使用。 libmatio是一个MATLAB文件I/O库,可以用来读取和写入MATLAB格式的文件,包括MATLAB数据文件和MATLAB V6格式文件。使用libmatio,您可以将MATLAB文件中的数据读取到您的C/C++程序中,也可以将您的C/C++程序中的数据写入MATLAB文件。它还支持不同类型的数据,如数字、字符、逻辑、结构、单元、稀疏矩阵、函数句柄等。除此之外,libmatio还支持MATLAB文件的压缩和解压缩。
资源推荐
资源详情
资源评论
收起资源包目录
libmatio_x64.zip (37个子文件)
libmatio_x64
include
mat4.h 2KB
matvar_struct.c 18KB
matio.h 15KB
fortran
write_data.f90 31KB
matio_t.inc.in 4KB
read_data.f90 33KB
matio_internal.c 14KB
create.f90 24KB
write.f90 19KB
Makefile.am 2KB
matio.f90 16KB
endian.c 5KB
safe-math.h 47KB
mat5.c 199KB
mat.c 93KB
mat73.c 112KB
matvar_cell.c 6KB
io.c 14KB
mat5.h 2KB
.Makefile.fortran.am.swp 12KB
snprintf.c 47KB
matio.sym 1021B
matio_pubconf.h.in 5KB
matio_private.h 10KB
matioConfig.h 9KB
mat73.h 2KB
stdint_msvc.h 8KB
matio_pubconf.h 4KB
read_data_impl.h 18KB
inflate.c 12KB
mat4.c 42KB
Makefile.am 3KB
read_data.c 40KB
matioConfig.h.in 7KB
lib
libmatio.lib 14KB
dll
libmatio.dll 920KB
hdf5.dll 3.26MB
共 37 条
- 1
资源评论
zw_Loneranger
- 粉丝: 4910
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功