ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fvar_io2.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 #if defined(__TURBOC__)
14  #pragma hdrstop
15  #include <iostream.h>
16  #include <strstrea.h>
17 #endif
18 
19 #ifdef __ZTC__
20  #include <iostream.hpp>
21  #if (__ZTC__ < 0x310)
22  #include <sstream.hpp>
23  #else
24  #include <strstream.h>
25  #endif
26 #endif
27 
28 #ifdef __NDPX__
29  #include <iostream.h>
30  #include <sstream.h>
31 #endif
32 
33 #ifdef __SUN__
34  #include <iostream.h>
35  #include <strstream.h>
36  #define __USE_IOSTREAM__
37 #endif
38 
39 #include <string.h>
40 #include <ctype.h>
41 
42 #include <sstream>
43 using std::istringstream;
44 
45 #ifndef OPT_LIB
46  #include <cassert>
47 #endif
48 
49 const int MAX_FIELD_LENGTH = 500;
50 
54 dvar_vector::dvar_vector(const char * s)
55 {
56  size_t n = strlen(s);
57  int lbraces = 0;
58  int rbraces = 0;
59  int commas = 0;
60 
61  char* t = new char[n];
62 
63  for (size_t k = 0; k < n; k++)
64  {
65  if (s[k] == '{')
66  {
67  lbraces ++;
68  t[k] = ' ';
69  }
70  else if (s[k] == '}')
71  {
72  rbraces ++;
73  t[k] = ' ';
74  }
75  else if (s[k] == ',')
76  {
77  commas ++;
78  t[k] = ' ';
79  }
80  else
81  {
82  t[k] = s[k];
83  }
84  }
85 
86  if (lbraces != rbraces)
87  {
88  cerr << "Unbalanced braces in dvector::dvector(const char * s)\n";
89  cerr << s << "\n";
90  ad_exit(1);
91  }
92 
93  if (lbraces > 1)
94  {
95  cerr << "Only one level of braces allowed in "
96  "dvector::dvector(const char * s)\n";
97  cerr << s << "\n";
98  ad_exit(1);
99  }
100 
101  if (lbraces == 1)
102  {
103  int ncl = 1;
104  int nch = commas + 1;
105 
106  allocate(ncl,nch);
107  istringstream ss(t);
108 
109  for (int k = ncl; k <= nch; k++)
110  {
111  ss >> this->elem(k);
112  //va[k].nc=0.;
113  }
114  }
115  else
116  {
117  const char* filename = s;
118  ifstream infile(filename);
119  if (!infile)
120  {
121  cerr << "Error opening file " << filename << " in dvector constructor "
122  << "dvector::dvector(char * filename)\n";
123  ad_exit(1);
124  }
125 
126  int i=0;
127 
128 // char * field = (char *) new[size_t(MAX_FIELD_LENGTH+1)];
129  char* field = new char[size_t(MAX_FIELD_LENGTH+1)];
130  infile.width(size_t(MAX_FIELD_LENGTH+1));
131  int count=0;
132  char c;
133  infile.get(c);
134  while (!infile.eof())
135  {
136  if (isspace(c))
137  {
138  infile.get(c);
139  }
140  else if (c == ',')
141  {
142  infile.get(c);
143  }
144  else
145  {
146  ++count;
147  do
148  {
149  infile.get(c);
150  } while (!isspace(c) && c != ',');
151  }
152  }
153 
154  infile.clear();
155  infile.seekg(0,ios::beg);
156 
157  allocate(1,count);
158 
159  #ifdef DIAG
160  cout << "Created a ncopies with address " << _farptr_tolong(&(shape->ncopies))<<"\n";
161  cout << "Created a dvector with address " << _farptr_tolong(va) <<"\n";
162  #endif
163 #ifndef OPT_LIB
164  assert(size() > 0);
165 #endif
167  if (!gs)
168  {
169  cerr << "Error: instance of gradient_structure is a nullptr.\n";
170  ad_exit(1);
171  }
172  if ((va = gs->ARR_LIST1->arr_new((unsigned int)size()))==0 )
173  {
174  cerr << " Error trying to allocate memory for dvector\n";
175  ad_exit(21);
176  }
177  char* err_ptr;
178  infile.width(size_t(MAX_FIELD_LENGTH+1));
179  for (i=1;i<=count;i++)
180  {
181  int index = 0;
182  char c;
183  infile.get(c);
184  while (!infile.eof())
185  {
186  if (isspace(c))
187  {
188  infile.get(c);
189  }
190  else if (c == ',')
191  {
192  infile.get(c);
193  }
194  else
195  {
196  do
197  {
198  field[index] = c;
199 
200  infile.get(c);
201  ++index;
202  } while (!isspace(c) && c != ',');
203  field[index] = '\0';
204  break;
205  }
206  }
207  elem(i)=strtod(field,&err_ptr); // increment column counter
208 
209  if (isalpha((unsigned char)err_ptr[0]))
210  {
211  cerr << "Error decoding field " << filename
212  << " in dmatrix::dmatrix(char * filename) " << "\n";
213  cerr << "Error occurred at element " << count << "\n";
214  cerr << "Offending characters start with "
215  << err_ptr[0]
216  << err_ptr[1]
217  << err_ptr[2]
218  << err_ptr[3] << "\n";
219  ad_exit(1);
220  }
221  if (value(elem(i))== HUGE_VAL ||value(elem(i))== -HUGE_VAL)
222  {
223  cerr << "Overflow Error decoding field " << filename
224  << " in dmatrix::dmatrix(char * filename) " << "\n";
225  cerr << "Error occurred at element " << count << "\n";
226  ad_exit(1);
227  }
228  }
229  delete[] field;
230  field = 0;
231 
232  infile.close();
233  }
234  delete [] t;
235  t = 0;
236 }
dvar_vector()
Default constructor.
Definition: fvar_arr.cpp:150
double_and_int * arr_new(unsigned int sz)
Definition: newdar.cpp:78
const int MAX_FIELD_LENGTH
Definition: dmat_io2.cpp:56
friend dvector value(const dvar_vector &v1)
Description not yet available.
Definition: fvar_ar3.cpp:43
void allocate(void)
Does not allocate, but initializes dvar_vector to empty.
Definition: fvar_arr.cpp:216
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.
static _THREAD gradient_structure * _instance
unsigned int ncopies
Copy counter to enable shallow copies.
Definition: vector_shapex.h:79
double_and_int * va
Definition: fvar.hpp:2175
class for things related to the gradient structures, including dimension of arrays, size of buffers, etc.
vector_shapex * shape
Definition: fvar.hpp:2179