ADMB Documentation  11.1.2247
 All Classes Files Functions Variables Typedefs Friends Defines
model33.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: model33.cpp 1934 2014-04-26 01:24:19Z johnoel $
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   if (ad_comm::global_savefile)
00036   {
00037     delete ad_comm::global_savefile;
00038     ad_comm::global_savefile=NULL;
00039   }
00040 
00041   if (ad_comm::global_bsavefile)
00042   {
00043     delete ad_comm::global_bsavefile;
00044     ad_comm::global_bsavefile=NULL;
00045   }
00046 }
00047 
00048 void parse_dll_options(char *pname, const int& _nopt, char *sp_options,
00049   char *** pargv)
00050 {
00051   int& nopt=(int&) _nopt;
00052   nopt=1;
00053   char ** a=*pargv;
00054   char * p=NULL;
00055   if (strlen(sp_options))
00056   {
00057     int string_size= (strlen(sp_options)+5)*sizeof(char);
00058     //int string_size= (strlen(sp_options)+1)*sizeof(char);
00059     p = (char *) malloc(string_size);
00060     char * ptmp=NULL;
00061     strcpy(p,sp_options);
00062     ptmp = strtok(p," ");
00063     if (ptmp) nopt++;
00064     //ssbul_l(ptmp);
00065     do
00066     {
00067       ptmp = strtok(NULL, " ");
00068       if (ptmp) nopt++;
00069     }
00070     while(ptmp);
00071 
00072     //a=(char **)malloc((nopt+5)*sizeof(char *));
00073     //for (int k=0;k<=nopt;k++) a[k]=NULL;
00074     nopt=1;
00075     strcpy(p,sp_options);
00076     ptmp = strtok(p," ");
00077     if (ptmp)
00078     {
00079       //a[nopt]=(char *)malloc((strlen(ptmp)+5)*sizeof(char));
00080       strcpy(a[nopt++],ptmp);
00081     }
00082     do
00083     {
00084       ptmp = strtok(NULL, " ");
00085       if (ptmp)
00086       {
00087         //a[nopt]=(char *)malloc((strlen(ptmp)+5)*sizeof(char));
00088         strcpy(a[nopt++],ptmp);
00089       }
00090     }
00091     while(ptmp);
00092     delete[] p;
00093     p=NULL;
00094   }
00095   else
00096   {
00097     //a=(char **)malloc((nopt+5)*sizeof(char *));
00098     //a[nopt]=NULL;
00099   }
00100   //a[0]=(char *)malloc((strlen(pname)+10)*sizeof(char));
00101   strcpy(a[0],pname);
00102   strcat(a[0],".exe");
00103   //return a;
00104 }
00105 
00106 char** no_dll_options(char *pname, const int& _nopt)
00107 {
00108   int& nopt=(int&) _nopt;
00109   nopt=1;
00110   char** a = 0;
00111   //char * p=NULL;
00112   //a=(char **)malloc((nopt+1)*sizeof(char *));
00113   a[nopt]=NULL;
00114   //a[0]=(char *)malloc((strlen(pname)+5)*sizeof(char));
00115   strcpy(a[0],pname);
00116   strcat(a[0],".exe");
00117   return a;
00118 }
00119 
00120 void cleanup_argv(int nopt,char *** pa)
00121 {
00122   if (pa)
00123   {
00124     char **a=*pa;
00125     if (a)
00126     {
00127       for (int i=0;i<=nopt;i++)
00128       {
00129         if (a[i])
00130         {
00131           free(a[i]);
00132         }
00133       }
00134       free(a);
00135       a=NULL;
00136     }
00137   }
00138 }
00139 
00140 #if !defined(_MSC_VER)
00141   #if defined(_WIN32)
00142     #include <windows.h>
00143 void get_sp_printf(void)
00144 {
00145   ad_printf=NULL;
00146 /*
00147   HINSTANCE h=LoadLibrary("sqpe.dll");
00148   if(h)
00149     ad_printf= (fptr) GetProcAddress(h,"S_newio_printf");
00150 */
00151 }
00152   #endif
00153 #endif
00154 
00155 void do_dll_housekeeping(int argc,char ** argv)
00156 {
00157 #if !defined(_MSC_VER)
00158   int on = option_match(argc,argv,"-sp");
00159   if (on > -1)
00160   {
00161   #if defined(_WIN32)
00162     get_sp_printf();
00163   #endif
00164     ad_exit=spdll_exit;
00165   }
00166   else if ( (on=option_match(argc,argv,"-spexit"))>-1)
00167   {
00168     ad_printf=printf;
00169     ad_exit=spdll_exit;
00170   }
00171   else
00172   {
00173     ad_exit=exit;
00174     if (!ad_printf) ad_printf=printf;
00175   }
00176 #else
00177   ad_exit=exit;
00178 
00179   if (!ad_printf) ad_printf=printf;
00180 #endif
00181 }
00182 
00183 /*
00184 void ssbul_l(char * ptmp){;}
00185 char ** parse_dll_options(char * pname, const int& _nopt,char * sp_options)
00186 {
00187   int& nopt=(int&) _nopt;
00188   nopt=1;
00189   char ** a;
00190 
00191   a=(char**)malloc(61);
00192   for (int i=0;i<60;i++)
00193   {
00194     a[i]=(char*)malloc(101*sizeof(char*));
00195   }
00196 
00197   char * p=NULL;
00198   if (strlen(sp_options))
00199   {
00200     int string_size= (strlen(sp_options)+5)*sizeof(char);
00201     //int string_size= (strlen(sp_options)+1)*sizeof(char);
00202     p = (char *) malloc(string_size);
00203     char * ptmp=NULL;
00204     strcpy(p,sp_options);
00205     ptmp = strtok(p," ");
00206     if (ptmp) nopt++;
00207     ssbul_l(ptmp);
00208     do
00209     {
00210       ptmp=strtok('\0'," ");
00211       if (ptmp) nopt++;
00212     }
00213     while(ptmp);
00214 
00215     //a=(char **)malloc((nopt+5)*sizeof(char *));
00216     //for (int k=0;k<=nopt;k++) a[k]=NULL;
00217     nopt=1;
00218     strcpy(p,sp_options);
00219     ptmp = strtok(p," ");
00220     if (ptmp)
00221     {
00222       //a[nopt]=(char *)malloc((strlen(ptmp)+5)*sizeof(char));
00223       strncpy(a[nopt++],ptmp,99);
00224     }
00225     do
00226     {
00227       ptmp=strtok('\0'," ");
00228       if (ptmp)
00229       {
00230         //a[nopt]=(char *)malloc((strlen(ptmp)+5)*sizeof(char));
00231         strncpy(a[nopt++],ptmp,99);
00232       }
00233       if (nopt>59)
00234       {
00235         cerr << "too many command line options in parse command line options" << endl;
00236         ad_exit(1);
00237       }
00238     }
00239     while(ptmp);
00240     delete p;
00241     p=NULL;
00242   }
00243   else
00244   {
00245     //a=(char **)malloc((nopt+5)*sizeof(char *));
00246     //a[nopt]=NULL;
00247   }
00248   //a[0]=(char *)malloc((strlen(pname)+10)*sizeof(char));
00249   strncpy(a[0],pname,99);
00250   strncat(a[0],".exe",99-strlen(a[0]));
00251   return a;
00252 }
00253 */
00254 /*
00255 class string_parser
00256 {
00257   char * str;
00258   char * nt;
00259   char * ct;
00260   string_parser(char * s,int n);
00261 };
00262 
00263 string_parser::string_parser(char * s,int n)
00264 {
00265   if (strlen(s)>n)
00266   {
00267     cerr "string too long in string_parser" << endl;
00268     exit(1);
00269   }
00270 
00271   str=new char[strlen(s+20];
00272   strcpy(str,s);
00273 
00274   ct=str;
00275 
00276   while (ct==' ')
00277   {
00278     ct++;
00279   }
00280 
00281   nt=ct;
00282 
00283   int ii=0;
00284   while (nt != ' ' && nt != '\0')
00285   {
00286     nt++;
00287     ii++;
00288   }
00289   nlen=ii;
00290 
00291   while (nt==' ')
00292   {
00293     nt++;
00294   }
00295 }
00296 
00297 char * string_parser::get_next_option(int & n)
00298 {
00299   n=nlen;
00300   char * rs=ct;
00301 
00302   if (nlen!=0)
00303   {
00304     ct=nt;
00305     int ii=0;
00306     while (nt != ' ' && nt != '\0')
00307     {
00308       nt++;
00309       ii++;
00310     }
00311     nlen=ii;
00312 
00313     while (nt==' ')
00314     {
00315       nt++;
00316     }
00317   }
00318   return rs;
00319 }
00320 
00321 char** parse_dll_options(char *pname, const int& _nopt, char *sp_options)
00322 {
00323   int& nopt=(int&) _nopt;
00324   nopt=1;
00325   char ** a;
00326 
00327   a=(char**)malloc(61);
00328   for (int i=0;i<60;i++)
00329   {
00330     a[i]=(char*)malloc(101*sizeof(char*));
00331   }
00332   do
00333   {
00334     s=get_next_option(sp,n);
00335     if(n==0) break;
00336     strncpy(a[ii++],sp,100);
00337   }
00338   while(1);
00339 
00340 
00341 
00342   strncpy(a[0],pname,99);
00343   strncat(a[0],".exe",99-strlen(a[0]));
00344   strncpy(a[1],"-maxfn",99);
00345   nopt=2;
00346   return a;
00347 }
00348 */
00349 class string_parser
00350 {
00351   char * str;
00352   char * nt;
00353   char * ct;
00354   int nlen;
00355 public:
00356   string_parser(char * s,int n);
00357   char * get_next_option(int & n);
00358 };
00359 
00360 void davesnobullshitstrncpy(char * a,const char * b, int n);
00361 char ** parse_dll_options(char * s,int& argc);
00362 
00363 
00364 string_parser::string_parser(char * s,int n)
00365 {
00366   if (strlen(s) > (unsigned int)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   int 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(int & n)
00399 {
00400   n=nlen;
00401 
00402   char * rt=ct;
00403   if (nlen!=0)
00404   {
00405     ct=nt;
00406     int 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, int 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   int& argc=(int&)(_argc);
00433   char ** a;
00434   const int maxargs=200;
00435   a = (char **) malloc(sizeof(char*)*(maxargs+2));
00436 
00437   for (int i=0;i<=maxargs;i++)
00438   {
00439     a[i]=0;
00440   }
00441   string_parser sp(s,1000);
00442 
00443   int 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 lne arguemtns 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   }
00461   while (n>0);
00462   a[0]=(char*)malloc(sizeof(char)*(strlen(pname)+6));
00463   strcpy(a[0],pname);
00464   strcat(a[0],".exe");
00465   argc=ii;
00466   return a;
00467 }