From 358640ba4f5a36a139cc06af107fde302097ade0 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 19 May 2010 15:55:47 +0200 Subject: [PATCH] Mex files / modifications for mex --- matlab/swz/c-code/mex/mex_top_level.c | 73 ++++++++++++++++++++++++++ matlab/swz/c-code/mex/modify_for_mex.c | 53 ++++++++++--------- matlab/swz/c-code/mex/modify_for_mex.h | 22 ++++++++ 3 files changed, 122 insertions(+), 26 deletions(-) create mode 100644 matlab/swz/c-code/mex/mex_top_level.c diff --git a/matlab/swz/c-code/mex/mex_top_level.c b/matlab/swz/c-code/mex/mex_top_level.c new file mode 100644 index 000000000..f070b0691 --- /dev/null +++ b/matlab/swz/c-code/mex/mex_top_level.c @@ -0,0 +1,73 @@ +#include +#include +#include +#include + +#include "modify_for_mex.h" + +int main(int nargs, char **args); + +/* MATLAB interface */ +void +mexFunction(int nlhs, mxArray *plhs[], + int nrhs, const mxArray *prhs[]) +{ + int nargs = 0; + int n = 0; + int maxnargs = 0; + + char *mainarg = "./a.out"; + char *argument = NULL; + char *beginarg = NULL; + char **args = NULL; + + /* + * Check args + */ + if (nrhs != 1 || !mxIsChar(prhs[0])) + mexErrMsgTxt("This function takes only one string argument."); + + if (nlhs != 0) + mexWarnMsgTxt("This function has no return arguments.\n"); + + /* + * Allocate memory + */ + maxnargs = (int)(mxGetN(prhs[0])/2+1); + argument = (char *)calloc(mxGetN(prhs[0])+1, sizeof(char)); + args = (char **)calloc(maxnargs, sizeof(char *)); + if (argument==NULL || args==NULL) + mexErrMsgTxt("In swz_mex: could not allocate memory. (1)"); + + /* + * Create argument string from prhs and parse to create args / nargs + */ + if (!(args[nargs] = (char *)calloc(strlen(mainarg)+1, sizeof(char)))) + mexErrMsgTxt("In swz_mex: could not allocate memory. (2)"); + strncpy(args[nargs++], mainarg, strlen(mainarg)); + + if (mxGetString(prhs[0], argument, mxGetN(prhs[0])+1)) + mexErrMsgTxt("In swz_mex: error using mxGetString.\n"); + + beginarg = &argument[0]; + while(n=strcspn(beginarg, " ")) + { + if (!(args[nargs] = (char *)calloc(n+1, sizeof(char)))) + mexErrMsgTxt("In swz_mex: could not allocate memory. (3)"); + strncpy(args[nargs++], beginarg, n); + beginarg += (isspace(beginarg[n]) || isblank(beginarg[n]) ? ++n : n); + } + free(argument); + + /* + * Call top_level function (formerly main) + */ + main(nargs, args); + + /* + * free memory + */ + for (n=0; n #include #include #include #include +#if defined(MATLAB_MEX_FILE) || defined(OCTAVE_MEX_FILE) +#include +#endif + +int +swz_fprintf_stdout(char *msg, ...) +{ + int ret; + va_list ap; + va_start(ap, msg); + +#if defined(MATLAB_MEX_FILE) + ret = mexPrintf(msg, ap); +#elif defined(OCTAVE_MEX_FILE) + mexPrintf(msg, ap); + ret = 1; +#else + ret = vprintf(msg, ap); +#endif + + va_end(ap); + return ret; +} + void -swz_fprintf_err(FILE *cad, const char * str, ...) +swz_fprintf_err(char *str, ...) { char *whole_str=(char*)NULL; char *msg_truncated = ".....MSG TRUNCATED\n"; @@ -16,30 +40,7 @@ swz_fprintf_err(FILE *cad, const char * str, ...) va_start(ap, str); #if defined(MATLAB_MEX_FILE) || defined(OCTAVE_MEX_FILE) - if (!(whole_str = (char *)malloc(sizeof(str)*sizeof(char)))) - { - printf("Could not allocate memory\n"); - exit(0); - } - - strcpy(whole_str, str); - while (strtok_r(whole_str, "%", &whole_str) != NULL) - num_args++; - - num_args = sizeof(str)*sizeof(char) + num_args*sizeof(long double); - if (!(whole_str = (char *)realloc(whole_str, num_args + strlen(msg_truncated) + 1))) - { - printf("Could not allocate memory\n"); - exit(0); - } - - vsnprintf(whole_str, num_args, str, ap); - if(strlen(whole_str) + 1 == num_args) - strcat(whole_str, msg_truncated); - - printf("%s", whole_str); - - free(whole_str); + mexPrintf(str, ap); #else vfprintf(stderr, str, ap); #endif diff --git a/matlab/swz/c-code/mex/modify_for_mex.h b/matlab/swz/c-code/mex/modify_for_mex.h index 7f7b77154..1d6aa83bb 100644 --- a/matlab/swz/c-code/mex/modify_for_mex.h +++ b/matlab/swz/c-code/mex/modify_for_mex.h @@ -2,9 +2,31 @@ #define _MEXMOD void swz_exit(int status); void swz_fprintf_err(const char * str, ...); +int swz_fprintf_stdout(char *msg, ...); #endif + #if defined(MATLAB_MEX_FILE) || defined(OCTAVE_MEX_FILE) +#include "matrix.h" +#include + +/* //#undef printf ansi-c*/ +/* //#define printf swz_printf ansi-c*/ +/*#define fflush(stdout) mexEvalString("drawnow;");*/ + +#undef printf +#undef exit + #define printf mexPrintf #define exit swz_exit + +#undef malloc +#undef calloc +#undef realloc +#undef free + +#define malloc mxMalloc +#define calloc mxCalloc +#define realloc mxRealloc +#define free mxFree #endif