ADMB Documentation  11.1.2504
 All Classes Files Functions Variables Typedefs Friends Defines
dmat_io2.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: dmat_io2.cpp 2395 2014-09-20 08:07:58Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #define __USE_IOSTREAM__
00012 
00013 #include "fvar.hpp"
00014 
00015 #if defined(__TURBOC__)
00016   #pragma hdrstop
00017   #include <iostream.h>
00018   #include <iomanip.h>
00019   #include <fstream.h>
00020   #include <strstrea.h>
00021 #endif
00022 
00023 #ifdef __SUN__
00024   #include <iostream.h>
00025   #include <iomanip.h>
00026   #include <fstream.h>
00027   #include <limits.h>
00028   #include <strstream.h>
00029 #endif
00030 
00031 #ifdef __ZTC__
00032   #include <iostream.hpp>
00033   #if (__ZTC__ < 0x310)
00034     #include <sstream.hpp>
00035   #else
00036     #include <strstream.h>
00037   #endif
00038 #endif
00039 
00040 #ifdef __NDPX__
00041   #include <iostream.h>
00042   #include <sstream.h>
00043 #endif
00044 
00045 #include <string.h>
00046 #include <ctype.h>
00047 
00048 #include <sstream>
00049 using std::istringstream;
00050 
00051 #include <cassert>
00052 #include <climits>
00053 
00054 int mystrlen(const char* line);
00055 
00056 const unsigned int MAX_LINE_LENGTH = 10000;
00057 const int MAX_FIELD_LENGTH = 500;
00058 const int MAX_NUMBER_COLUMNS = 6550;
00059 const int MAX_NUMBER_ROWS = 6550;
00060 
00061 int get_non_blank_line(const ifstream& infile,char * & line,
00062    const unsigned int& line_length);
00063 
00068  struct dvec_ptr_ptr
00069  {
00070    void ** m;
00071  };
00072 
00073 const int MAXROWS = 5050;
00074 
00079 dmatrix::dmatrix(char* s)
00080 {
00081   int i;
00082   #ifdef DIAG
00083     myheapcheck("Entering dmatrix( char * s)" );
00084   #endif
00085 
00086   const size_t len = strlen(s);
00087   assert(len <= INT_MAX);
00088   const int n = (int)len;
00089   int braces = 0;
00090   int nrow = 0;
00091   int ncol = 0;
00092 
00093   ivector columns(1, MAXROWS);
00094   ivector k1(1, MAXROWS);
00095   ivector k2(1, MAXROWS);
00096 
00097   for (int k = 0; k < n; k++)
00098   {
00099     if (s[k] == '{')
00100     {
00101       braces ++;
00102       if (braces != 1)
00103       {
00104         cerr << "Unbalanced braces in dmatrix::dmatrix( char * s)\n";
00105         cerr << "at character " << k << "\n";
00106         ad_exit(1);
00107       }
00108       ncol = 1;
00109       k1[nrow+1] = k;
00110     }
00111     else if (s[k] == '}')
00112     {
00113       braces --;
00114       if (braces != 0)
00115       {
00116         cerr << "Unbalanced braces in dmatrix::dmatrix( char * s)\n";
00117         cerr << "at character " << k << "\n";
00118         ad_exit(1);
00119       }
00120       k2[nrow+1] = k;
00121       nrow ++;
00122       if (nrow > MAXROWS)
00123       {
00124         cerr << "Too many rows in dmatrix::dmatrix( char * s)\n";
00125         ad_exit(1);
00126       }
00127       columns[nrow] = ncol;
00128     }
00129     else if (s[k] == ',')
00130     {
00131       if (braces != 0)
00132       {
00133         ncol++;
00134       }
00135     }
00136   }
00137 
00138   if (braces != 0)
00139   {
00140     cerr << "Unbalanced braces in dmatrix::dmatrix(char * s)\n";
00141     cerr << s << "\n";
00142     ad_exit(1);
00143   }
00144 
00145   if (nrow > 0)
00146   {
00147     ivector ub(1,nrow);
00148     ivector lb(1,nrow);
00149     for (i=1; i<=nrow; i++)
00150     {
00151        ub[i] = columns[i];
00152        lb[i] = 1;
00153     }
00154     index_min=1;
00155     index_max=nrow;
00156     int rs=rowsize();
00157     if ( (m = new dvector [rs]) == 0)
00158     {
00159       cerr << " Error allocating memory in dmatrix contructor\n";
00160       ad_exit(21);
00161     }
00162     if ( (shape = new mat_shapex(m))== 0)
00163     {
00164       cerr << " Error allocating memory in dmatrix contructor\n";
00165       ad_exit(21);
00166     }
00167 
00168 
00169     #ifdef DIAG
00170       cerr << "Created a dmatrix with adress "<< farptr_tolong(m)<<"\n";
00171     #endif
00172 
00173     m -= rowmin();
00174 
00175     //char * t = (char*) new[strlen(s)+1];
00176     char *t = new char[strlen(s)+1];
00177     for (i=rowmin(); i<=rowmax(); i++)
00178     {
00179       for (int k = k1[i]; k <= k2[i]; k++)
00180       {
00181         t[k-k1[i]] = s[k];
00182       }
00183       t[k2[i]-k1[i]+1] = '\0';
00184 
00185       m[i].allocate(t);
00186     }
00187     delete[] t;
00188     t = 0;
00189   }
00190   else // no rows implies s is a file name
00191   {
00192     char * filename = s;
00193     ifstream infile(filename);
00194     if (!infile)
00195     {
00196        cerr << "Error opening file " << filename << " in dmatrix constructor "
00197             << "dmatrix::dmatrix(char * filename)\n";
00198        ad_exit(1);
00199     }
00200     char *line = new char [MAX_LINE_LENGTH+2];
00201     char *field = new char [MAX_FIELD_LENGTH+1];
00202 
00203     int i=0;
00204     ivector nc(1,MAX_NUMBER_ROWS);
00205 
00206     //while ( (infile.getline(line,MAX_LINE_LENGTH)).good() )
00207     while ( get_non_blank_line(infile,line,MAX_LINE_LENGTH) )
00208     {
00209       strcat(line," ");
00210      // increment row counter
00211       if ( i++ > MAX_NUMBER_ROWS)
00212       {
00213         cerr << " MAX_NUMBER_ROWS exceeded in "
00214                 " dmatrix::dmatrix(char * filename)\n";
00215         ad_exit(21);
00216       }
00217 
00218       int j=0;              // j counts columns
00219       istringstream f(line);
00220       while ( (f >> field).good() )
00221       {
00222        //char * err_ptr;
00223        // increment row counter
00224        if ( ++j > MAX_NUMBER_COLUMNS)
00225        {
00226          cerr << " MAX_NUMBER_COLUMNS exceeded in "
00227                  " dmatrix::dmatrix(char * filename)\n";
00228          ad_exit(21);
00229        }
00230      }
00231      // Need to check error status f
00232      nc[i]=j;
00233    }
00234    int nr=i;
00235    if (nr == 0)
00236    {
00237      cerr << "Error in dmatrix constructor There doesn't seem to be any data\n"
00238       << "in file " << filename
00239       << " caled in dmatrix::dmatrix(char * filename)\n";
00240       ad_exit(1);
00241    }
00242 
00243    infile.clear();
00244    infile.seekg(0,ios::beg);
00245 
00246    ivector index_up(1,nr);
00247    ivector index_down(1,nr);
00248      int One=1;
00249      int Zero=0;
00250    index_down.fill_seqadd(One,Zero);
00251 
00252    for (i=1;i<=nr;i++)
00253    {
00254      index_up[i]=nc[i];
00255    }
00256    index_min=1;
00257    index_max=nr;
00258 
00259    int rs=rowsize();
00260    if ( (m = new dvector [rs]) == 0)
00261    {
00262      cerr << " Error allocating memory in dmatrix contructor\n";
00263      ad_exit(21);
00264    }
00265    if ( (shape = new mat_shapex(m))== 0)
00266    {
00267      cerr << " Error allocating memory in dmatrix contructor\n";
00268      ad_exit(21);
00269    }
00270 
00271    #ifdef DIAG
00272      cerr << "Created a dmatrix with adress "<< farptr_tolong(m)<<"\n";
00273    #endif
00274 
00275    m -= rowmin();
00276 
00277    for (i=rowmin(); i<=rowmax(); i++)
00278    {
00279      m[i].allocate(index_down[i],index_up[i]);
00280      #ifdef DIAG
00281        cerr << "Created a dvector with address "<< farptr_tolong(*(m+i))<<"\n";
00282      #endif
00283    }
00284    #ifdef DIAG
00285      myheapcheck("Leaving dmatrix(nrl,nrh,ncl,nch)" );
00286    #endif
00287 
00288    i=0;
00289    while (get_non_blank_line(infile,line,MAX_LINE_LENGTH) )
00290    {
00291      strcat(line," ");
00292      // increment row counter
00293      i++;
00294 
00295      int j=0;              // j counts columns
00296      istringstream f(line);
00297      while ( (f >> field).good() )
00298      {
00299        char * err_ptr;
00300        // increment row counter
00301        j++;
00302        elem(i,j)=strtod(field,&err_ptr); // increment column counter
00303 
00304        if (isalpha(err_ptr[0]))
00305        {
00306          cerr << "Error decoding field " << filename
00307                 << " in dmatrix::dmatrix(char * filename) " << "\n";
00308          cerr << "Error occurred in line " << i << " at field " << j << "\n";
00309          cerr << "Offending characters start with "
00310                 << err_ptr[0]
00311                 << err_ptr[1]
00312                 << err_ptr[2]
00313          << err_ptr[3] << "\n";
00314          ad_exit(1);
00315        }
00316 
00317        if (elem(i,j) == HUGE_VAL ||elem(i,j) == -HUGE_VAL)
00318        {
00319          cerr << "Overflow Error decoding field " << filename
00320                 << " in dmatrix::dmatrix(char * filename) " << "\n";
00321          cerr << "Error occurred in line " << i << " at field " << j << "\n";
00322          cerr << "Offending characters start with "
00323               << err_ptr[0]
00324               << err_ptr[1]
00325               << err_ptr[2]
00326               << err_ptr[3] << "\n";
00327          ad_exit(1);
00328        }
00329      }
00330      // Need to check error status f
00331    }
00332    delete[] line;
00333    line = 0;
00334    delete[] field;
00335    field = 0;
00336   }
00337 }
00338 
00343 int get_non_blank_line(const ifstream& _infile,char * & line,
00344      const unsigned int& line_length)
00345    {
00346      ifstream& infile=(ifstream&) _infile;
00347      char ch;
00348      int tmp;
00349      while ( (tmp=(infile.get(line,line_length)).good()) !=0)
00350      {
00351        //cout << line << endl;
00352        infile >> ch; // get rid of the terminating character
00353        if (ch != '\0') infile.putback(ch); // If character is not null
00354                                            // put if back
00355        int length=mystrlen(line);
00356        if (length == -1)
00357        {
00358          cerr << "Error computing input line length field reading file\n";
00359          ad_exit(1);
00360        }
00361 
00362        for (int i=0;i<length;i++)
00363        {
00364          if (line[i] != ' ')
00365          {
00366            return tmp;
00367          }
00368        }
00369      }
00370      return tmp;
00371    }
00372 
00377 int mystrlen(const char* line)
00378 {
00379   int ii = 0;
00380   while(ii < INT_MAX)
00381   {
00382     if (line[ii]=='\0') return ii;
00383     ii++;
00384   }
00385   return -1;
00386 }