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