Skip to content

Commit

Permalink
- Made the matlab reader/writer more portable (int32_t instead of int)
Browse files Browse the repository at this point in the history
- Use calloc+free instead of new+memset(0)+delete[]


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@8611 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Apr 13, 2011
1 parent 5a23323 commit 099cdda
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
11 changes: 5 additions & 6 deletions c_runtime/read_matlab4.c
Expand Up @@ -87,13 +87,12 @@ const char* omc_new_matlab4_reader(const char *filename, ModelicaMatReader *read
char tmp[size_omc_mat_Aclass];
if (fread(tmp,size_omc_mat_Aclass-1,1,reader->file) != 1) return "Corrupt header: Aclass matrix";
tmp[size_omc_mat_Aclass-1] = '\0';
// binTrans
/* binTrans */
if (0 == strncmp(tmp,omc_mat_Aclass,size_omc_mat_Aclass)) {
/* fprintf(stderr, "use binTrans format\n"); */
binTrans = 1;
}
// binNormal
else if (0 == strncmp(tmp,dymola_mat_Aclass,size_omc_mat_Aclass)) {
} else if (0 == strncmp(tmp,dymola_mat_Aclass,size_omc_mat_Aclass)) {
/* binNormal */
/* fprintf(stderr, "use binNormal format\n"); */
binTrans = 0;
}
Expand Down Expand Up @@ -144,8 +143,8 @@ const char* omc_new_matlab4_reader(const char *filename, ModelicaMatReader *read
}
case 3: { /* "dataInfo" */
unsigned int i;
int *tmp = (int*) malloc(sizeof(int)*hdr.ncols*hdr.mrows);
if (1 != fread(tmp,sizeof(int)*hdr.ncols*hdr.mrows,1,reader->file)) {
int32_t *tmp = (int32_t*) malloc(sizeof(int32_t)*hdr.ncols*hdr.mrows);
if (1 != fread(tmp,sizeof(int32_t)*hdr.ncols*hdr.mrows,1,reader->file)) {
free(tmp); tmp=NULL;
return "Corrupt header: dataInfo matrix";
}
Expand Down
26 changes: 13 additions & 13 deletions c_runtime/simulation_result_mat.cpp
Expand Up @@ -130,8 +130,9 @@ simulation_result_mat::simulation_result_mat(const char* filename,

char *stringMatrix = NULL;
int rows, cols;
int *intMatrix = NULL;
int32_t *intMatrix = NULL;
double *doubleMatrix = NULL;
assert(sizeof(char) == 1);
rt_tick(SIM_TIMER_OUTPUT);
numVars = calcDataSize(indx_map);
names = calcDataNames(numVars+nParams,indx_parammap);
Expand All @@ -142,24 +143,24 @@ simulation_result_mat::simulation_result_mat(const char* filename,
if (!fp) throw SimulationResultFileOpenException();

// write `AClass' matrix
writeMatVer4Matrix("Aclass", 4, 11, Aclass, sizeof(char));
writeMatVer4Matrix("Aclass", 4, 11, Aclass, sizeof(int8_t));

// flatten variables' names
flattenStrBuf(numVars+nParams, names, stringMatrix, rows, cols, false /* We cannot plot derivatives if we fix the names ... */, false);
// write `name' matrix
writeMatVer4Matrix("name", rows, cols, stringMatrix, sizeof(char));
delete[] stringMatrix; stringMatrix = NULL;
writeMatVer4Matrix("name", rows, cols, stringMatrix, sizeof(int8_t));
free(stringMatrix); stringMatrix = NULL;

// flatten variables' comments
flattenStrBuf(numVars+nParams, names, stringMatrix, rows, cols, false, true);
// write `description' matrix
writeMatVer4Matrix("description", rows, cols, stringMatrix, sizeof(char));
delete[] stringMatrix; stringMatrix = NULL;
writeMatVer4Matrix("description", rows, cols, stringMatrix, sizeof(int8_t));
free(stringMatrix); stringMatrix = NULL;

// generate dataInfo table
generateDataInfo(intMatrix, rows, cols, globalData, numVars, nParams,indx_map,indx_parammap);
// write `dataInfo' matrix
writeMatVer4Matrix("dataInfo", cols, rows, intMatrix, sizeof(int));
writeMatVer4Matrix("dataInfo", cols, rows, intMatrix, sizeof(int32_t));
delete[] doubleMatrix; doubleMatrix = NULL;

// generate `data_1' matrix (with parameter data)
Expand Down Expand Up @@ -259,7 +260,7 @@ long simulation_result_mat::flattenStrBuf(int dims,
char* &dest, int& longest, int& nstrings,
bool fixNames, bool useComment)
{
int i,j,len;
int i,len;
nstrings = dims;
longest = 0; // the longest-string length

Expand All @@ -270,8 +271,7 @@ long simulation_result_mat::flattenStrBuf(int dims,
}

// allocate memory
dest = new char[longest*nstrings+1];
memset(dest,0,(longest*nstrings+1)*sizeof(char));
dest = (char*) calloc(longest*nstrings+1, sizeof(char));
if (!dest) throw SimulationResultMallocException();
// copy data
char *ptr = dest;
Expand Down Expand Up @@ -306,9 +306,9 @@ void simulation_result_mat::writeMatVer4MatrixHeader(const char *name,
MHeader_t hdr;

int type = 0;
if (size == sizeof(char))
if (size == 1 /* char */)
type = 51;
if (size == sizeof(int))
if (size == 4 /* int32 */)
type = 20;

// create matrix header structure
Expand Down Expand Up @@ -336,7 +336,7 @@ void simulation_result_mat::writeMatVer4Matrix(const char *name,
}


void simulation_result_mat::generateDataInfo(int* &dataInfo,
void simulation_result_mat::generateDataInfo(int32_t* &dataInfo,
int& rows, int& cols,
const sim_DATA *mdl_data,
int nVars, int nParams, map<void*,int> &indx_map, map<void*,int> &indx_parammap)
Expand Down

0 comments on commit 099cdda

Please sign in to comment.