ADMB Documentation  11.1.2382
 All Classes Files Functions Variables Typedefs Friends Defines
dmat_io2.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: dmat_io2.cpp 2303 2014-09-05 22:59:41Z 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   int k;
00098   for (k = 0; k < n; k++)
00099   {
00100     if (s[k] == '{')
00101     {
00102       braces ++;
00103       if (braces != 1)
00104       {
00105         cerr << "Unbalanced braces in dmatrix::dmatrix( char * s)\n";
00106         cerr << "at character " << k << "\n";
00107         ad_exit(1);
00108       }
00109       ncol = 1;
00110       k1[nrow+1] = k;
00111     }
00112     else if (s[k] == '}')
00113     {
00114       braces --;
00115       if (braces != 0)
00116       {
00117         cerr << "Unbalanced braces in dmatrix::dmatrix( char * s)\n";
00118         cerr << "at character " << k << "\n";
00119         ad_exit(1);
00120       }
00121       k2[nrow+1] = k;
00122       nrow ++;
00123       if (nrow > MAXROWS)
00124       {
00125         cerr << "Too many rows in dmatrix::dmatrix( char * s)\n";
00126         ad_exit(1);
00127       }
00128       columns[nrow] = ncol;
00129     }
00130     else if (s[k] == ',')
00131     {
00132       if (braces != 0)
00133       {
00134         ncol++;
00135       }
00136     }
00137   }
00138 
00139   if (braces != 0)
00140   {
00141     cerr << "Unbalanced braces in dmatrix::dmatrix(char * s)\n";
00142     cerr << s << "\n";
00143     ad_exit(1);
00144   }
00145 
00146   if (nrow > 0)
00147   {
00148     ivector ub(1,nrow);
00149     ivector lb(1,nrow);
00150     for (i=1; i<=nrow; i++)
00151     {
00152        ub[i] = columns[i];
00153        lb[i] = 1;
00154     }
00155     index_min=1;
00156     index_max=nrow;
00157     int rs=rowsize();
00158     if ( (m = new dvector [rs]) == 0)
00159     {
00160       cerr << " Error allocating memory in dmatrix contructor\n";
00161       ad_exit(21);
00162     }
00163     if ( (shape = new mat_shapex(m))== 0)
00164     {
00165       cerr << " Error allocating memory in dmatrix contructor\n";
00166       ad_exit(21);
00167     }
00168 
00169 
00170     #ifdef DIAG
00171       cerr << "Created a dmatrix with adress "<< farptr_tolong(m)<<"\n";
00172     #endif
00173 
00174     m -= rowmin();
00175 
00176     k = 0;
00177     //char * t = (char*) new[strlen(s)+1];
00178     char *t = new char[strlen(s)+1];
00179     for (i=rowmin(); i<=rowmax(); i++)
00180     {
00181       for (k = k1[i]; k <= k2[i]; k++)
00182       {
00183         t[k-k1[i]] = s[k];
00184       }
00185       t[k2[i]-k1[i]+1] = '\0';
00186 
00187       m[i].allocate(t);
00188     }
00189     delete[] t;
00190     t = 0;
00191   }
00192   else // no rows implies s is a file name
00193   {
00194     char * filename = s;
00195     ifstream infile(filename);
00196     if (!infile)
00197     {
00198        cerr << "Error opening file " << filename << " in dmatrix constructor "
00199             << "dmatrix::dmatrix(char * filename)\n";
00200        ad_exit(1);
00201     }
00202     char *line = new char [MAX_LINE_LENGTH+2];
00203     char *field = new char [MAX_FIELD_LENGTH+1];
00204 
00205     int i=0;
00206     ivector nc(1,MAX_NUMBER_ROWS);
00207 
00208     //while ( (infile.getline(line,MAX_LINE_LENGTH)).good() )
00209     while ( get_non_blank_line(infile,line,MAX_LINE_LENGTH) )
00210     {
00211       strcat(line," ");
00212      // increment row counter
00213       if ( i++ > MAX_NUMBER_ROWS)
00214       {
00215         cerr << " MAX_NUMBER_ROWS exceeded in "
00216                 " dmatrix::dmatrix(char * filename)\n";
00217         ad_exit(21);
00218       }
00219 
00220       int j=0;              // j counts columns
00221       istringstream f(line);
00222       while ( (f >> field).good() )
00223       {
00224        //char * err_ptr;
00225        // increment row counter
00226        if ( ++j > MAX_NUMBER_COLUMNS)
00227        {
00228          cerr << " MAX_NUMBER_COLUMNS exceeded in "
00229                  " dmatrix::dmatrix(char * filename)\n";
00230          ad_exit(21);
00231        }
00232      }
00233      // Need to check error status f
00234      nc[i]=j;
00235    }
00236    int nr=i;
00237    if (nr == 0)
00238    {
00239      cerr << "Error in dmatrix constructor There doesn't seem to be any data\n"
00240       << "in file " << filename
00241       << " caled in dmatrix::dmatrix(char * filename)\n";
00242       ad_exit(1);
00243    }
00244 
00245    infile.clear();
00246    infile.seekg(0,ios::beg);
00247 
00248    ivector index_up(1,nr);
00249    ivector index_down(1,nr);
00250      int One=1;
00251      int Zero=0;
00252    index_down.fill_seqadd(One,Zero);
00253 
00254    for (i=1;i<=nr;i++)
00255    {
00256      index_up[i]=nc[i];
00257    }
00258    index_min=1;
00259    index_max=nr;
00260 
00261    int rs=rowsize();
00262    if ( (m = new dvector [rs]) == 0)
00263    {
00264      cerr << " Error allocating memory in dmatrix contructor\n";
00265      ad_exit(21);
00266    }
00267    if ( (shape = new mat_shapex(m))== 0)
00268    {
00269      cerr << " Error allocating memory in dmatrix contructor\n";
00270      ad_exit(21);
00271    }
00272 
00273    #ifdef DIAG
00274      cerr << "Created a dmatrix with adress "<< farptr_tolong(m)<<"\n";
00275    #endif
00276 
00277    m -= rowmin();
00278 
00279    for (i=rowmin(); i<=rowmax(); i++)
00280    {
00281      m[i].allocate(index_down[i],index_up[i]);
00282      #ifdef DIAG
00283        cerr << "Created a dvector with address "<< farptr_tolong(*(m+i))<<"\n";
00284      #endif
00285    }
00286    #ifdef DIAG
00287      myheapcheck("Leaving dmatrix(nrl,nrh,ncl,nch)" );
00288    #endif
00289 
00290    i=0;
00291    while (get_non_blank_line(infile,line,MAX_LINE_LENGTH) )
00292    {
00293      strcat(line," ");
00294      // increment row counter
00295      i++;
00296 
00297      int j=0;              // j counts columns
00298      istringstream f(line);
00299      while ( (f >> field).good() )
00300      {
00301        char * err_ptr;
00302        // increment row counter
00303        j++;
00304        elem(i,j)=strtod(field,&err_ptr); // increment column counter
00305 
00306        if (isalpha(err_ptr[0]))
00307        {
00308          cerr << "Error decoding field " << filename
00309                 << " in dmatrix::dmatrix(char * filename) " << "\n";
00310          cerr << "Error occurred in line " << i << " at field " << j << "\n";
00311          cerr << "Offending characters start with "
00312                 << err_ptr[0]
00313                 << err_ptr[1]
00314                 << err_ptr[2]
00315          << err_ptr[3] << "\n";
00316          ad_exit(1);
00317        }
00318 
00319        if (elem(i,j) == HUGE_VAL ||elem(i,j) == -HUGE_VAL)
00320        {
00321          cerr << "Overflow Error decoding field " << filename
00322                 << " in dmatrix::dmatrix(char * filename) " << "\n";
00323          cerr << "Error occurred in line " << i << " at field " << j << "\n";
00324          cerr << "Offending characters start with "
00325               << err_ptr[0]
00326               << err_ptr[1]
00327               << err_ptr[2]
00328               << err_ptr[3] << "\n";
00329          ad_exit(1);
00330        }
00331      }
00332      // Need to check error status f
00333    }
00334    delete[] line;
00335    line = 0;
00336    delete[] field;
00337    field = 0;
00338   }
00339 }
00340 
00345 int get_non_blank_line(const ifstream& _infile,char * & line,
00346      const unsigned int& line_length)
00347    {
00348      ifstream& infile=(ifstream&) _infile;
00349      char ch;
00350      int tmp;
00351      while ( (tmp=(infile.get(line,line_length)).good()) !=0)
00352      {
00353        //cout << line << endl;
00354        infile >> ch; // get rid of the terminating character
00355        if (ch != '\0') infile.putback(ch); // If character is not null
00356                                            // put if back
00357        int length=mystrlen(line);
00358        if (length == -1)
00359        {
00360          cerr << "Error computing input line length field reading file\n";
00361          ad_exit(1);
00362        }
00363 
00364        for (int i=0;i<length;i++)
00365        {
00366          if (line[i] != ' ')
00367          {
00368            return tmp;
00369          }
00370        }
00371      }
00372      return tmp;
00373    }
00374 
00379 int mystrlen(const char* line)
00380 {
00381   int ii = 0;
00382   while(ii < INT_MAX)
00383   {
00384     if (line[ii]=='\0') return ii;
00385     ii++;
00386   }
00387   return -1;
00388 }