dynare/mex/sources/ms-sbvar/mex_top_level.cc

98 lines
2.7 KiB
C++
Raw Normal View History

/*
* Copyright © 2010-2020 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/>.
*/
2010-05-19 15:55:47 +02:00
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <dynmex.h>
#include "modify_for_mex.h"
int main(int nargs, char **args);
void
mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
int nargs = 0;
int n = 0;
int maxnargs = 0;
2010-09-17 19:08:58 +02:00
const char *mainarg = "./a.out";
2010-05-19 15:55:47 +02:00
char *argument = NULL;
char *beginarg = NULL;
char **args = NULL;
/*
* Check args
*/
if (nrhs != 1 || !mxIsChar(prhs[0]) || nlhs != 0)
mexErrMsgTxt("Error in MS-SBVAR MEX file: this function takes 1 string input argument and returns no output argument.");
2010-05-19 15:55:47 +02:00
/*
* Allocate memory
*/
2019-12-20 14:50:19 +01:00
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 *)));
2017-05-16 16:30:27 +02:00
if (argument == NULL || args == NULL)
mexErrMsgTxt("Error in MS-SBVAR MEX file: could not allocate memory. (1)");
2010-05-19 15:55:47 +02:00
/*
* Create argument string from prhs and parse to create args / nargs
*/
2019-12-20 14:50:19 +01:00
if (!(args[nargs] = static_cast<char *>(mxCalloc(strlen(mainarg)+1, sizeof(char)))))
mexErrMsgTxt("Error in MS-SBVAR MEX file: could not allocate memory. (2)");
2010-09-17 19:08:58 +02:00
2010-05-19 15:55:47 +02:00
strncpy(args[nargs++], mainarg, strlen(mainarg));
if (mxGetString(prhs[0], argument, mxGetN(prhs[0])+1))
mexErrMsgTxt("Error in MS-SBVAR MEX file: error using mxGetString.\n");
2010-05-19 15:55:47 +02:00
beginarg = &argument[0];
2017-05-16 16:30:27 +02:00
while ((n = strcspn(beginarg, " ")))
2010-05-19 15:55:47 +02:00
{
2019-12-20 14:50:19 +01:00
if (!(args[nargs] = static_cast<char *>(mxCalloc(n+1, sizeof(char)))))
mexErrMsgTxt("Error in MS-SBVAR MEX file: could not allocate memory. (3)");
2010-09-17 19:08:58 +02:00
2010-05-19 15:55:47 +02:00
strncpy(args[nargs++], beginarg, n);
beginarg += (isspace(beginarg[n]) || isblank(beginarg[n]) ? ++n : n);
}
mxFree(argument);
2010-05-19 15:55:47 +02:00
/*
* Call top_level function (formerly main)
*/
2010-09-17 19:08:58 +02:00
try
{
main(nargs, args);
}
catch (const char *str)
{
mexErrMsgTxt(str);
2010-09-17 19:08:58 +02:00
}
2010-05-19 15:55:47 +02:00
/*
* free memory
*/
2017-05-16 16:30:27 +02:00
for (n = 0; n < nargs; n++)
mxFree(args[n]);
mxFree(args);
2010-05-19 15:55:47 +02:00
}