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