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