Initial import
git-svn-id: http://moon:8086/svn/software/trunk/libsrc/mlio@1 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
// -----------------------------------------------------------------------------------------
|
||||
// mlio.c
|
||||
// Input/output for Matlab matrices
|
||||
// -----------------------------------------------------------------------------------------
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "mlio.h"
|
||||
|
||||
// -----------------------------------------------------------------------------------------
|
||||
int mlio_open(mlio_t *pObj, char *pName, unsigned mode)
|
||||
{
|
||||
|
||||
pObj->io_mode = mode;
|
||||
|
||||
if (mode == MLIO_READ)
|
||||
{
|
||||
pObj->pFile = fopen(pName, "rb");
|
||||
|
||||
// Read input file
|
||||
if (!pObj->pFile)
|
||||
{
|
||||
fprintf(stderr,"Unable to open input file %s\n", pName);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Header
|
||||
fread(&pObj->hdr_file.version, 1, sizeof(unsigned), pObj->pFile);
|
||||
fscanf(pObj->pFile, "%s", pObj->hdr_file.prec_typename);
|
||||
fseek(pObj->pFile, 1, SEEK_CUR);
|
||||
fread(&pObj->hdr_data, 1, sizeof(data_hdr_t), pObj->pFile);
|
||||
printf("Rows=%d, Columns=%d, File size=%d bytes\n", pObj->hdr_data.num_row, pObj->hdr_data.num_col, pObj->hdr_data.size);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
pObj->pFile = fopen(pName, "wb");
|
||||
|
||||
// Write output file
|
||||
if (!pObj->pFile)
|
||||
{
|
||||
fprintf(stderr,"Unable to open output file %s\n", pName);
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
int mlio_close(mlio_t *pObj)
|
||||
{
|
||||
return fclose(pObj->pFile);
|
||||
}
|
||||
|
||||
int mlio_read(mlio_t *pObj, double *pData)
|
||||
{
|
||||
// Read data
|
||||
fread(pData, pObj->hdr_data.num_row*pObj->hdr_data.num_col, sizeof(double), pObj->pFile);
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
int mlio_write(mlio_t *pObj, double *pData, unsigned num_rows, unsigned num_cols)
|
||||
{
|
||||
|
||||
pObj->hdr_data.num_row = num_rows;
|
||||
pObj->hdr_data.num_col = num_cols;
|
||||
|
||||
// Header
|
||||
fwrite(&pObj->hdr_file.version, 1, sizeof(unsigned), pObj->pFile);
|
||||
fprintf(pObj->pFile, "%s\r", pObj->hdr_file.prec_typename);
|
||||
fwrite(&pObj->hdr_data, 1, sizeof(data_hdr_t), pObj->pFile);
|
||||
|
||||
// Write data
|
||||
fwrite(pData, pObj->hdr_data.num_row*pObj->hdr_data.num_col, sizeof(double), pObj->pFile);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------------------
|
||||
@@ -0,0 +1,46 @@
|
||||
// -----------------------------------------------------------------------------------------
|
||||
// mlio.h
|
||||
// Input/output for Matlab matrices
|
||||
// 26.02.2005, J. Ahrensfeld
|
||||
//
|
||||
// -----------------------------------------------------------------------------------------
|
||||
#ifndef MLIO_H
|
||||
#define MLIO_H
|
||||
|
||||
// -----------------------------------------------------------------------------------------
|
||||
#define MLIO_READ 1
|
||||
#define MLIO_WRITE 2
|
||||
|
||||
// -----------------------------------------------------------------------------------------
|
||||
typedef struct _data_hdr_t
|
||||
{
|
||||
unsigned num_row, num_col;
|
||||
unsigned size;
|
||||
|
||||
} data_hdr_t;
|
||||
|
||||
typedef struct _file_hdr_t
|
||||
{
|
||||
unsigned version;
|
||||
char prec_typename[32];
|
||||
|
||||
} file_hdr_t;
|
||||
|
||||
typedef struct _mlio_t
|
||||
{
|
||||
FILE *pFile;
|
||||
unsigned io_mode;
|
||||
|
||||
file_hdr_t hdr_file;
|
||||
data_hdr_t hdr_data;
|
||||
} mlio_t;
|
||||
|
||||
// -----------------------------------------------------------------------------------------
|
||||
int mlio_open(mlio_t *pObj, char *pName, unsigned mode);
|
||||
int mlio_close(mlio_t *pObj);
|
||||
int mlio_read(mlio_t *pObj, double *pData);
|
||||
int mlio_write(mlio_t *pObj, double *pData, unsigned num_rows, unsigned num_cols);
|
||||
|
||||
// -----------------------------------------------------------------------------------------
|
||||
#endif //MLIO_H
|
||||
|
||||
Reference in New Issue
Block a user