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