/*
 * PatMexSample.c
 *
 *  Created on: 2009/02/13
 *      Author: shouno
 */

//
// This sample is a practice for myself
//
// Assumuption:
// MATLAB call this function as
// pat = PatMexSample( filename, index )
//

#include "mex.h"

void mexFunction( int nlhs, mxArray *lhs[], int nrhs, const mxArray *rhs[] );

// Gateway function
void mexFunction( int nlhs, mxArray *lhs[], int nrhs, const mxArray *rhs[] )
{
	int i, len, st;
	char *inbuf, tmp;

	if( nrhs != 1 )
		mexErrMsgTxt( "One input required" );

	if( mxIsChar(rhs[0]) != 1 )
		mexErrMsgTxt( "Input must be a string" );

	if( mxGetM(rhs[0]) != 1 ) // must be a row verctor of char
		mexErrMsgTxt( "Input must be a row vector" );

	len = mxGetN(rhs[0]);
	inbuf = mxCalloc( len+1, sizeof(char) );
	st = mxGetString( rhs[0], inbuf, len+1 );

	// driving procedure should be described below.
	// e.g. reverse the input text;

	for( i = 0 ; i < len/2 ; i++ ){
		tmp = inbuf[i];
		inbuf[i] = inbuf[len-1-i];
		inbuf[len-1-i] = tmp;
	}

	lhs[0] = mxCreateString(inbuf);
	return;
}
