ADMB Documentation  11.1.1916
 All Classes Files Functions Variables Typedefs Friends Defines
fvar_io2.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: fvar_io2.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 
00013 #if defined(__TURBOC__)
00014   #pragma hdrstop
00015   #include <iostream.h>
00016   #include <strstrea.h>
00017 #endif
00018 
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 #endif
00028 
00029 #ifdef __NDPX__
00030   #include <iostream.h>
00031   #include <sstream.h>
00032 #endif
00033 
00034 #ifdef __SUN__
00035   #include <iostream.h>
00036   #include <strstream.h>
00037   #define __USE_IOSTREAM__
00038 #endif
00039 
00040 #include <string.h>
00041 #include <ctype.h>
00042 
00043 #include <sstream>
00044 using std::istringstream;
00045 
00046 const unsigned int MAX_LINE_LENGTH = 10000;
00047 const int MAX_FIELD_LENGTH = 500;
00048 const int MAX_NUMBER_COLUMNS = 6550;
00049 const int MAX_NUMBER_ROWS = 6550;
00050 
00055 dvar_vector::dvar_vector(const char * s)
00056 {
00057   int n = strlen(s);
00058   int lbraces = 0;
00059   int rbraces = 0;
00060   int commas  = 0;
00061 
00062   char *t = new char[n];
00063 
00064   int k;
00065   for (k = 0; k < n; k++)
00066   {
00067     if (s[k] == '{')
00068     {
00069       lbraces ++;
00070       t[k] = ' ';
00071     }
00072     else if (s[k] == '}')
00073     {
00074       rbraces ++;
00075       t[k] = ' ';
00076     }
00077     else if (s[k] == ',')
00078     {
00079       commas ++;
00080       t[k] = ' ';
00081     }
00082     else
00083     {
00084       t[k] = s[k];
00085     }
00086   }
00087 
00088   if (lbraces != rbraces)
00089   {
00090     cerr << "Unbalanced braces in dvector::dvector(const char * s)\n";
00091     cerr << s << "\n";
00092     ad_exit(1);
00093   }
00094 
00095   if (lbraces > 1)
00096   {
00097     cerr << "Only one level of braces allowed in "
00098     "dvector::dvector(const char * s)\n";
00099     cerr << s << "\n";
00100     ad_exit(1);
00101   }
00102 
00103   if (lbraces == 1)
00104   {
00105     int ncl = 1;
00106     int nch = commas + 1;
00107 
00108     allocate(ncl,nch);
00109     istringstream ss(t);
00110 
00111     for (k = ncl; k <= nch; k++)
00112     {
00113       ss >> this->elem(k);
00114       //va[k].nc=0.;
00115     }
00116     delete[] t;
00117     t = 0;
00118   }
00119   else
00120   {
00121     const char * filename=s;
00122     ifstream infile(filename);
00123     if (!infile)
00124     {
00125      cerr << "Error opening file " << filename << " in dvector constructor "
00126       << "dvector::dvector(char * filename)\n";
00127       ad_exit(1);
00128    }
00129 
00130    int i=0;
00131 
00132 //   char * field = (char *) new[size_t(MAX_FIELD_LENGTH+1)];
00133    char * field = new char[size_t(MAX_FIELD_LENGTH+1)];
00134    int count=0;
00135    do
00136    {
00137      infile >> field;
00138      if (infile.good())
00139      {
00140        count++;
00141      }
00142      else
00143      {
00144        if (!infile.eof())
00145        {
00146          cerr << "Error reading file " << filename << " in dvector constructor "
00147            << "dvector::dvector(char * filename)\n";
00148          cerr << "Error appears to have occurred at element"
00149           << count+1 << endl;
00150          cerr << "Stream state is " << infile.rdstate() << endl;
00151          ad_exit(1);
00152        }
00153      }
00154    }
00155    while (!infile.eof());
00156 
00157    infile.clear();
00158    infile.seekg(0,ios::beg);
00159 
00160    allocate(1,count);
00161 
00162    #ifdef DIAG
00163      cout << "Created a ncopies with address " << _farptr_tolong(ncopies)<<"\n";
00164      cout << "Created a dvector with address " << _farptr_tolong(v) <<"\n";
00165    #endif
00166    if ( (va = arr_new(size()))==0 )
00167    {
00168      cerr << " Error trying to allocate memory for dvector\n";
00169      ad_exit(21);
00170    }
00171    char* err_ptr;
00172    for (i=1;i<=count;i++)
00173    {
00174      infile >> field;
00175      elem(i)=strtod(field,&err_ptr); // increment column counter
00176 
00177      if (isalpha(err_ptr[0]))
00178      {
00179        cerr << "Error decoding field " << filename
00180          << " in dmatrix::dmatrix(char * filename) " << "\n";
00181        cerr << "Error occurred at element " << count << "\n";
00182        cerr << "Offending characters start with "
00183            << err_ptr[0]
00184            << err_ptr[1]
00185            << err_ptr[2]
00186            << err_ptr[3] << "\n";
00187        ad_exit(1);
00188      }
00189      if (value(elem(i))== HUGE_VAL ||value(elem(i))== -HUGE_VAL)
00190      {
00191        cerr << "Overflow Error decoding field " << filename
00192            << " in dmatrix::dmatrix(char * filename) " << "\n";
00193        cerr << "Error occurred at element " << count << "\n";
00194        ad_exit(1);
00195      }
00196    }
00197    delete[] field;
00198    field = 0;
00199  }
00200 }