From ae3f150c99cb7a32fc4f394ce740ded64d8e816f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Mon, 10 Feb 2020 16:25:03 +0100
Subject: [PATCH] MS-SBVAR MEX: various simplifications and modernizations
---
mex/sources/ms-sbvar/mex_top_level.cc | 28 ++++----
mex/sources/ms-sbvar/mex_write_to_matlab.c | 77 ----------------------
mex/sources/ms-sbvar/modify_for_mex.h | 8 +--
3 files changed, 12 insertions(+), 101 deletions(-)
delete mode 100644 mex/sources/ms-sbvar/mex_write_to_matlab.c
diff --git a/mex/sources/ms-sbvar/mex_top_level.cc b/mex/sources/ms-sbvar/mex_top_level.cc
index cee4610d12..5022d9501a 100644
--- a/mex/sources/ms-sbvar/mex_top_level.cc
+++ b/mex/sources/ms-sbvar/mex_top_level.cc
@@ -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);
}
diff --git a/mex/sources/ms-sbvar/mex_write_to_matlab.c b/mex/sources/ms-sbvar/mex_write_to_matlab.c
deleted file mode 100644
index 84af6c6a54..0000000000
--- a/mex/sources/ms-sbvar/mex_write_to_matlab.c
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
- * 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;
-}
diff --git a/mex/sources/ms-sbvar/modify_for_mex.h b/mex/sources/ms-sbvar/modify_for_mex.h
index 19ad4f112f..351692e0d2 100644
--- a/mex/sources/ms-sbvar/modify_for_mex.h
+++ b/mex/sources/ms-sbvar/modify_for_mex.h
@@ -1,5 +1,5 @@
/*
- * 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
--
GitLab