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