ADMB Documentation  11.5.3297
 All Classes Files Functions Variables Typedefs Friends Defines
dvect9.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  */
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   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((unsigned char)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  delete [] t;
00244  t = 0;
00245 }
00246 
00251 void dvector::allocate(const char * s)
00252 {
00253   const size_t n = strlen(s);
00254   int lbraces = 0;
00255   int rbraces = 0;
00256   int commas  = 0;
00257 
00258   char* t = new char[n+1];
00259   assert(t);
00260   t[n]= '\0';
00261 
00262   for (size_t 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((unsigned char)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   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((unsigned char)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  delete [] t;
00439  t = 0;
00440 }