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