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