Initializing help system before first use

Redirecting the Mosel output

When integrating a Mosel model into an application it may be desirable to be able to redirect any output produced by the model to the application. This can be done by the means of a callback function. This function takes a predefined signature as shown in the following C program. If it is called from outside of the execution of any Mosel model, its parameter model will be NULL. In our example the callback function prefixes the printout of every line of Mosel output with Mosel:.

#include <stdio.h>
#include "xprm_mc.h"

/**** Callback function to handle output ****/
long XPRM_RTC cbmsg(XPRMmodel model, void *info, char *buf, unsigned long size)
{
 printf("Mosel: %.*s", (int)size, buf);
 return 0;
}

int main()
{
 int result;
 char outfile_name[40];           /* File name of output stream */

 if(XPRMinit())                   /* Initialize Mosel */
  return 1;
                                  /* Prepare file name for output stream */
                                  /* using 'cb' driver                   */
 sprintf(outfile_name, "cb:%p", cbmsg);

                                  /* Set default output stream to callback */
 XPRMsetdefstream(NULL, XPRM_F_WRITE, outfile_name);

                                  /* Execute = compile/load/run a model */
 if(XPRMexecmod(NULL, "burglar2.mos", NULL, &result, NULL))
  return 2;

 return 0;
} 

The same procedure that has been presented here for redirecting the Mosel output can also be applied to redirect any error messages produced by Mosel—the only required modification consists in replacing the constant XPRM_F_WRITE by XPRM_F_ERROR in the argument of function XPRMsetdefstream.