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