ADMB Documentation  11.4.2833
 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   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     size_t 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 
00083     do
00084     {
00085       ptmp = strtok(NULL, " ");
00086       if (ptmp)
00087       {
00088         //a[nopt]=(char *)malloc((strlen(ptmp)+5)*sizeof(char));
00089         strcpy(a[nopt++],ptmp);
00090       }
00091     }
00092     while(ptmp);
00093 
00094     if (p)
00095     {
00096       free(p);
00097       p=NULL;
00098     }
00099   }
00100 /*
00101   else
00102   {
00103     //a=(char **)malloc((nopt+5)*sizeof(char *));
00104     //a[nopt]=NULL;
00105   }
00106 */
00107   //a[0]=(char *)malloc((strlen(pname)+10)*sizeof(char));
00108   strcpy(a[0],pname);
00109 #ifdef _WIN32
00110   strcat(a[0],".exe");
00111 #endif
00112   //return a;
00113 }
00114 
00115 char** no_dll_options(char *pname, const int& _nopt)
00116 {
00117   int& nopt=(int&) _nopt;
00118   nopt=1;
00119   char** a = (char**)malloc((nopt+1)*sizeof(char*));
00120   if (a)
00121   {
00122     a[nopt] = NULL;
00123     a[0] = (char*)malloc((strlen(pname)+5)*sizeof(char));
00124     if (a[0])
00125     {
00126       strcpy(a[0],pname);
00127 #ifdef _WIN32
00128       strcat(a[0],".exe");
00129 #endif
00130     }
00131   }
00132   return a;
00133 }
00134 
00135 void cleanup_argv(int nopt,char *** pa)
00136 {
00137   if (pa)
00138   {
00139     char **a=*pa;
00140     if (a)
00141     {
00142       for (int i=0;i<=nopt;i++)
00143       {
00144         if (a[i])
00145         {
00146           free(a[i]);
00147         }
00148       }
00149       free(a);
00150       a=NULL;
00151     }
00152   }
00153 }
00154 
00155 #if defined(_WIN32)
00156   #include <windows.h>
00157 #endif
00158 
00159   #if !defined(_MSC_VER)
00160 void get_sp_printf(void)
00161 {
00162   ad_printf=NULL;
00163 /*
00164   HINSTANCE h=LoadLibrary("sqpe.dll");
00165   if(h)
00166     ad_printf= (fptr) GetProcAddress(h,"S_newio_printf");
00167 */
00168 }
00169   #endif
00170 
00171 void do_dll_housekeeping(int argc,char ** argv)
00172 {
00173 #if !defined(_MSC_VER)
00174   int on = option_match(argc,argv,"-sp");
00175   if (on > -1)
00176   {
00177   #if defined(_WIN32)
00178     get_sp_printf();
00179   #endif
00180     ad_exit=spdll_exit;
00181   }
00182   else if ( (on=option_match(argc,argv,"-spexit"))>-1)
00183   {
00184     ad_printf=printf;
00185     ad_exit=spdll_exit;
00186   }
00187   else
00188   {
00189     ad_exit=exit;
00190     if (!ad_printf) ad_printf=printf;
00191   }
00192 #else
00193   ad_exit=exit;
00194 
00195   if (!ad_printf) ad_printf=printf;
00196 #endif
00197 }
00198 
00199 /*
00200 void ssbul_l(char * ptmp){;}
00201 char ** parse_dll_options(char * pname, const int& _nopt,char * sp_options)
00202 {
00203   int& nopt=(int&) _nopt;
00204   nopt=1;
00205   char ** a;
00206 
00207   a=(char**)malloc(61);
00208   for (int i=0;i<60;i++)
00209   {
00210     a[i]=(char*)malloc(101*sizeof(char*));
00211   }
00212 
00213   char * p=NULL;
00214   if (strlen(sp_options))
00215   {
00216     int string_size= (strlen(sp_options)+5)*sizeof(char);
00217     //int string_size= (strlen(sp_options)+1)*sizeof(char);
00218     p = (char *) malloc(string_size);
00219     char * ptmp=NULL;
00220     strcpy(p,sp_options);
00221     ptmp = strtok(p," ");
00222     if (ptmp) nopt++;
00223     ssbul_l(ptmp);
00224     do
00225     {
00226       ptmp=strtok('\0'," ");
00227       if (ptmp) nopt++;
00228     }
00229     while(ptmp);
00230 
00231     //a=(char **)malloc((nopt+5)*sizeof(char *));
00232     //for (int k=0;k<=nopt;k++) a[k]=NULL;
00233     nopt=1;
00234     strcpy(p,sp_options);
00235     ptmp = strtok(p," ");
00236     if (ptmp)
00237     {
00238       //a[nopt]=(char *)malloc((strlen(ptmp)+5)*sizeof(char));
00239       strncpy(a[nopt++],ptmp,99);
00240     }
00241     do
00242     {
00243       ptmp=strtok('\0'," ");
00244       if (ptmp)
00245       {
00246         //a[nopt]=(char *)malloc((strlen(ptmp)+5)*sizeof(char));
00247         strncpy(a[nopt++],ptmp,99);
00248       }
00249       if (nopt>59)
00250       {
00251         cerr << "too many command line options in parse command line options" << endl;
00252         ad_exit(1);
00253       }
00254     }
00255     while(ptmp);
00256     delete p;
00257     p=NULL;
00258   }
00259   else
00260   {
00261     //a=(char **)malloc((nopt+5)*sizeof(char *));
00262     //a[nopt]=NULL;
00263   }
00264   //a[0]=(char *)malloc((strlen(pname)+10)*sizeof(char));
00265   strncpy(a[0],pname,99);
00266   strncat(a[0],".exe",99-strlen(a[0]));
00267   return a;
00268 }
00269 */
00270 /*
00271 class string_parser
00272 {
00273   char * str;
00274   char * nt;
00275   char * ct;
00276   string_parser(char * s,int n);
00277 };
00278 
00279 string_parser::string_parser(char * s,int n)
00280 {
00281   if (strlen(s)>n)
00282   {
00283     cerr "string too long in string_parser" << endl;
00284     exit(1);
00285   }
00286 
00287   str=new char[strlen(s+20];
00288   strcpy(str,s);
00289 
00290   ct=str;
00291 
00292   while (ct==' ')
00293   {
00294     ct++;
00295   }
00296 
00297   nt=ct;
00298 
00299   int ii=0;
00300   while (nt != ' ' && nt != '\0')
00301   {
00302     nt++;
00303     ii++;
00304   }
00305   nlen=ii;
00306 
00307   while (nt==' ')
00308   {
00309     nt++;
00310   }
00311 }
00312 
00313 char * string_parser::get_next_option(int & n)
00314 {
00315   n=nlen;
00316   char * rs=ct;
00317 
00318   if (nlen!=0)
00319   {
00320     ct=nt;
00321     int ii=0;
00322     while (nt != ' ' && nt != '\0')
00323     {
00324       nt++;
00325       ii++;
00326     }
00327     nlen=ii;
00328 
00329     while (nt==' ')
00330     {
00331       nt++;
00332     }
00333   }
00334   return rs;
00335 }
00336 
00337 char** parse_dll_options(char *pname, const int& _nopt, char *sp_options)
00338 {
00339   int& nopt=(int&) _nopt;
00340   nopt=1;
00341   char ** a;
00342 
00343   a=(char**)malloc(61);
00344   for (int i=0;i<60;i++)
00345   {
00346     a[i]=(char*)malloc(101*sizeof(char*));
00347   }
00348   do
00349   {
00350     s=get_next_option(sp,n);
00351     if(n==0) break;
00352     strncpy(a[ii++],sp,100);
00353   }
00354   while(1);
00355 
00356 
00357 
00358   strncpy(a[0],pname,99);
00359   strncat(a[0],".exe",99-strlen(a[0]));
00360   strncpy(a[1],"-maxfn",99);
00361   nopt=2;
00362   return a;
00363 }
00364 */
00365 class string_parser
00366 {
00367   char * str;
00368   char * nt;
00369   char * ct;
00370   size_t nlen;
00371 public:
00372   string_parser(char* s, const size_t n);
00373   char* get_next_option(size_t& n);
00374 };
00375 
00376 string_parser::string_parser(char * s, const size_t n)
00377 {
00378   if (strlen(s) > n)
00379   {
00380     cerr << "string too long in string_parser" << endl;
00381     exit(1);
00382   }
00383 
00384   str=new char[strlen(s)+20];
00385   strcpy(str,s);
00386 
00387   ct=str;
00388 
00389   while (*ct==' ')
00390   {
00391     ct++;
00392   }
00393 
00394   nt=ct;
00395 
00396   size_t ii=0;
00397   while (*nt != ' ' && *nt != '\0')
00398   {
00399     nt++;
00400     ii++;
00401   }
00402   nlen=ii;
00403 
00404   while (*nt==' ')
00405   {
00406     nt++;
00407   }
00408 }
00409 
00410 char* string_parser::get_next_option(size_t& n)
00411 {
00412   n=nlen;
00413 
00414   char * rt=ct;
00415   if (nlen!=0)
00416   {
00417     ct=nt;
00418     size_t ii=0;
00419     while (*nt != ' ' && *nt != '\0')
00420     {
00421       nt++;
00422       ii++;
00423     }
00424     nlen=ii;
00425 
00426     while (*nt==' ')
00427     {
00428       nt++;
00429     }
00430   }
00431   return rt;
00432 }
00433 
00434 typedef char * chararray;
00435 
00436 void davesnobullshitstrncpy(char * a,const char * b, const size_t n)
00437 {
00438   strncpy(a,b,n);
00439   a[n]=0;
00440 }
00441 
00442 char** parse_dll_options(char *pname, const int& _argc, char *s)
00443 {
00444   const int maxargs=200;
00445   char** a = (char**)malloc(sizeof(char*)*(maxargs+2));
00446   if (a)
00447   {
00448     int& argc=(int&)(_argc);
00449     for (int i=0;i<=maxargs;i++)
00450     {
00451       a[i]=0;
00452     }
00453     string_parser sp(s,1000);
00454 
00455     size_t n=0;
00456     int ii=1;
00457     do
00458     {
00459       char * ss=sp.get_next_option(n);
00460       if (n>0)
00461       {
00462         if (ii>maxargs)
00463         {
00464           cerr << "maximum number of command line arguments exceeded"
00465                << endl;
00466         }
00467         a[ii]=(char*)malloc(sizeof(char)*(n+2));
00468         davesnobullshitstrncpy(a[ii],ss,n);
00469         cout << a[ii] << endl;
00470         ii++;
00471       }
00472     } while (n > 0);
00473     a[0]=(char*)malloc(sizeof(char)*(strlen(pname)+6));
00474     if (a[0])
00475     {
00476       strcpy(a[0],pname);
00477 #ifdef _WIN32
00478       strcat(a[0],".exe");
00479 #endif
00480       argc=ii;
00481     }
00482   }
00483   return a;
00484 }