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