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