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