Commit 14a081c0 authored by Houtan Bastani's avatar Houtan Bastani
Browse files

add swz_fprintf_err to handle printing to stderr

parent c955ae62
#include <stdlib.h>
#include "mex.h"
#include <stdlib.h>
#include <stdarg.h>
#include <stdarg.h>
#include <string.h>
void
swz_fprintf_err(FILE *cad, const char * str, ...)
{
char *whole_str=(char*)NULL;
char *msg_truncated = ".....MSG TRUNCATED\n";
int num_args = 0;
va_list ap;
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);
#else
vfprintf(stderr, str, ap);
#endif
va_end(ap);
}
void
swz_exit(int status)
{
......
#ifndef _MEXMOD
#define _MEXMOD
void swz_exit(int status);
void swz_fprintf_err(const char * str, ...);
#endif
#if defined(MATLAB_MEX_FILE) || defined(OCTAVE_MEX_FILE)
......
......@@ -122,13 +122,13 @@ TMarkovStateVariable* CreateMarkovStateVariable_Single(int nstates, int nobs, TM
if (!CheckRestrictions(FreeDim,NonZeroIndex,MQ,nstates))
{
fprintf(stderr,"CreateMarkovStateVariable_Single(): Error in restrictions\n");
swz_fprintf_err("CreateMarkovStateVariable_Single(): Error in restrictions\n");
exit(0);
}
if (!CheckPrior(Prior,nstates) || !CheckPriorOnFreeParameters(Prior,NonZeroIndex,nstates))
{
fprintf(stderr,"CreateMarkovStateVariable_Single(): Error in priors\n");
swz_fprintf_err("CreateMarkovStateVariable_Single(): Error in priors\n");
exit(0);
}
......@@ -137,13 +137,13 @@ TMarkovStateVariable* CreateMarkovStateVariable_Single(int nstates, int nobs, TM
if ((nstates <= 0) || (nobs <= 0))
{
fprintf(stderr,"CreateMarkovStateVariable(): improper argument values\n");
swz_fprintf_err("CreateMarkovStateVariable(): improper argument values\n");
exit(0);
}
if (!(sv=(TMarkovStateVariable*)malloc(sizeof(TMarkovStateVariable))))
{
fprintf(stderr,"CreateMarkovStateVariable(): out of memory\n");
swz_fprintf_err("CreateMarkovStateVariable(): out of memory\n");
exit(0);
}
......@@ -696,7 +696,7 @@ TMarkovStateVariable* CreateMarkovStateVariable_Lags(int nlags, TMarkovStateVari
if (base->n_state_variables > 1)
{
fprintf(stderr,"CreateMarkovStateVariable_Lags(): multiple state variable for base.");
swz_fprintf_err("CreateMarkovStateVariable_Lags(): multiple state variable for base.");
exit(0);
}
......@@ -801,13 +801,13 @@ void Swap_SV(TMarkovStateVariable *sv, int i, int j)
TPermutation X;
if (sv->n_state_variables > 1)
{
fprintf(stderr,"Swap(): Can only swap indices for terminal state variables.\n");
swz_fprintf_err("Swap(): Can only swap indices for terminal state variables.\n");
exit(0);
}
if ((i < 0) || (j < 0) || (i >= sv->nbasestates) || (j >= sv->nbasestates))
{
fprintf(stderr,"Swap(): Indicies out of range.\n");
swz_fprintf_err("Swap(): Indicies out of range.\n");
exit(0);
}
......@@ -829,7 +829,7 @@ void Swap_SV(TMarkovStateVariable *sv, int i, int j)
FreePermutation(X);
if (!Update_B_from_Q_SV(sv))
{
fprintf(stderr,"Swap(): Restrictions violated.\n");
swz_fprintf_err("Swap(): Restrictions violated.\n");
exit(0);
}
PropagateSwap_SV(sv);
......@@ -907,7 +907,7 @@ void DrawTransitionMatrix_SV(TMarkovStateVariable *sv)
for (k=dw_DimA(sv->FreeDim)-1; k >= 0; k--)
if (!DrawDirichletVector(sv->b[k],sv->b[k]))
{
fprintf(stderr,"Error drawing Dirichlet vector\n");
swz_fprintf_err("Error drawing Dirichlet vector\n");
exit(0);
}
......@@ -950,7 +950,7 @@ void DrawTransitionMatrixFromPrior_SV(TMarkovStateVariable *sv)
for (j=dw_DimA(sv->FreeDim)-1; j >= 0; j--)
if (!DrawDirichletVector(sv->b[j],sv->b[j]))
{
fprintf(stderr,"Error drawing Dirichlet vector\n");
swz_fprintf_err("Error drawing Dirichlet vector\n");
exit(0);
}
......@@ -1417,7 +1417,7 @@ TStateModel* CreateStateModel_new(TMarkovStateVariable *sv, ThetaRoutines *routi
if (!(model=(TStateModel*)malloc(sizeof(TStateModel))))
{
fprintf(stderr,"CreateStateModel(): Out of memory\n");
swz_fprintf_err("CreateStateModel(): Out of memory\n");
exit(0);
}
......@@ -2136,7 +2136,7 @@ void ConvertQToFreeParameters(TStateModel *model, PRECISION *f)
TVector* ba=model->sv->ba;
if (!(model->sv->valid_transition_matrix))
{
fprintf(stderr,"ConvertQToFreeParameters(): Transition matrices not valid.\n");
swz_fprintf_err("ConvertQToFreeParameters(): Transition matrices not valid.\n");
exit(0);
}
for (i=0; i < dw_DimA(ba); f+=k, i++)
......@@ -2210,7 +2210,7 @@ void ConvertQToLogFreeParameters(TStateModel *model, PRECISION *f)
if (!(model->sv->valid_transition_matrix))
{
fprintf(stderr,"ConvertQToFreeParameters(): Transition matrices not valid.\n");
swz_fprintf_err("ConvertQToFreeParameters(): Transition matrices not valid.\n");
exit(0);
}
......
......@@ -1030,13 +1030,13 @@ TMarkovStateVariable* CreateMarkovStateVariable_File(FILE *f, char *filename, in
id="//== Number Observations ==//";
if (!dw_SetFilePosition(f_in,id))
{
fprintf(stderr,"Line identifier ""%s"" not found.\n",id);
swz_fprintf_err("Line identifier ""%s"" not found.\n",id);
exit(0);
}
fscanf(f_in," %d ",&nobs);
if (nobs <= 0)
{
fprintf(stderr,"Number Observations must be positive\n");
swz_fprintf_err("Number Observations must be positive\n");
exit(0);
}
}
......@@ -1044,13 +1044,13 @@ TMarkovStateVariable* CreateMarkovStateVariable_File(FILE *f, char *filename, in
id="//== Number Independent State Variables ==//";
if (!dw_SetFilePosition(f_in,id))
{
fprintf(stderr,"Line identifier ""%s"" not found.\n",id);
swz_fprintf_err("Line identifier ""%s"" not found.\n",id);
exit(0);
}
fscanf(f_in," %d ",&n_state_variables);
if (n_state_variables <= 0)
{
fprintf(stderr,"Number Independent State Variables must be positive\n");
swz_fprintf_err("Number Independent State Variables must be positive\n");
exit(0);
}
......@@ -1060,20 +1060,20 @@ TMarkovStateVariable* CreateMarkovStateVariable_File(FILE *f, char *filename, in
sprintf(id_buffer,"//== Number of states for state_variable[%d] ==//",i+1);
if (!dw_SetFilePosition(f_in,id_buffer))
{
fprintf(stderr,"Line identifier ""%s"" not found.\n",id_buffer);
swz_fprintf_err("Line identifier ""%s"" not found.\n",id_buffer);
exit(0);
}
fscanf(f_in," %d ",&nstates);
if (nstates <= 0)
{
fprintf(stderr,"Number of states for state_variable[%d] must be positive\n",i+1);
swz_fprintf_err("Number of states for state_variable[%d] must be positive\n",i+1);
exit(0);
}
sprintf(id_buffer,"//== Transition matrix prior for state_variable[%d]. (n_states x n_states) ==//",i+1);
if (!dw_SetFilePosition(f_in,id_buffer))
{
fprintf(stderr,"Line identifier ""%s"" not found.\n",id_buffer);
swz_fprintf_err("Line identifier ""%s"" not found.\n",id_buffer);
exit(0);
}
dw_ReadMatrix(f_in,prior=CreateMatrix(nstates,nstates));
......@@ -1088,7 +1088,7 @@ TMarkovStateVariable* CreateMarkovStateVariable_File(FILE *f, char *filename, in
sprintf(id_buffer,"//== Column restrictions for state_variable[%d] ==//",i+1);
if (!dw_SetFilePosition(f_in,id_buffer))
{
fprintf(stderr,"Line identifier ""%s"" not found.\n",id_buffer);
swz_fprintf_err("Line identifier ""%s"" not found.\n",id_buffer);
exit(0);
}
restrictions=dw_CreateArray_matrix(nstates);
......@@ -1097,7 +1097,7 @@ TMarkovStateVariable* CreateMarkovStateVariable_File(FILE *f, char *filename, in
dw_ReadMatrix(f_in,restrictions[j]=CreateMatrix(nstates,dims[j]));
else
{
fprintf(stderr,"Free Dirichet dimensions for column %d of state_variable[%d] must be positive\n",j+1,i+1);
swz_fprintf_err("Free Dirichet dimensions for column %d of state_variable[%d] must be positive\n",j+1,i+1);
exit(0);
}
......
......@@ -69,7 +69,7 @@ int main(int nargs, char **args)
printf("Reading data...\n");
if (!(model=CreateTStateModelFromEstimateFinal(nargs,args,&cmd)))
{
fprintf(stderr,"Unable to read model or parameters\n");
swz_fprintf_err("Unable to read model or parameters\n");
exit(1);
}
p=(T_VAR_Parameters*)(model->theta);
......
......@@ -186,14 +186,14 @@ T_VAR_Parameters* CreateTheta_VAR(int flag, int nvars, int nlags, int nexg, int
if ((nvars <= 0) || (nlags < 0) || (nexg < 0))
{
fprintf(stderr,"CreateTheta_VAR(): Invalid arguments passed.\n");
swz_fprintf_err("CreateTheta_VAR(): Invalid arguments passed.\n");
exit(0);
}
//=== Allocate memory for T_VAR_Parameters ===
if (!(p=(T_VAR_Parameters*)malloc(sizeof(T_VAR_Parameters))))
{
fprintf(stderr,"Out of memory\n");
swz_fprintf_err("Out of memory\n");
exit(0);
}
......@@ -2259,7 +2259,7 @@ void Update_bplus_from_lambda_psi(T_VAR_Parameters *p)
PRECISION *p_bplus, *p_lambda, *p_psi;
if (!(p->Specification & SPEC_SIMS_ZHA))
{
fprintf(stderr,"Update_bplus_from_lambda_psi() called without Sims-Zha specification\n");
swz_fprintf_err("Update_bplus_from_lambda_psi() called without Sims-Zha specification\n");
exit(0);
}
for (j=p->nvars-1; j >= 0; j--)
......
......@@ -311,7 +311,7 @@ TStateModel* CreateStateModel_VAR_matlab(char *filename)
for (j=nvars-1; j >= 0; j--)
if (IV[j] != npre)
{
fprintf(stderr,"V[%d] not %d x %d\n",j,npre,npre);
swz_fprintf_err("V[%d] not %d x %d\n",j,npre,npre);
exit(0);
}
V=dw_CreateArray_matrix(nvars);
......@@ -362,10 +362,10 @@ TStateModel* CreateStateModel_VAR_matlab(char *filename)
}
break;
case 4:
fprintf(stderr,"Case %d not implimented.\n",4);
swz_fprintf_err("Case %d not implimented.\n",4);
exit(0);
default:
fprintf(stderr,"Unknown type.\n");
swz_fprintf_err("Unknown type.\n");
exit(0);
}
dw_FreeArray(IM);
......
......@@ -26,7 +26,7 @@ int main(int nargs, char **args)
if (nargs != 4)
{
fprintf(stderr,"Syntax:\n create_init_file <matlab filename> <specs filename> <file tag>\n");
swz_fprintf_err("Syntax:\n create_init_file <matlab filename> <specs filename> <file tag>\n");
exit(0);
}
......
......@@ -427,7 +427,7 @@ TStateModel* GetModelFromCommandLine(int nargs, char **args, TEstimateInfo *esti
free(filename);
//if (d2) free(d2);
//fprintf(stderr,"GetModelFromCommandLine(): Unable to create model.\n");
//swz_fprintf_err("GetModelFromCommandLine(): Unable to create model.\n");
goto ERROR;
}
......@@ -452,7 +452,7 @@ TStateModel* GetModelFromCommandLine(int nargs, char **args, TEstimateInfo *esti
ERROR:
if (d2) free(d2);
//fprintf(stderr,"GetModelFromCommandLine(): No specification file defined.\n");
//swz_fprintf_err("GetModelFromCommandLine(): No specification file defined.\n");
return (TStateModel*)NULL;
}
......
......@@ -271,7 +271,7 @@ T_MHM* CreateMHM_CommandLine(int nargs, char **args)
sprintf(spec_filename=(char*)malloc(strlen(d_in) + strlen(fmt) + strlen(tag) - 3),fmt,d_in,tag);
if (!(f_in=fopen(spec_filename,"rt")))
{
fprintf(stderr,"CreateMHM_CommandLine: Unable to create model from %s tag.\n",tag);
swz_fprintf_err("CreateMHM_CommandLine: Unable to create model from %s tag.\n",tag);
if (mhm) FreeMHM(mhm);
}
else
......@@ -307,14 +307,14 @@ T_MHM* CreateMHM_CommandLine(int nargs, char **args)
}
else
{
fprintf(stderr,"CreateMHM_CommandLine(): No specification file given.\n");
swz_fprintf_err("CreateMHM_CommandLine(): No specification file given.\n");
if (mhm) FreeMHM(mhm);
exit(0);
}
if (!mhm)
{
fprintf(stderr,"CreateMHM_CommandLine: No mhm input data file specified.\n");
swz_fprintf_err("CreateMHM_CommandLine: No mhm input data file specified.\n");
exit(0);
}
......
......@@ -1595,7 +1595,7 @@ void ComputeMarginal_TruncatedGaussianProposal(int ndraws_proposal, TMatrix X, T
/* case TYPE_TABLE: */
/* return SetupSphericalFromPosterior_Table(X,n); */
/* default: */
/* fprintf(stderr,"Unknown proposal type\n"); */
/* swz_fprintf_err("Unknown proposal type\n"); */
/* exit(0); */
/* } */
/* } */
......@@ -1621,7 +1621,7 @@ void ComputeMarginal_TruncatedGaussianProposal(int ndraws_proposal, TMatrix X, T
/* sprintf(filename,"table_md_%s.dat",tag); */
/* break; */
/* default: */
/* fprintf(stderr,"Unknown proposal type\n"); */
/* swz_fprintf_err("Unknown proposal type\n"); */
/* exit(0); */
/* } */
/* f_out=dw_CreateTextFile(filename); */
......
......@@ -88,14 +88,14 @@ int main(int nargs, char **args)
if (!spec)
{
fprintf(stderr,"No specification filename given\n");
fprintf(stderr,"Command line syntax:\n"
" -ft : file tag\n"
" -fs : specification filename\n"
" -fo : output filename (probablities.dat)\n"
" -fp : parameters filename (specification filename)\n"
" -fh : parameter header (Posterior mode: )\n"
);
swz_fprintf_err("No specification filename given\n");
swz_fprintf_err("Command line syntax:\n"
" -ft : file tag\n"
" -fs : specification filename\n"
" -fo : output filename (probablities.dat)\n"
" -fp : parameters filename (specification filename)\n"
" -fh : parameter header (Posterior mode: )\n"
);
exit(1);
}
......
......@@ -163,7 +163,7 @@ PRECISION DrawSpherical(TVector x)
while ((r < SPHERICAL_LOWER_TRUNCATE) || (SPHERICAL_UPPER_TRUNCATE < r));
return r;
default:
fprintf(stderr,"Unknown spherical type\n");
swz_fprintf_err("Unknown spherical type\n");
exit(0);
}
}
......@@ -186,7 +186,7 @@ PRECISION LogSphericalDensity(PRECISION r)
case SPHERICAL_TRUNCATED_GAUSSIAN:
return ((r < SPHERICAL_LOWER_TRUNCATE) || (r > SPHERICAL_UPPER_TRUNCATE)) ? MINUS_INFINITY : -0.5*r*r + SPHERICAL_CONSTANT;
default:
fprintf(stderr,"Unknown spherical type\n");
swz_fprintf_err("Unknown spherical type\n");
exit(0);
}
}
......
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