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