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