ADMB Documentation  11.1.2503
 All Classes Files Functions Variables Typedefs Friends Defines
dvec_io1.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: dvec_io1.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    #include <iostream.h>
00015    #include <strstrea.h>
00016 #endif
00017 #ifdef __NDPX__
00018    #include <iostream.h>
00019    #include <sstream.h>
00020 #endif
00021 
00022 #include <string.h>
00023 #include <ctype.h>
00024 
00025 #include <sstream>
00026 using std::istringstream;
00027 
00028 const unsigned int MAX_LINE_LENGTH = 10000;
00029 const int MAX_FIELD_LENGTH = 500;
00030 const int MAX_NUMBER_COLUMNS = 6550;
00031 const int MAX_NUMBER_ROWS = 6550;
00032 
00033 int get_non_blank_line(const ifstream& infile, char * & line,
00034    const unsigned int& line_length);
00035 
00040  struct dvec_ptr_ptr
00041  {
00042    void ** m;
00043  };
00044 
00049  dvector::dvector(char * filename, const int& column)
00050  {
00051    ifstream infile(filename);
00052    if (!infile)
00053    {
00054      cerr << "Error opening file " << filename << " in dmatrix constructor "
00055       << "dmatrix::dmatrix(char * filename)\n";
00056       ad_exit(1);
00057    }
00058    char *line = new char[MAX_LINE_LENGTH + 2];
00059    char *field = new char[MAX_FIELD_LENGTH + 1];
00060 
00061    int i=0;
00062    ivector nc(1,MAX_NUMBER_ROWS);
00063 
00064    //while ( (infile.getline(line,MAX_LINE_LENGTH)).good() )
00065    while ( get_non_blank_line(infile,line,MAX_LINE_LENGTH) )
00066    {
00067      strcat(line," ");
00068      // increment row counter
00069      if ( i++ > MAX_NUMBER_ROWS)
00070      {
00071        cerr << " MAX_NUMBER_ROWS exceeded in "
00072                " dmatrix::dmatrix(char * filename)\n";
00073        ad_exit(21);
00074      }
00075      int j=0;              // j counts columns
00076 
00077      #ifndef __ZTC__
00078        istringstream f(line);
00079        while ( (f >> field).good() )
00080      #else
00081        while( sscanf(line,"%s",field)) // reads a field from line into field
00082      #endif
00083      {
00084        // f >> field;      // Need to derive a class so that this thing stops at
00085                            // , or maybe deals with strings
00086        //char * err_ptr;
00087        // increment row counter
00088        if ( ++j > MAX_NUMBER_COLUMNS)
00089        {
00090          cerr << " MAX_NUMBER_COLUMNS exceeded in "
00091                  " dmatrix::dmatrix(char * filename)\n";
00092          ad_exit(21);
00093        }
00094      }
00095      // Need to check error status f
00096      if (j < column)
00097      {
00098        cerr << "Error -- not enough columns in line " << i
00099         << "\n in dvector::dvector(char * filename, const int& column) "
00100            " in file:  "
00101         << filename << "\n";
00102        ad_exit(1);
00103      }
00104    }
00105    int nr=i;
00106    if (nr == 0)
00107    {
00108      cerr << "Error in dvector constructor There doesn't seem to be any data\n"
00109       << "in file:  " << filename
00110       << " called in dvector::dvector(char * filename,const const& column)\n";
00111       ad_exit(1);
00112    }
00113    infile.clear();
00114    infile.seekg(0,ios::beg);
00115 
00116    if ( (v = new double [(size_t) size()]) ==0)
00117    {
00118      cerr << " Error trying to allocate memory for dvector\n";
00119      ad_exit(21);
00120    }
00121 #if defined(THREAD_SAFE)
00122    if ( (shape=new ts_vector_shapex(1,nr,v)) == NULL)
00123 #else
00124    if ( (shape=new vector_shapex(1,nr,v)) == NULL)
00125 #endif
00126    {
00127      cerr << "Error trying to allocate memory for dvector\n";
00128      ad_exit(21);
00129    }
00130 
00131    #ifdef DIAG
00132      cout << "Created a ncopies with address " << _farptr_tolong(ncopies)
00133           <<"\n";
00134      cout << "Created a dvector with address " << _farptr_tolong(v) <<"\n";
00135    #endif
00136 
00137 /* Deprecated empty function
00138    if (sizeof(int)==sizeof(char*))
00139    {
00140 #if defined(__x86_64)
00141      if ((intptr_t)v < indexmin() * sizeof(double))
00142 #else
00143      if ( (unsigned) v < indexmin() * sizeof(double) )
00144 #endif
00145      {
00146       //cerr << "Pointer wrap in dvector(unsigned int ncl, unsigned int nch)\n";
00147       //cerr << "pointer = "<< (unsigned int) v <<
00148       //" indexmin() = "<<indexmin()<<"\n";
00149         denormalize_ptr(&v, indexmin() * sizeof(double));
00150      }
00151    }
00152 */
00153 
00154    v -= indexmin();
00155 
00156 
00157  i=0;
00158  // while ( (infile.getline(line,MAX_LINE_LENGTH)).good() )
00159  while ( get_non_blank_line(infile,line,MAX_LINE_LENGTH) )
00160  {
00161    strcat(line," ");
00162    // increment row counter
00163    i++;
00164    int j=0;              // j counts columns
00165    #ifndef __ZTC__
00166      istringstream f(line);
00167      while ( (f >> field).good() )
00168    #else
00169      while( sscanf(line,"%s",field)) // reads a field from line into field
00170    #endif
00171    {
00172      // f >> field;      // Need to derive a class so that this thing stops at
00173            // , or maybe deals with strings
00174      char * err_ptr;
00175      // increment row counter
00176      j++;
00177 
00178      if (j==column)
00179      {
00180        elem(i)=strtod(field,&err_ptr); // increment column counter
00181 
00182        if (isalpha(err_ptr[0]))
00183        {
00184          cerr << "Error decoding field " << filename
00185           << " in dmatrix::dmatrix(char * filename) " << "\n";
00186          cerr << "Error occurred in line " << i << " at field " << j << "\n";
00187          cerr << "Offending characters start with "
00188            << err_ptr[0]
00189            << err_ptr[1]
00190            << err_ptr[2]
00191            << err_ptr[3] << "\n";
00192          ad_exit(1);
00193        }
00194 
00195        if (elem(i) == HUGE_VAL || elem(i) == -HUGE_VAL)
00196        {
00197          cerr << "Overflow Error decoding field " << filename
00198                 << " in dvector::dvector(char * filename) " << "\n";
00199          cerr << "Error occurred in line " << i << " at field " << j << "\n";
00200          ad_exit(1);
00201        }
00202      }
00203    }
00204    // Need to check error status f
00205  }
00206 
00207  delete[] line;
00208  line = 0;
00209  delete[] field;
00210  field = 0;
00211 }