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