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