ADMB Documentation  11.1.2274
 All Classes Files Functions Variables Typedefs Friends Defines
fvar_a10.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: fvar_a10.cpp 2218 2014-07-28 16:52:21Z 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__)
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 int MAX_FIELD_LENGTH = 500;
00045 
00050 void dvar_vector::fill(const char * s)
00051 {
00052   int n = strlen(s);
00053   int lbraces = 0;
00054   int rbraces = 0;
00055   int commas  = 0;
00056 
00057   char *t = new char[n];
00058 
00059   int k;
00060   for (k = 0; k < n; k++)
00061   {
00062     if (s[k] == '{')
00063     {
00064       lbraces ++;
00065       t[k] = ' ';
00066     }
00067     else if (s[k] == '}')
00068     {
00069       rbraces ++;
00070       t[k] = ' ';
00071     }
00072     else if (s[k] == ',')
00073     {
00074       commas ++;
00075       t[k] = ' ';
00076     }
00077     else
00078     {
00079       t[k] = s[k];
00080     }
00081   }
00082 
00083   if (lbraces != rbraces)
00084   {
00085     cerr << "Unbalanced braces in dvar_vector::fill(const char * s)\n";
00086     cerr << s << "\n";
00087     ad_exit(1);
00088   }
00089 
00090   if (lbraces > 1)
00091   {
00092     cerr << "Only one level of braces allowed in "
00093     "dvar_vector::fill(const char * s)\n";
00094     cerr << s << "\n";
00095     ad_exit(1);
00096   }
00097 
00098   if (lbraces ==0)
00099   {
00100     cerr << "Missing braces { ... } in dvar_vector::fill(const char * s)\n";
00101     cerr << s << "\n";
00102     ad_exit(1);
00103   }
00104 
00105   if (lbraces == 1)
00106   {
00107     int nch = commas + 1;
00108 
00109     if (nch != size())
00110     {
00111       if (nch < size())
00112       {
00113         cerr << "Not enough elements to fill vector in "
00114         "dvar_vector::fill(const char * s)\n";
00115         cerr << s << "\n";
00116         ad_exit(1);
00117       }
00118       else
00119       {
00120         cerr << "Too many elements for size of vector in "
00121         "dvar_vector::fill(const char * s)\n";
00122         cerr << s << "\n";
00123         ad_exit(1);
00124       }
00125     }
00126     istringstream ss(t);
00127 
00128 //   char * field = (char *) new[size_t(MAX_FIELD_LENGTH+1)];
00129    char * field = new char[size_t(MAX_FIELD_LENGTH+1)];
00130    char * err_ptr;
00131 
00132    for (int i=indexmin();i<=indexmax();i++)
00133    {
00134      ss >> field;
00135      elem(i)=strtod(field,&err_ptr); // increment column counter
00136 
00137      if (isalpha(err_ptr[0]))
00138      {
00139        cerr << "Error decoding field "
00140          << " in dmatrix::dmatrix(char * filename) " << "\n";
00141        cerr << "Error occurred at element " << i << "\n";
00142        cerr << "Offending characters start with "
00143            << err_ptr[0]
00144            << err_ptr[1]
00145            << err_ptr[2]
00146            << err_ptr[3] << "\n";
00147        ad_exit(1);
00148      }
00149      if (elem(i) == HUGE_VAL || elem(i) == -HUGE_VAL)
00150      {
00151        cerr << "Overflow Error decoding field "
00152            " in dmatrix::dmatrix(char * ) " << "\n";
00153        cerr << "Error occurred at element " << i << "\n";
00154        ad_exit(1);
00155      }
00156    }
00157    delete[] field;
00158    field = 0;
00159 
00160    delete[] t;
00161    t = 0;
00162   }
00163 }