ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
fvar_io2.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 
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   }
00115   else
00116   {
00117     const char* filename = s;
00118     ifstream infile(filename);
00119     if (!infile)
00120     {
00121      cerr << "Error opening file " << filename << " in dvector constructor "
00122       << "dvector::dvector(char * filename)\n";
00123       ad_exit(1);
00124    }
00125 
00126    int i=0;
00127 
00128 //   char * field = (char *) new[size_t(MAX_FIELD_LENGTH+1)];
00129    char * field = new char[size_t(MAX_FIELD_LENGTH+1)];
00130    int count=0;
00131    do
00132    {
00133      infile >> field;
00134      if (infile.good())
00135      {
00136        count++;
00137      }
00138      else
00139      {
00140        if (!infile.eof())
00141        {
00142          cerr << "Error reading file " << filename << " in dvector constructor "
00143            << "dvector::dvector(char * filename)\n";
00144          cerr << "Error appears to have occurred at element"
00145           << count+1 << endl;
00146          cerr << "Stream state is " << infile.rdstate() << endl;
00147          ad_exit(1);
00148        }
00149      }
00150    }
00151    while (!infile.eof());
00152 
00153    infile.clear();
00154    infile.seekg(0,ios::beg);
00155 
00156    allocate(1,count);
00157 
00158    #ifdef DIAG
00159      cout << "Created a ncopies with address " << _farptr_tolong(ncopies)<<"\n";
00160      cout << "Created a dvector with address " << _farptr_tolong(v) <<"\n";
00161    #endif
00162 #ifndef OPT_LIB
00163   assert(size() > 0);
00164 #endif
00165    if ((va = arr_new((unsigned int)size()))==0 )
00166    {
00167      cerr << " Error trying to allocate memory for dvector\n";
00168      ad_exit(21);
00169    }
00170    char* err_ptr;
00171    for (i=1;i<=count;i++)
00172    {
00173      infile >> field;
00174      elem(i)=strtod(field,&err_ptr); // increment column counter
00175 
00176      if (isalpha((unsigned char)err_ptr[0]))
00177      {
00178        cerr << "Error decoding field " << filename
00179          << " in dmatrix::dmatrix(char * filename) " << "\n";
00180        cerr << "Error occurred at element " << count << "\n";
00181        cerr << "Offending characters start with "
00182            << err_ptr[0]
00183            << err_ptr[1]
00184            << err_ptr[2]
00185            << err_ptr[3] << "\n";
00186        ad_exit(1);
00187      }
00188      if (value(elem(i))== HUGE_VAL ||value(elem(i))== -HUGE_VAL)
00189      {
00190        cerr << "Overflow Error decoding field " << filename
00191            << " in dmatrix::dmatrix(char * filename) " << "\n";
00192        cerr << "Error occurred at element " << count << "\n";
00193        ad_exit(1);
00194      }
00195    }
00196    delete[] field;
00197    field = 0;
00198  }
00199  delete [] t;
00200  t = 0;
00201 }