Go to the documentation of this file.00001
00002
00003
00004
00005
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 for (int k = 0; k < n; k++)
00064 {
00065 if (s[k] == '{')
00066 {
00067 lbraces ++;
00068 t[k] = ' ';
00069 }
00070 else if (s[k] == '}')
00071 {
00072 rbraces ++;
00073 t[k] = ' ';
00074 }
00075 else if (s[k] == ',')
00076 {
00077 commas ++;
00078 t[k] = ' ';
00079 }
00080 else
00081 {
00082 t[k] = s[k];
00083 }
00084 }
00085
00086 if (lbraces == 1 && rbraces == 1)
00087 {
00088 int nch = commas + 1;
00089
00090 if (nch != size())
00091 {
00092 if (nch < size())
00093 {
00094 cerr << "Not enough elements to fill vector in "
00095 "dvar_vector::fill(const char * s)\n";
00096 cerr << s << "\n";
00097 ad_exit(1);
00098 }
00099 else
00100 {
00101 cerr << "Too many elements for size of vector in "
00102 "dvar_vector::fill(const char * s)\n";
00103 cerr << s << "\n";
00104 ad_exit(1);
00105 }
00106 }
00107 istringstream ss(t);
00108
00109
00110 char* field = new char[size_t(MAX_FIELD_LENGTH+1)];
00111 char* err_ptr = NULL;
00112
00113 for (int i=indexmin();i<=indexmax();i++)
00114 {
00115 ss >> field;
00116 elem(i)=strtod(field,&err_ptr);
00117
00118 if (isalpha((unsigned char)err_ptr[0]))
00119 {
00120 cerr << "Error decoding field "
00121 << " in dmatrix::dmatrix(char * filename) " << "\n";
00122 cerr << "Error occurred at element " << i << "\n";
00123 cerr << "Offending characters start with "
00124 << err_ptr[0]
00125 << err_ptr[1]
00126 << err_ptr[2]
00127 << err_ptr[3] << "\n";
00128 ad_exit(1);
00129 }
00130 if (elem(i) == HUGE_VAL || elem(i) == -HUGE_VAL)
00131 {
00132 cerr << "Overflow Error decoding field "
00133 " in dmatrix::dmatrix(char * ) " << "\n";
00134 cerr << "Error occurred at element " << i << "\n";
00135 ad_exit(1);
00136 }
00137 }
00138 delete [] field;
00139 field = NULL;
00140 }
00141 else
00142 {
00143 delete [] t;
00144 t = NULL;
00145
00146 if (lbraces != rbraces)
00147 {
00148 cerr << "Unbalanced braces in dvar_vector::fill(const char * s)\n";
00149 cerr << s << "\n";
00150 ad_exit(1);
00151 }
00152 if (lbraces > 1)
00153 {
00154 cerr << "Only one level of braces allowed in "
00155 "dvar_vector::fill(const char * s)\n";
00156 cerr << s << "\n";
00157 ad_exit(1);
00158 }
00159 if (lbraces == 0)
00160 {
00161 cerr << "Missing braces { ... } in dvar_vector::fill(const char * s)\n";
00162 cerr << s << "\n";
00163 ad_exit(1);
00164 }
00165 }
00166 delete [] t;
00167 t = NULL;
00168 }