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