ADMB Documentation  11.1x.2730
 All Classes Files Functions Variables Typedefs Friends Defines
fvar_io2.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: fvar_io2.cpp 2693 2014-11-19 19:11:40Z 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 #ifdef __ZTC__
00020   #include <iostream.hpp>
00021   #if (__ZTC__ < 0x310)
00022     #include <sstream.hpp>
00023   #else
00024     #include <strstream.h>
00025   #endif
00026 #endif
00027 
00028 #ifdef __NDPX__
00029   #include <iostream.h>
00030   #include <sstream.h>
00031 #endif
00032 
00033 #ifdef __SUN__
00034   #include <iostream.h>
00035   #include <strstream.h>
00036   #define __USE_IOSTREAM__
00037 #endif
00038 
00039 #include <string.h>
00040 #include <ctype.h>
00041 
00042 #include <sstream>
00043 using std::istringstream;
00044 
00045 #ifndef OPT_LIB
00046   #include <cassert>
00047 #endif
00048 
00049 const int MAX_FIELD_LENGTH = 500;
00050 
00054 dvar_vector::dvar_vector(const char * s)
00055 {
00056   size_t n = strlen(s);
00057   int lbraces = 0;
00058   int rbraces = 0;
00059   int commas  = 0;
00060 
00061   char *t = new char[n];
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     istringstream ss(t);
00108 
00109     for (int k = ncl; k <= nch; k++)
00110     {
00111       ss >> this->elem(k);
00112       //va[k].nc=0.;
00113     }
00114     delete[] t;
00115     t = 0;
00116   }
00117   else
00118   {
00119     const char * filename=s;
00120     ifstream infile(filename);
00121     if (!infile)
00122     {
00123      cerr << "Error opening file " << filename << " in dvector constructor "
00124       << "dvector::dvector(char * filename)\n";
00125       ad_exit(1);
00126    }
00127 
00128    int i=0;
00129 
00130 //   char * field = (char *) new[size_t(MAX_FIELD_LENGTH+1)];
00131    char * field = new char[size_t(MAX_FIELD_LENGTH+1)];
00132    int count=0;
00133    do
00134    {
00135      infile >> field;
00136      if (infile.good())
00137      {
00138        count++;
00139      }
00140      else
00141      {
00142        if (!infile.eof())
00143        {
00144          cerr << "Error reading file " << filename << " in dvector constructor "
00145            << "dvector::dvector(char * filename)\n";
00146          cerr << "Error appears to have occurred at element"
00147           << count+1 << endl;
00148          cerr << "Stream state is " << infile.rdstate() << endl;
00149          ad_exit(1);
00150        }
00151      }
00152    }
00153    while (!infile.eof());
00154 
00155    infile.clear();
00156    infile.seekg(0,ios::beg);
00157 
00158    allocate(1,count);
00159 
00160    #ifdef DIAG
00161      cout << "Created a ncopies with address " << _farptr_tolong(ncopies)<<"\n";
00162      cout << "Created a dvector with address " << _farptr_tolong(v) <<"\n";
00163    #endif
00164 #ifndef OPT_LIB
00165   assert(size() > 0);
00166 #endif
00167    if ((va = arr_new((unsigned int)size()))==0 )
00168    {
00169      cerr << " Error trying to allocate memory for dvector\n";
00170      ad_exit(21);
00171    }
00172    char* err_ptr;
00173    for (i=1;i<=count;i++)
00174    {
00175      infile >> field;
00176      elem(i)=strtod(field,&err_ptr); // increment column counter
00177 
00178      if (isalpha((unsigned char)err_ptr[0]))
00179      {
00180        cerr << "Error decoding field " << filename
00181          << " in dmatrix::dmatrix(char * filename) " << "\n";
00182        cerr << "Error occurred at element " << count << "\n";
00183        cerr << "Offending characters start with "
00184            << err_ptr[0]
00185            << err_ptr[1]
00186            << err_ptr[2]
00187            << err_ptr[3] << "\n";
00188        ad_exit(1);
00189      }
00190      if (value(elem(i))== HUGE_VAL ||value(elem(i))== -HUGE_VAL)
00191      {
00192        cerr << "Overflow Error decoding field " << filename
00193            << " in dmatrix::dmatrix(char * filename) " << "\n";
00194        cerr << "Error occurred at element " << count << "\n";
00195        ad_exit(1);
00196      }
00197    }
00198    delete[] field;
00199    field = 0;
00200  }
00201 }