ADMB Documentation  11.1.2503
 All Classes Files Functions Variables Typedefs Friends Defines
dvect10.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: dvect10.cpp 2305 2014-09-05 23:50:13Z 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 #ifdef __SUN__
00023    #include <iostream.h>
00024   #include <strstream.h>
00025   #define __USE_IOSTREAM__
00026 #endif
00027 
00028 #ifdef __NDPX__
00029    #include <iostream.h>
00030    #include <sstream.h>
00031 #endif
00032 
00033 #include <string.h>
00034 #include <ctype.h>
00035 
00036 #include <sstream>
00037 using std::istringstream;
00038 
00039 #include <cassert>
00040 #include <climits>
00041 
00042 const int MAX_FIELD_LENGTH = 500;
00043 
00048 void dvector::fill(const char * s)
00049 {
00050   const size_t len = strlen(s);
00051   assert(len <= INT_MAX);
00052   const int n = (int)len;
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 dvector::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 dvector::fill(const char * s)\n";
00093     cerr << s << "\n";
00094     ad_exit(1);
00095   }
00096 
00097   if (lbraces ==0)
00098   {
00099     cerr << "Missing braces { ... } in dvector::fill(const char * s)\n";
00100     cerr << s << "\n";
00101     ad_exit(1);
00102   }
00103 
00104   if (lbraces == 1)
00105   {
00106     int nch = commas + 1;
00107 
00108     if (nch != size())
00109     {
00110       if (nch < size())
00111       {
00112         cerr << "Not enough elements to fill vector in "
00113         "dvector::fill(const char * s)\n";
00114         cerr << s << "\n";
00115         ad_exit(1);
00116       }
00117       else
00118       {
00119         cerr << "Too many elements for size of vector in "
00120         "dvector::fill(const char * s)\n";
00121         cerr << s << "\n";
00122         ad_exit(1);
00123       }
00124     }
00125     istringstream ss(t);
00126 
00127 //   char * field = (char *) new[size_t(MAX_FIELD_LENGTH+1)];
00128    char * field = new char[size_t(MAX_FIELD_LENGTH+1)];
00129    char * err_ptr;
00130 
00131    for (int i=indexmin();i<=indexmax();i++)
00132    {
00133      ss >> field;
00134      elem(i)=strtod(field,&err_ptr); // increment column counter
00135 
00136      if (isalpha(err_ptr[0]))
00137      {
00138        cerr << "Error decoding field "
00139          << " in dvector::dvector(char * filename) " << "\n";
00140        cerr << "Error occurred at element " << i << "\n";
00141        cerr << "Offending characters start with "
00142            << field << endl;
00143        ad_exit(1);
00144      }
00145 
00146      if (elem(i)== HUGE_VAL ||elem(i)== -HUGE_VAL)
00147      {
00148        cerr << "Overflow Error decoding field "
00149            " in dvector::dvector(char * ) " << "\n";
00150        cerr << "Error occurred at element " << i << "\n";
00151        cerr << "Offending characters start with "
00152            << field << endl;
00153        ad_exit(1);
00154      }
00155    }
00156    delete[] field;
00157    field = 0;
00158 
00159    delete[] t;
00160    t = 0;
00161   }
00162 }