Commit 358640ba authored by Houtan Bastani's avatar Houtan Bastani
Browse files

Mex files / modifications for mex

parent 601c087b
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <dynmex.h>
#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<nargs; n++)
free(args[n]);
free(args);
}
#include "mex.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdarg.h>
#include <string.h>
#if defined(MATLAB_MEX_FILE) || defined(OCTAVE_MEX_FILE)
#include <dynmex.h>
#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
......
......@@ -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 <dynmex.h>
/* //#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
Supports Markdown
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