Complete module example
Below follows the complete code of the static module meminit and the main function that declares this module and executes the Mosel model which requires the module.
#include <stdio.h> #include <stdlib.h> #include "xprm_mc.h" #include "xprm_ni.h" static int meminit_init(XPRMnifct nifct, int *interver, int *libver, XPRMdsointer **interf); /* Main function */ int main() { XPRMmodel mod; int result; char params[80]; static int tabinit[]= {23,78,45,90,234,111,900,68,110}; XPRMinit(); /* Initialize Mosel */ /* Register `meminit' as a static module (=stored in the program) */ XPRMregstatdso("meminit", meminit_init); XPRMcompmod("", "meminit_test.mos", NULL, NULL); /* Compile the model */ mod=XPRMloadmod("meminit_test.bim", NULL); /* Load the model */ /* Parameters: the address of the data table and its size */ sprintf(params, "MEMDAT='%p', MEMSIZ=%d", tabinit, sizeof(tabinit)/sizeof(int)); XPRMrunmod(mod, &result, params); /* Run the model */ } / ******************** **** Body of the module 'meminit' ******************** ****/ static int mi_meminit(XPRMcontext ctx, void *libctx); /* List of subroutines */ static XPRMdsofct tabfct[]= { {"meminit", 1000, XPRM_TYP_NOT, 3, "AI.isi", mi_meminit} }; /* Main interface structure */ static XPRMdsointer dsointer= { 0, NULL, sizeof(tabfct)/sizeof(XPRMdsofct), tabfct, 0, NULL, 0, NULL }; static XPRMnifct mm; /* To store the mosel function table */ /* Initialization function of the module */ static int meminit_init(XPRMnifct nifct, int *interver, int *libver, XPRMdsointer **interf) { mm=nifct; /* Save the table of functions */ *interver=XPRM_NIVERS; /* The interface version we are using */ *libver=XPRM_MKVER(0,0,1); /* The version of the module: 0.0.1 */ *interf=&dsointer; /* Our interface */ return 0; } /* Implementation of procedure `meminit' */ static int mi_meminit(XPRMcontext ctx, void *libctx) { XPRMarray arr; XPRMstring adr_s; XPRMset ndxset; int *adr,siz,index[1],last,i; arr=XPRM_POP_REF(ctx); /* The array */ adr_s=XPRM_POP_STRING(ctx); /* Data location (as a string) */ siz=XPRM_POP_INT(ctx); /* Data size */ sscanf(adr_s,"%p",&adr); /* Get the address from the string */ mm->getarrsets(arr,&ndxset); index[0]=mm->getfirstsetndx(ndxset); last=mm->getlastsetndx(ndxset); for(i=0;(i<siz) && (index[0]<=last);i++,index[0]++) mm->setarrvalint(ctx,arr,index,adr[i]); return XPRM_RT_OK; }