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