ADMB Documentation  11.1.1897
 All Classes Files Functions Variables Typedefs Friends Defines
fvar_a10.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: fvar_a10.cpp 1877 2014-04-08 23:55:06Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #include "fvar.hpp"
00012 
00013 #ifdef __ZTC__
00014   #include <iostream.hpp>
00015   #if (__ZTC__ < 0x310)
00016     #include <sstream.hpp>
00017   #else
00018     #include <strstream.h>
00019   #endif
00020 #endif
00021 
00022 #if defined(__TURBOC__) && !defined(__linux__)
00023    #include <iostream.h>
00024    #include <strstrea.h>
00025 #endif
00026 
00027 #ifdef __SUN__
00028    #include <iostream.h>
00029   #include <strstream.h>
00030   #define __USE_IOSTREAM__
00031 #endif
00032 
00033 #ifdef __NDPX__
00034    #include <iostream.h>
00035    #include <sstream.h>
00036 #endif
00037 
00038 #include <string.h>
00039 #include <ctype.h>
00040 
00041 #include <sstream>
00042 using std::istringstream;
00043 
00044 const unsigned int MAX_LINE_LENGTH = 10000;
00045 const int MAX_FIELD_LENGTH = 500;
00046 const int MAX_NUMBER_COLUMNS = 6550;
00047 const int MAX_NUMBER_ROWS = 6550;
00048 
00053 void dvar_vector::fill(const char * s)
00054 {
00055   int n = strlen(s);
00056   int lbraces = 0;
00057   int rbraces = 0;
00058   int commas  = 0;
00059 
00060   char *t = new char[n];
00061 
00062   int k;
00063   for (k = 0; k < n; k++)
00064   {
00065     if (s[k] == '{')
00066     {
00067       lbraces ++;
00068       t[k] = ' ';
00069     }
00070     else if (s[k] == '}')
00071     {
00072       rbraces ++;
00073       t[k] = ' ';
00074     }
00075     else if (s[k] == ',')
00076     {
00077       commas ++;
00078       t[k] = ' ';
00079     }
00080     else
00081     {
00082       t[k] = s[k];
00083     }
00084   }
00085 
00086   if (lbraces != rbraces)
00087   {
00088     cerr << "Unbalanced braces in dvar_vector::fill(const char * s)\n";
00089     cerr << s << "\n";
00090     ad_exit(1);
00091   }
00092 
00093   if (lbraces > 1)
00094   {
00095     cerr << "Only one level of braces allowed in "
00096     "dvar_vector::fill(const char * s)\n";
00097     cerr << s << "\n";
00098     ad_exit(1);
00099   }
00100 
00101   if (lbraces ==0)
00102   {
00103     cerr << "Missing braces { ... } in dvar_vector::fill(const char * s)\n";
00104     cerr << s << "\n";
00105     ad_exit(1);
00106   }
00107 
00108   if (lbraces == 1)
00109   {
00110     int nch = commas + 1;
00111 
00112     if (nch != size())
00113     {
00114       if (nch < size())
00115       {
00116         cerr << "Not enough elements to fill vector in "
00117         "dvar_vector::fill(const char * s)\n";
00118         cerr << s << "\n";
00119         ad_exit(1);
00120       }
00121       else
00122       {
00123         cerr << "Too many elements for size of vector in "
00124         "dvar_vector::fill(const char * s)\n";
00125         cerr << s << "\n";
00126         ad_exit(1);
00127       }
00128     }
00129     istringstream ss(t);
00130 
00131 //   char * field = (char *) new[size_t(MAX_FIELD_LENGTH+1)];
00132    char * field = new char[size_t(MAX_FIELD_LENGTH+1)];
00133    char * err_ptr;
00134 
00135    for (int i=indexmin();i<=indexmax();i++)
00136    {
00137      ss >> field;
00138      elem(i)=strtod(field,&err_ptr); // increment column counter
00139 
00140      if (isalpha(err_ptr[0]))
00141      {
00142        cerr << "Error decoding field "
00143          << " in dmatrix::dmatrix(char * filename) " << "\n";
00144        cerr << "Error occurred at element " << i << "\n";
00145        cerr << "Offending characters start with "
00146            << err_ptr[0]
00147            << err_ptr[1]
00148            << err_ptr[2]
00149            << err_ptr[3] << "\n";
00150        ad_exit(1);
00151      }
00152      if (elem(i) == HUGE_VAL || elem(i) == -HUGE_VAL)
00153      {
00154        cerr << "Overflow Error decoding field "
00155            " in dmatrix::dmatrix(char * ) " << "\n";
00156        cerr << "Error occurred at element " << i << "\n";
00157        ad_exit(1);
00158      }
00159    }
00160    delete[] field;
00161    field = 0;
00162 
00163    delete[] t;
00164    t = 0;
00165   }
00166 }