ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fvar_a10.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: David Fournier
5  * Copyright (c) 2008-2012 Regents of the University of California
6  */
11 #include "fvar.hpp"
12 
13 #ifdef __ZTC__
14  #include <iostream.hpp>
15  #if (__ZTC__ < 0x310)
16  #include <sstream.hpp>
17  #else
18  #include <strstream.h>
19  #endif
20 #endif
21 
22 #if defined(__TURBOC__)
23  #include <iostream.h>
24  #include <strstrea.h>
25 #endif
26 
27 #ifdef __SUN__
28  #include <iostream.h>
29  #include <strstream.h>
30  #define __USE_IOSTREAM__
31 #endif
32 
33 #ifdef __NDPX__
34  #include <iostream.h>
35  #include <sstream.h>
36 #endif
37 
38 #include <string.h>
39 #include <ctype.h>
40 
41 #ifndef OPT_LIB
42  #include <cassert>
43  #include <climits>
44 #endif
45 
46 const int MAX_FIELD_LENGTH = 500;
47 
52 void dvar_vector::fill(const char * s)
53 {
54  const size_t len = strlen(s);
55 #ifndef OPT_LIB
56  assert(len <= INT_MAX);
57 #endif
58  char *t = new char[len];
59  const int n = static_cast<int>(len);
60  int lbraces = 0;
61  int rbraces = 0;
62  unsigned int commas = 0;
63 
64  for (int k = 0; k < n; k++)
65  {
66  if (s[k] == '{')
67  {
68  lbraces ++;
69  t[k] = ' ';
70  }
71  else if (s[k] == '}')
72  {
73  rbraces ++;
74  t[k] = ' ';
75  }
76  else if (s[k] == ',')
77  {
78  commas ++;
79  t[k] = ' ';
80  }
81  else
82  {
83  t[k] = s[k];
84  }
85  }
86 
87  if (lbraces == 1 && rbraces == 1)
88  {
89  unsigned int nch = commas + 1;
90 
91  if (nch != size())
92  {
93  if (nch < size())
94  {
95  cerr << "Not enough elements to fill vector in "
96  "dvar_vector::fill(const char * s)\n";
97  cerr << s << "\n";
98  ad_exit(1);
99  }
100  else
101  {
102  cerr << "Too many elements for size of vector in "
103  "dvar_vector::fill(const char * s)\n";
104  cerr << s << "\n";
105  ad_exit(1);
106  }
107  }
108 // char * field = (char *) new[size_t(MAX_FIELD_LENGTH+1)];
109  char* field = new char[size_t(MAX_FIELD_LENGTH+1)];
110  char* err_ptr = NULL;
111 
112  size_t index = 0;
113  size_t length = strlen(t);
114  for (int i=indexmin();i<=indexmax();i++)
115  {
116  char c = t[index];
117  while (c == ' ')
118  {
119  ++index;
120  if (index >= length) break;
121 
122  c = t[index];
123  }
124  int field_index = 0;
125  while (c != ' ')
126  {
127  field[field_index] = c;
128  ++field_index;
129 
130  ++index;
131  if (index >= length) break;
132 
133  c = t[index];
134  }
135  field[field_index] = '\0';
136  elem(i)=strtod(field,&err_ptr); // increment column counter
137 
138  if (isalpha((unsigned char)err_ptr[0]))
139  {
140  cerr << "Error decoding field "
141  << " in dmatrix::dmatrix(char * filename) " << "\n";
142  cerr << "Error occurred at element " << i << "\n";
143  cerr << "Offending characters start with "
144  << err_ptr[0]
145  << err_ptr[1]
146  << err_ptr[2]
147  << err_ptr[3] << "\n";
148  ad_exit(1);
149  }
150  if (elem(i) == HUGE_VAL || elem(i) == -HUGE_VAL)
151  {
152  cerr << "Overflow Error decoding field "
153  " in dmatrix::dmatrix(char * ) " << "\n";
154  cerr << "Error occurred at element " << i << "\n";
155  ad_exit(1);
156  }
157  }
158  delete [] field;
159  field = NULL;
160  }
161  else
162  {
163  delete [] t;
164  t = NULL;
165 
166  if (lbraces != rbraces)
167  {
168  cerr << "Unbalanced braces in dvar_vector::fill(const char * s)\n";
169  cerr << s << "\n";
170  ad_exit(1);
171  }
172  if (lbraces > 1)
173  {
174  cerr << "Only one level of braces allowed in "
175  "dvar_vector::fill(const char * s)\n";
176  cerr << s << "\n";
177  ad_exit(1);
178  }
179  if (lbraces == 0)
180  {
181  cerr << "Missing braces { ... } in dvar_vector::fill(const char * s)\n";
182  cerr << s << "\n";
183  ad_exit(1);
184  }
185  }
186  delete [] t;
187  t = NULL;
188 }
void fill(const char *)
Fill variable vector from values in string s.
Definition: fvar_a10.cpp:52
const int MAX_FIELD_LENGTH
Definition: dmat_io2.cpp:56
exitptr ad_exit
Definition: gradstrc.cpp:53
prevariable elem(int i)
Definition: fvar.hpp:2221
unsigned int size() const
Definition: fvar.hpp:2297
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
int indexmin() const
Definition: fvar.hpp:2287
size_t length(const adstring &t)
Returns the size of adstr.
Definition: string1.cpp:228
int indexmax() const
Definition: fvar.hpp:2292