ADMB Documentation  11.4.2890
 All Classes Files Functions Variables Typedefs Friends Defines
model33.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00007 #include <admodel.h>
00008 
00009 void ad_make_code_reentrant(void)
00010 {
00011   initial_params::num_initial_params=0;
00012   initial_params::mc_phase=0;
00013   initial_params::mceval_phase=0;
00014   initial_params::sd_phase=0;
00015   likeprof_params::num_likeprof_params=0;
00016   stddev_params::num_stddev_params=0;
00017   if (ad_comm::global_datafile)
00018   {
00019     delete ad_comm::global_datafile;
00020     ad_comm::global_datafile=NULL;
00021   }
00022 
00023   if (ad_comm::global_parfile)
00024   {
00025     delete ad_comm::global_parfile;
00026     ad_comm::global_parfile=NULL;
00027   }
00028 
00029   if (ad_comm::global_bparfile)
00030   {
00031     delete ad_comm::global_bparfile;
00032     ad_comm::global_bparfile=NULL;
00033   }
00034 }
00035 
00036 void parse_dll_options(char *pname, const int& _nopt, char *sp_options,
00037   char *** pargv)
00038 {
00039   int& nopt=(int&) _nopt;
00040   nopt=1;
00041   char ** a=*pargv;
00042   char * p=NULL;
00043   if (strlen(sp_options))
00044   {
00045     size_t string_size = (strlen(sp_options)+5)*sizeof(char);
00046     //int string_size= (strlen(sp_options)+1)*sizeof(char);
00047     p = (char *) malloc(string_size);
00048     char * ptmp=NULL;
00049     strcpy(p,sp_options);
00050     ptmp = strtok(p," ");
00051     if (ptmp) nopt++;
00052     //ssbul_l(ptmp);
00053     do
00054     {
00055       ptmp = strtok(NULL, " ");
00056       if (ptmp) nopt++;
00057     }
00058     while(ptmp);
00059 
00060     //a=(char **)malloc((nopt+5)*sizeof(char *));
00061     //for (int k=0;k<=nopt;k++) a[k]=NULL;
00062     nopt=1;
00063     strcpy(p,sp_options);
00064     ptmp = strtok(p," ");
00065     if (ptmp)
00066     {
00067       //a[nopt]=(char *)malloc((strlen(ptmp)+5)*sizeof(char));
00068       strcpy(a[nopt++],ptmp);
00069     }
00070 
00071     do
00072     {
00073       ptmp = strtok(NULL, " ");
00074       if (ptmp)
00075       {
00076         //a[nopt]=(char *)malloc((strlen(ptmp)+5)*sizeof(char));
00077         strcpy(a[nopt++],ptmp);
00078       }
00079     }
00080     while(ptmp);
00081 
00082     if (p)
00083     {
00084       free(p);
00085       p=NULL;
00086     }
00087   }
00088 /*
00089   else
00090   {
00091     //a=(char **)malloc((nopt+5)*sizeof(char *));
00092     //a[nopt]=NULL;
00093   }
00094 */
00095   //a[0]=(char *)malloc((strlen(pname)+10)*sizeof(char));
00096   strcpy(a[0],pname);
00097 #ifdef _WIN32
00098   strcat(a[0],".exe");
00099 #endif
00100   //return a;
00101 }
00102 
00103 char** no_dll_options(char *pname, const int& _nopt)
00104 {
00105   int& nopt=(int&) _nopt;
00106   nopt=1;
00107   char** a = (char**)malloc((nopt+1)*sizeof(char*));
00108   if (a)
00109   {
00110     a[nopt] = NULL;
00111     a[0] = (char*)malloc((strlen(pname)+5)*sizeof(char));
00112     if (a[0])
00113     {
00114       strcpy(a[0],pname);
00115 #ifdef _WIN32
00116       strcat(a[0],".exe");
00117 #endif
00118     }
00119   }
00120   return a;
00121 }
00122 
00123 void cleanup_argv(int nopt,char *** pa)
00124 {
00125   if (pa)
00126   {
00127     char **a=*pa;
00128     if (a)
00129     {
00130       for (int i=0;i<=nopt;i++)
00131       {
00132         if (a[i])
00133         {
00134           free(a[i]);
00135         }
00136       }
00137       free(a);
00138       a=NULL;
00139     }
00140   }
00141 }
00142 
00143 #if defined(_WIN32)
00144   #include <windows.h>
00145 #endif
00146 
00147   #if !defined(_MSC_VER)
00148 void get_sp_printf(void)
00149 {
00150   ad_printf=NULL;
00151 /*
00152   HINSTANCE h=LoadLibrary("sqpe.dll");
00153   if(h)
00154     ad_printf= (fptr) GetProcAddress(h,"S_newio_printf");
00155 */
00156 }
00157   #endif
00158 
00159 void do_dll_housekeeping(int argc,char ** argv)
00160 {
00161 #if !defined(_MSC_VER)
00162   int on = option_match(argc,argv,"-sp");
00163   if (on > -1)
00164   {
00165   #if defined(_WIN32)
00166     get_sp_printf();
00167   #endif
00168     ad_exit=spdll_exit;
00169   }
00170   else if ( (on=option_match(argc,argv,"-spexit"))>-1)
00171   {
00172     ad_printf=printf;
00173     ad_exit=spdll_exit;
00174   }
00175   else
00176   {
00177     ad_exit=exit;
00178     if (!ad_printf) ad_printf=printf;
00179   }
00180 #else
00181   ad_exit=exit;
00182 
00183   if (!ad_printf) ad_printf=printf;
00184 #endif
00185 }
00186 
00187 /*
00188 void ssbul_l(char * ptmp){;}
00189 char ** parse_dll_options(char * pname, const int& _nopt,char * sp_options)
00190 {
00191   int& nopt=(int&) _nopt;
00192   nopt=1;
00193   char ** a;
00194 
00195   a=(char**)malloc(61);
00196   for (int i=0;i<60;i++)
00197   {
00198     a[i]=(char*)malloc(101*sizeof(char*));
00199   }
00200 
00201   char * p=NULL;
00202   if (strlen(sp_options))
00203   {
00204     int string_size= (strlen(sp_options)+5)*sizeof(char);
00205     //int string_size= (strlen(sp_options)+1)*sizeof(char);
00206     p = (char *) malloc(string_size);
00207     char * ptmp=NULL;
00208     strcpy(p,sp_options);
00209     ptmp = strtok(p," ");
00210     if (ptmp) nopt++;
00211     ssbul_l(ptmp);
00212     do
00213     {
00214       ptmp=strtok('\0'," ");
00215       if (ptmp) nopt++;
00216     }
00217     while(ptmp);
00218 
00219     //a=(char **)malloc((nopt+5)*sizeof(char *));
00220     //for (int k=0;k<=nopt;k++) a[k]=NULL;
00221     nopt=1;
00222     strcpy(p,sp_options);
00223     ptmp = strtok(p," ");
00224     if (ptmp)
00225     {
00226       //a[nopt]=(char *)malloc((strlen(ptmp)+5)*sizeof(char));
00227       strncpy(a[nopt++],ptmp,99);
00228     }
00229     do
00230     {
00231       ptmp=strtok('\0'," ");
00232       if (ptmp)
00233       {
00234         //a[nopt]=(char *)malloc((strlen(ptmp)+5)*sizeof(char));
00235         strncpy(a[nopt++],ptmp,99);
00236       }
00237       if (nopt>59)
00238       {
00239         cerr << "too many command line options in parse command line options" << endl;
00240         ad_exit(1);
00241       }
00242     }
00243     while(ptmp);
00244     delete p;
00245     p=NULL;
00246   }
00247   else
00248   {
00249     //a=(char **)malloc((nopt+5)*sizeof(char *));
00250     //a[nopt]=NULL;
00251   }
00252   //a[0]=(char *)malloc((strlen(pname)+10)*sizeof(char));
00253   strncpy(a[0],pname,99);
00254   strncat(a[0],".exe",99-strlen(a[0]));
00255   return a;
00256 }
00257 */
00258 /*
00259 class string_parser
00260 {
00261   char * str;
00262   char * nt;
00263   char * ct;
00264   string_parser(char * s,int n);
00265 };
00266 
00267 string_parser::string_parser(char * s,int n)
00268 {
00269   if (strlen(s)>n)
00270   {
00271     cerr "string too long in string_parser" << endl;
00272     exit(1);
00273   }
00274 
00275   str=new char[strlen(s+20];
00276   strcpy(str,s);
00277 
00278   ct=str;
00279 
00280   while (ct==' ')
00281   {
00282     ct++;
00283   }
00284 
00285   nt=ct;
00286 
00287   int ii=0;
00288   while (nt != ' ' && nt != '\0')
00289   {
00290     nt++;
00291     ii++;
00292   }
00293   nlen=ii;
00294 
00295   while (nt==' ')
00296   {
00297     nt++;
00298   }
00299 }
00300 
00301 char * string_parser::get_next_option(int & n)
00302 {
00303   n=nlen;
00304   char * rs=ct;
00305 
00306   if (nlen!=0)
00307   {
00308     ct=nt;
00309     int ii=0;
00310     while (nt != ' ' && nt != '\0')
00311     {
00312       nt++;
00313       ii++;
00314     }
00315     nlen=ii;
00316 
00317     while (nt==' ')
00318     {
00319       nt++;
00320     }
00321   }
00322   return rs;
00323 }
00324 
00325 char** parse_dll_options(char *pname, const int& _nopt, char *sp_options)
00326 {
00327   int& nopt=(int&) _nopt;
00328   nopt=1;
00329   char ** a;
00330 
00331   a=(char**)malloc(61);
00332   for (int i=0;i<60;i++)
00333   {
00334     a[i]=(char*)malloc(101*sizeof(char*));
00335   }
00336   do
00337   {
00338     s=get_next_option(sp,n);
00339     if(n==0) break;
00340     strncpy(a[ii++],sp,100);
00341   }
00342   while(1);
00343 
00344 
00345 
00346   strncpy(a[0],pname,99);
00347   strncat(a[0],".exe",99-strlen(a[0]));
00348   strncpy(a[1],"-maxfn",99);
00349   nopt=2;
00350   return a;
00351 }
00352 */
00353 class string_parser
00354 {
00355   char * str;
00356   char * nt;
00357   char * ct;
00358   size_t nlen;
00359 public:
00360   string_parser(char* s, const size_t n);
00361   char* get_next_option(size_t& n);
00362 };
00363 
00364 string_parser::string_parser(char * s, const size_t n)
00365 {
00366   if (strlen(s) > n)
00367   {
00368     cerr << "string too long in string_parser" << endl;
00369     exit(1);
00370   }
00371 
00372   str=new char[strlen(s)+20];
00373   strcpy(str,s);
00374 
00375   ct=str;
00376 
00377   while (*ct==' ')
00378   {
00379     ct++;
00380   }
00381 
00382   nt=ct;
00383 
00384   size_t ii=0;
00385   while (*nt != ' ' && *nt != '\0')
00386   {
00387     nt++;
00388     ii++;
00389   }
00390   nlen=ii;
00391 
00392   while (*nt==' ')
00393   {
00394     nt++;
00395   }
00396 }
00397 
00398 char* string_parser::get_next_option(size_t& n)
00399 {
00400   n=nlen;
00401 
00402   char * rt=ct;
00403   if (nlen!=0)
00404   {
00405     ct=nt;
00406     size_t ii=0;
00407     while (*nt != ' ' && *nt != '\0')
00408     {
00409       nt++;
00410       ii++;
00411     }
00412     nlen=ii;
00413 
00414     while (*nt==' ')
00415     {
00416       nt++;
00417     }
00418   }
00419   return rt;
00420 }
00421 
00422 typedef char * chararray;
00423 
00424 void davesnobullshitstrncpy(char * a,const char * b, const size_t n)
00425 {
00426   strncpy(a,b,n);
00427   a[n]=0;
00428 }
00429 
00430 char** parse_dll_options(char *pname, const int& _argc, char *s)
00431 {
00432   const int maxargs=200;
00433   char** a = (char**)malloc(sizeof(char*)*(maxargs+2));
00434   if (a)
00435   {
00436     int& argc=(int&)(_argc);
00437     for (int i=0;i<=maxargs;i++)
00438     {
00439       a[i]=0;
00440     }
00441     string_parser sp(s,1000);
00442 
00443     size_t n=0;
00444     int ii=1;
00445     do
00446     {
00447       char * ss=sp.get_next_option(n);
00448       if (n>0)
00449       {
00450         if (ii>maxargs)
00451         {
00452           cerr << "maximum number of command line arguments exceeded"
00453                << endl;
00454         }
00455         a[ii]=(char*)malloc(sizeof(char)*(n+2));
00456         davesnobullshitstrncpy(a[ii],ss,n);
00457         cout << a[ii] << endl;
00458         ii++;
00459       }
00460     } while (n > 0);
00461     a[0]=(char*)malloc(sizeof(char)*(strlen(pname)+6));
00462     if (a[0])
00463     {
00464       strcpy(a[0],pname);
00465 #ifdef _WIN32
00466       strcat(a[0],".exe");
00467 #endif
00468       argc=ii;
00469     }
00470   }
00471   return a;
00472 }