ADMB Documentation  11.1.2274
 All Classes Files Functions Variables Typedefs Friends Defines
dvect9.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: dvect9.cpp 2218 2014-07-28 16:52:21Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #include "fvar.hpp"
00012 #include <math.h>
00013 
00014 #if defined(__TURBOC__)
00015   #pragma hdrstop
00016   #include <iostream.h>
00017   #include <strstrea.h>
00018 #endif
00019 
00020 #ifdef __ZTC__
00021   #include <iostream.hpp>
00022   #if (__ZTC__ < 0x310)
00023     #include <sstream.hpp>
00024   #else
00025     #include <strstream.h>
00026   #endif
00027   #define __USE_IOSTREAM__
00028 #endif
00029 
00030 #ifdef __NDPX__
00031   #include <iostream.h>
00032   #include <sstream.h>
00033 #endif
00034 
00035 #ifdef __SUN__
00036   #include <iostream.h>
00037   #include <strstream.h>
00038   #define __USE_IOSTREAM__
00039 #endif
00040 
00041 #include <string.h>
00042 #include <ctype.h>
00043 
00044 #include <sstream>
00045 using std::istringstream;
00046 
00047 const int MAX_FIELD_LENGTH = 500;
00048 
00053 dvector::dvector(const char * s)
00054 {
00055   int n = strlen(s);
00056   int lbraces = 0;
00057   int rbraces = 0;
00058   int commas  = 0;
00059 
00060   char *t = new char[n+1];
00061   t[n]=0;
00062 
00063   int k;
00064   for (k = 0; k < n; k++)
00065   {
00066     if (s[k] == '{')
00067     {
00068       lbraces ++;
00069       t[k] = ' ';
00070     }
00071     else if (s[k] == '}')
00072     {
00073       rbraces ++;
00074       t[k] = ' ';
00075     }
00076     else if (s[k] == ',')
00077     {
00078       commas ++;
00079       t[k] = ' ';
00080     }
00081     else
00082     {
00083       t[k] = s[k];
00084     }
00085   }
00086 
00087   if (lbraces != rbraces)
00088   {
00089     cerr << "Unbalanced braces in dvector::dvector(const char * s)\n";
00090     cerr << s << "\n";
00091     ad_exit(1);
00092   }
00093 
00094   if (lbraces > 1)
00095   {
00096     cerr << "Only one level of braces allowed in "
00097     "dvector::dvector(const char * s)\n";
00098     cerr << s << "\n";
00099     ad_exit(1);
00100   }
00101 
00102   if (lbraces == 1)
00103   {
00104     int ncl = 1;
00105     int nch = commas + 1;
00106 
00107     allocate(ncl,nch);
00108 
00109     istringstream ss(t);
00110 
00111    char *field =  new char[size_t(MAX_FIELD_LENGTH+1)];
00112    char *err_ptr;
00113 
00114    for (int i=ncl;i<=nch;i++)
00115    {
00116      ss >> field;
00117      v[i]=strtod(field,&err_ptr); // increment column counter
00118 
00119      if (isalpha(err_ptr[0]))
00120      {
00121        cerr << "Error decoding field "
00122          << " in dvector::dvector(char * filename) " << "\n";
00123        cerr << "Error occurred at element " << i << "\n";
00124        cerr << "Offending characters start with "
00125            << field << endl;
00126        ad_exit(1);
00127      }
00128 
00129      if (elem(i)== HUGE_VAL ||elem(i)== -HUGE_VAL)
00130      {
00131        cerr << "Overflow Error decoding field "
00132            " in dvector::dvector(char * ) " << "\n";
00133        cerr << "Error occurred at element " << i << "\n";
00134        cerr << "Offending characters start with "
00135            << field << endl;
00136        ad_exit(1);
00137      }
00138    }
00139    delete [] field;
00140    field=0;
00141 
00142     delete [] t;
00143     t=0;
00144   }
00145   else // if lbraces == 0
00146   {
00147    const char * filename=s;
00148    ifstream infile(filename);
00149    if (!infile)
00150    {
00151      cerr << "Error opening file " << filename << " in dvector constructor "
00152       << "dvector::dvector(char * filename)\n";
00153       ad_exit(1);
00154    }
00155 
00156    int i=0;
00157 
00158 //   char * field = (char*) new[size_t(MAX_FIELD_LENGTH+1)];
00159    char * field = new char[size_t(MAX_FIELD_LENGTH+1)];
00160    int count=0;
00161    do
00162    {
00163      infile >> field;
00164      if (infile.good())
00165      {
00166        count++;
00167      }
00168      else
00169      {
00170        if (!infile.eof())
00171        {
00172          cerr << "Error reading file " << filename << " in dvector constructor "
00173            << "dvector::dvector(char * filename)\n";
00174          cerr << "Error appears to have occurred at element"
00175           << count+1 << endl;
00176          cerr << "Stream state is " << infile.rdstate() << endl;
00177          ad_exit(1);
00178        }
00179      }
00180    }
00181    while (!infile.eof());
00182    infile.clear();
00183    infile.seekg(0,ios::beg);
00184 
00185     if ( (v = new double [count+2]) ==0)
00186     {
00187       cerr << " Error trying to allocate memory for dvector\n";
00188       ad_exit(21);
00189     }
00190  #if defined(THREAD_SAFE)
00191   if ( (shape=new ts_vector_shapex(1,count,v)) == NULL)
00192  #else
00193   if ( (shape=new vector_shapex(1,count,v)) == NULL)
00194  #endif
00195     {
00196       cerr << "Error trying to allocate memory for dvector\n";
00197       ad_exit(1);
00198     }
00199 
00200 
00201     index_min=1;
00202     index_max=count;
00203     v -= indexmin();
00204 
00205    #ifdef DIAG
00206      cout << "Created a ncopies with address " << _farptr_tolong(ncopies)
00207           <<"\n";
00208      cout << "Created a dvector with address " << _farptr_tolong(v) <<"\n";
00209    #endif
00210    char * err_ptr;
00211    for (i=1;i<=count;i++)
00212    {
00213      infile >> field;
00214      elem(i)=strtod(field,&err_ptr); // increment column counter
00215 
00216      if (isalpha(err_ptr[0]))
00217      {
00218        cerr << "Error decoding field " << filename
00219          << " in dvector::dvector(char * filename) " << "\n";
00220        cerr << "Error occurred at element " << count << "\n";
00221        cerr << "Offending characters start with "
00222            << err_ptr[0]
00223            << err_ptr[1]
00224            << err_ptr[2]
00225            << err_ptr[3] << "\n";
00226        ad_exit(1);
00227      }
00228 
00229      if (elem(i) == HUGE_VAL ||elem(i) == -HUGE_VAL)
00230      {
00231        cerr << "Overflow Error decoding field " << filename
00232            << " in dvector::dvector(char * filename) " << "\n";
00233        cerr << "Error occurred at element " << count << "\n";
00234        cerr << "Offending characters start with "
00235            << err_ptr[0]
00236            << err_ptr[1]
00237            << err_ptr[2]
00238            << err_ptr[3] << "\n";
00239        ad_exit(1);
00240      }
00241    }
00242    delete[] field;
00243    field = 0;
00244  }
00245 }
00246 
00251 void dvector::allocate(const char * s)
00252 {
00253   int n = strlen(s);
00254   int lbraces = 0;
00255   int rbraces = 0;
00256   int commas  = 0;
00257 
00258   char *t = new char[n+1];
00259   t[n+1]=0;
00260 
00261   int k;
00262   for (k = 0; k < n; k++)
00263   {
00264     if (s[k] == '{')
00265     {
00266       lbraces ++;
00267       t[k] = ' ';
00268     }
00269     else if (s[k] == '}')
00270     {
00271       rbraces ++;
00272       t[k] = ' ';
00273     }
00274     else if (s[k] == ',')
00275     {
00276       commas ++;
00277       t[k] = ' ';
00278     }
00279     else
00280     {
00281       t[k] = s[k];
00282     }
00283   }
00284 
00285   if (lbraces != rbraces)
00286   {
00287     cerr << "Unbalanced braces in dvector::dvector(const char * s)\n";
00288     cerr << s << "\n";
00289     ad_exit(1);
00290   }
00291 
00292   if (lbraces > 1)
00293   {
00294     cerr << "Only one level of braces allowed in "
00295     "dvector::dvector(const char * s)\n";
00296     cerr << s << "\n";
00297     ad_exit(1);
00298   }
00299 
00300   if (lbraces == 1)
00301   {
00302     int ncl = 1;
00303     int nch = commas + 1;
00304 
00305     allocate(ncl,nch);
00306 
00307     istringstream ss(t);
00308 
00309    char * field =  new char[size_t(MAX_FIELD_LENGTH+1)];
00310    char * err_ptr;
00311 
00312    for (int i=ncl;i<=nch;i++)
00313    {
00314      ss >> field;
00315      v[i]=strtod(field,&err_ptr); // increment column counter
00316 
00317      if (isalpha(err_ptr[0]))
00318      {
00319        cerr << "Error decoding field "
00320          << " in dvector::dvector(char * filename) " << "\n";
00321        cerr << "Error occurred at element " << i << "\n";
00322        cerr << "Offending characters start with "
00323            << err_ptr[0]
00324            << err_ptr[1]
00325            << err_ptr[2]
00326            << err_ptr[3] << "\n";
00327        ad_exit(1);
00328      }
00329 
00330      if (elem(i) == HUGE_VAL ||elem(i) == -HUGE_VAL)
00331      {
00332        cerr << "Overflow Error decoding field "
00333            " in dvector::dvector(char * ) " << "\n";
00334        cerr << "Error occurred at element " << i << "\n";
00335        ad_exit(1);
00336      }
00337    }
00338    delete [] field;
00339    field=0;
00340 
00341     delete [] t;
00342     t=0;
00343   }
00344   else // if lbraces == 0
00345   {
00346    const char * filename=s;
00347    ifstream infile(filename);
00348    if (!infile)
00349    {
00350      cerr << "Error opening file " << filename << " in dvector constructor "
00351       << "dvector::dvector(char * filename)\n";
00352       ad_exit(1);
00353    }
00354 
00355    int i=0;
00356 
00357 //   char * field = (char*) new[size_t(MAX_FIELD_LENGTH+1)];
00358    char * field = new char[size_t(MAX_FIELD_LENGTH+1)];
00359    int count=0;
00360    do
00361    {
00362      infile >> field;
00363      if (infile.good())
00364      {
00365        count++;
00366      }
00367      else
00368      {
00369        if (!infile.eof())
00370        {
00371          cerr << "Error reading file " << filename << " in dvector constructor "
00372            << "dvector::dvector(char * filename)\n";
00373          cerr << "Error appears to have occurred at element"
00374           << count+1 << endl;
00375          cerr << "Stream state is " << infile.rdstate() << endl;
00376          ad_exit(1);
00377        }
00378      }
00379    }
00380    while (!infile.eof());
00381    infile.clear();
00382    infile.seekg(0,ios::beg);
00383 
00384    if ( (v = new double [(size_t) size()]) ==0)
00385    {
00386      cerr << " Error trying to allocate memory for dvector\n";
00387      ad_exit(21);
00388    }
00389 #if defined(THREAD_SAFE)
00390    if ( (shape=new ts_vector_shapex(1,count,v)) == NULL)
00391 #else
00392    if ( (shape=new vector_shapex(1,count,v)) == NULL)
00393 #endif
00394    {
00395      cerr << "Error trying to allocate memory for dvector\n";
00396      ad_exit(21);
00397    }
00398 
00399    #ifdef DIAG
00400      cout << "Created a ncopies with address " << _farptr_tolong(ncopies)
00401           <<"\n";
00402      cout << "Created a dvector with address " << _farptr_tolong(v) <<"\n";
00403    #endif
00404    v -= indexmin();
00405    char * err_ptr;
00406    for (i=1;i<=count;i++)
00407    {
00408      infile >> field;
00409      elem(i)=strtod(field,&err_ptr); // increment column counter
00410 
00411      if (isalpha(err_ptr[0]))
00412      {
00413        cerr << "Error decoding field " << filename
00414          << " in dvector::dvector(char * filename) " << "\n";
00415        cerr << "Error occurred at element " << count << "\n";
00416        cerr << "Offending characters start with "
00417            << err_ptr[0]
00418            << err_ptr[1]
00419            << err_ptr[2]
00420            << err_ptr[3] << "\n";
00421        ad_exit(1);
00422      }
00423 
00424      if (elem(i) == HUGE_VAL ||elem(i) == -HUGE_VAL)
00425      {
00426        cerr << "Overflow Error decoding field " << filename
00427            << " in dvector::dvector(char * filename) " << "\n";
00428        cerr << "Error occurred at element " << count << "\n";
00429        cerr << "Offending characters start with "
00430            << err_ptr[0]
00431            << err_ptr[1]
00432            << err_ptr[2]
00433            << err_ptr[3] << "\n";
00434        ad_exit(1);
00435        ad_exit(1);
00436      }
00437    }
00438    delete [] field;
00439    field=0;
00440  }
00441 }