MS-SBVAR MEX: various simplifications and modernizations

parent 72dfbbdc
......@@ -17,9 +17,9 @@
* along with Dynare. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <dynmex.h>
#include "modify_for_mex.h"
......@@ -31,13 +31,7 @@ mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
int nargs = 0;
int n = 0;
int maxnargs = 0;
const char *mainarg = "./a.out";
char *argument = NULL;
char *beginarg = NULL;
char **args = NULL;
/*
* Check args
......@@ -48,10 +42,10 @@ mexFunction(int nlhs, mxArray *plhs[],
/*
* Allocate memory
*/
maxnargs = static_cast<int>(mxGetN(prhs[0])/2+1);
argument = static_cast<char *>(mxCalloc(mxGetN(prhs[0])+1, sizeof(char)));
args = static_cast<char **>(mxCalloc(maxnargs, sizeof(char *)));
if (argument == NULL || args == NULL)
int maxnargs = static_cast<int>(mxGetN(prhs[0])/2+1);
char *argument = static_cast<char *>(mxCalloc(mxGetN(prhs[0])+1, sizeof(char)));
char **args = static_cast<char **>(mxCalloc(maxnargs, sizeof(char *)));
if (!argument || !args)
mexErrMsgTxt("Error in MS-SBVAR MEX file: could not allocate memory. (1)");
/*
......@@ -60,13 +54,13 @@ mexFunction(int nlhs, mxArray *plhs[],
if (!(args[nargs] = static_cast<char *>(mxCalloc(strlen(mainarg)+1, sizeof(char)))))
mexErrMsgTxt("Error in MS-SBVAR MEX file: could not allocate memory. (2)");
strncpy(args[nargs++], mainarg, strlen(mainarg));
strcpy(args[nargs++], mainarg);
if (mxGetString(prhs[0], argument, mxGetN(prhs[0])+1))
mexErrMsgTxt("Error in MS-SBVAR MEX file: error using mxGetString.\n");
beginarg = &argument[0];
while ((n = strcspn(beginarg, " ")))
char *beginarg = argument;
while (int n = strcspn(beginarg, " "))
{
if (!(args[nargs] = static_cast<char *>(mxCalloc(n+1, sizeof(char)))))
mexErrMsgTxt("Error in MS-SBVAR MEX file: could not allocate memory. (3)");
......@@ -91,7 +85,7 @@ mexFunction(int nlhs, mxArray *plhs[],
/*
* free memory
*/
for (n = 0; n < nargs; n++)
for (int n = 0; n < nargs; n++)
mxFree(args[n]);
mxFree(args);
}
/*
* Copyright © 2011-2017 Dynare Team
*
* This file is part of Dynare.
*
* Dynare is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Dynare is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dynare. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include "modify_for_mex.h"
void
mex_write_to_matlab_matfile(double *data, int rows, int cols, const char *varname, const char *filename)
{
mxArray *toWrite;
toWrite = getMxArray(data, rows, cols);
MATFile *matfile = matOpen(filename, "w");
if (matfile == NULL)
mexErrMsgTxt("Error encountered in mex when opening a .mat file");
if (matPutVariable(matfile, varname, toWrite) != 0
|| ferror(matGetFp(matfile)) > 0
|| feof(matGetFp(matfile)) > 0)
mexErrMsgTxt("Error encountered in mex when writing a .mat file");
if (matClose(matfile) != 0)
mexErrMsgTxt("Error encountered in mex when closing a .mat file");
}
void
mex_write_to_matlab_global_struct(double *data, int rows, int cols, const char *varname)
{
mxArray *toWrite;
toWrite = getMxArray(data, rows, cols);
int fieldnumber = mxAddField(globalMatlabStruct, varname);
if (fieldnumber < 0)
mexErrMsgTxt("Error encountered in mex when assigining to global structure");
mxSetFieldByNumber(globalMatlabStruct, 0, fieldnumber, toWrite);
}
mxArray *
getMxArray(double *data, int rows, int cols)
{
mxArray *mat;
double *pind;
int i;
if (rows == 1 && cols == 1)
mat = mxCreateDoubleScalar(data[0]);
else
{
mat = mxCreateDoubleMatrix(rows, cols, mxREAL);
if (mat != NULL)
{
pind = mxGetPr(mat);
for (i = 0; i < rows*cols; i++)
pind[i] = data[i];
}
}
if (mat == NULL)
mexErrMsgTxt("Error encountered in mex: not enough heap space to allocate memory for mxArray");
return mat;
}
/*
* Copyright © 2010-2017 Dynare Team
* Copyright © 2010-2020 Dynare Team
*
* This file is part of Dynare.
*
......@@ -42,11 +42,5 @@ extern bool utIsInterruptPending();
void msExit(int status);
extern int constant_seed;
/* Write Matlab Output
mxArray *globalMatlabStruct;*/
void mex_write_to_matlab_matfile(double *, int, int, const char *, const char *);
void mex_write_to_matlab_global_struct(double *, int, int, const char *);
mxArray *getMxArray(double *, int, int);
#endif
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment