ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dvect9.cpp
Go to the documentation of this file.
1 
5 #include "fvar.hpp"
6 #include <math.h>
7 
8 #if defined(__TURBOC__)
9  #pragma hdrstop
10  #include <iostream.h>
11  #include <strstrea.h>
12 #endif
13 
14 #ifdef __ZTC__
15  #include <iostream.hpp>
16  #if (__ZTC__ < 0x310)
17  #include <sstream.hpp>
18  #else
19  #include <strstream.h>
20  #endif
21  #define __USE_IOSTREAM__
22 #endif
23 
24 #ifdef __NDPX__
25  #include <iostream.h>
26  #include <sstream.h>
27 #endif
28 
29 #ifdef __SUN__
30  #include <iostream.h>
31  #include <strstream.h>
32  #define __USE_IOSTREAM__
33 #endif
34 
35 #include <string.h>
36 #include <ctype.h>
37 
38 #include <cassert>
39 
40 const unsigned int MAX_FIELD_LENGTH = 500;
41 
48 dvector::dvector(const char* s)
49 {
50  const size_t n = strlen(s);
51  int lbraces = 0;
52  int rbraces = 0;
53  int commas = 0;
54 
55  char* t = new char[n+1];
56  assert(t);
57  t[n] = '\0';
58 
59  for (size_t k = 0; k < n; k++)
60  {
61  if (s[k] == '{')
62  {
63  lbraces ++;
64  t[k] = ' ';
65  }
66  else if (s[k] == '}')
67  {
68  rbraces ++;
69  t[k] = ' ';
70  }
71  else if (s[k] == ',')
72  {
73  commas ++;
74  t[k] = ' ';
75  }
76  else
77  {
78  t[k] = s[k];
79  }
80  }
81  if (lbraces != rbraces)
82  {
83  cerr << "Unbalanced braces in dvector::dvector(const char * s)\n";
84  cerr << s << "\n";
85  ad_exit(1);
86  }
87  if (lbraces > 1)
88  {
89  cerr << "Only one level of braces allowed in "
90  "dvector::dvector(const char * s)\n";
91  cerr << s << "\n";
92  ad_exit(1);
93  }
94  if (lbraces == 1)
95  {
96  int ncl = 1;
97  int nch = commas + 1;
98 
99  allocate(ncl,nch);
100 
101  char *field = new char[MAX_FIELD_LENGTH + 1];
102  char *err_ptr;
103  size_t index = 0;
104  size_t length = strlen(t);
105  for (int i=ncl;i<=nch;i++)
106  {
107  char c = t[index];
108  while (c == ' ')
109  {
110  ++index;
111  if (index >= length) break;
112 
113  c = t[index];
114  }
115  int field_index = 0;
116  while (c != ' ')
117  {
118  field[field_index] = c;
119  ++field_index;
120 
121  ++index;
122  if (index >= length) break;
123 
124  c = t[index];
125  }
126  field[field_index] = '\0';
127 
128  v[i]=strtod(field,&err_ptr); // increment column counter
129 
130  if (isalpha((unsigned char)err_ptr[0]))
131  {
132  cerr << "Error decoding field "
133  << " in dvector::dvector(char * filename) " << "\n";
134  cerr << "Error occurred at element " << i << "\n";
135  cerr << "Offending characters start with "
136  << field << endl;
137  ad_exit(1);
138  }
139  if (elem(i)== HUGE_VAL ||elem(i)== -HUGE_VAL)
140  {
141  cerr << "Overflow Error decoding field in dvector::dvector(char* )"
142  << "\n";
143  cerr << "Error occurred at element " << i << "\n";
144  cerr << "Offending characters start with "
145  << field << endl;
146  ad_exit(1);
147  }
148  }
149  delete [] field;
150  field=0;
151  }
152  else // if lbraces == 0
153  {
154  const char * filename=s;
155  ifstream infile(filename);
156  if (!infile)
157  {
158  cerr << "Error opening file " << filename << " in dvector constructor "
159  << "dvector::dvector(char* filename)\n";
160  ad_exit(1);
161  }
162  infile.width(MAX_FIELD_LENGTH);
163  char* field = new char[MAX_FIELD_LENGTH + 1];
164  infile.width(MAX_FIELD_LENGTH + 1);
165  int count = 0;
166  char c;
167  infile.get(c);
168  while (!infile.eof())
169  {
170  if (isspace(c))
171  {
172  infile.get(c);
173  }
174  else if (c == ',')
175  {
176  infile.get(c);
177  }
178  else
179  {
180  ++count;
181  do
182  {
183  infile.get(c);
184  } while (!isspace(c) && c != ',');
185  }
186 /*
187  if (!infile.good())
188  {
189  cerr << "Error reading file " << filename
190  << " in dvector constructor "
191  << "dvector::dvector(char * filename)\n";
192  cerr << "Error appears to have occurred at element"
193  << count+1 << endl;
194  cerr << "Stream state is " << infile.rdstate() << endl;
195  ad_exit(1);
196  }
197 */
198  }
199 
200  infile.clear();
201  infile.seekg(0,ios::beg);
202 
203  if ((v = new double[static_cast<unsigned int>(count + 2)]) ==0)
204  {
205  cerr << " Error trying to allocate memory for dvector\n";
206  ad_exit(21);
207  }
208  #if defined(THREAD_SAFE)
209  if ((shape = new ts_vector_shapex(1, count, v)) == NULL)
210  #else
211  if ((shape = new vector_shapex(1, count, v)) == NULL)
212  #endif
213  {
214  cerr << "Error trying to allocate memory for dvector\n";
215  ad_exit(1);
216  }
217  index_min = 1;
218  index_max = count;
219  v -= indexmin();
220 
221 #ifdef DIAG
222  cout << "Created ncopies with address " << _farptr_tolong(&(shape->ncopies)) <<"\n";
223  cout << "Created dvector with address " << _farptr_tolong(v) <<"\n";
224 #endif
225  char* err_ptr;
226  infile.width(MAX_FIELD_LENGTH + 1);
227  for (int i = 1; i <= count; ++i)
228  {
229  int index = 0;
230  char c;
231  infile.get(c);
232  while (!infile.eof())
233  {
234  if (isspace(c))
235  {
236  infile.get(c);
237  }
238  else if (c == ',')
239  {
240  infile.get(c);
241  }
242  else
243  {
244  do
245  {
246  field[index] = c;
247 
248  infile.get(c);
249  ++index;
250  } while (!isspace(c) && c != ',');
251  field[index] = '\0';
252  break;
253  }
254  }
255  elem(i) = strtod(field,&err_ptr); // increment column counter
256 
257  if (isalpha((unsigned char)err_ptr[0]))
258  {
259  cerr << "Error decoding field " << filename
260  << " in dvector::dvector(char * filename) " << "\n";
261  cerr << "Error occurred at element " << count << "\n";
262  cerr << "Offending characters start with "
263  << err_ptr[0]
264  << err_ptr[1]
265  << err_ptr[2]
266  << err_ptr[3] << "\n";
267  ad_exit(1);
268  }
269  if (elem(i) == HUGE_VAL || elem(i) == -HUGE_VAL)
270  {
271  cerr << "Overflow Error decoding field " << filename
272  << " in dvector::dvector(char * filename) " << "\n";
273  cerr << "Error occurred at element " << count << "\n";
274  cerr << "Offending characters start with "
275  << err_ptr[0]
276  << err_ptr[1]
277  << err_ptr[2]
278  << err_ptr[3] << "\n";
279  ad_exit(1);
280  }
281  }
282  delete[] field;
283  field = 0;
284 
285  infile.close();
286  }
287  delete [] t;
288  t = 0;
289 }
296 void dvector::allocate(const char* s)
297 {
298  const size_t n = strlen(s);
299  int lbraces = 0;
300  int rbraces = 0;
301  int commas = 0;
302 
303  char* t = new char[n + 1];
304  assert(t);
305  t[n]= '\0';
306 
307  for (size_t k = 0; k < n; k++)
308  {
309  if (s[k] == '{')
310  {
311  lbraces ++;
312  t[k] = ' ';
313  }
314  else if (s[k] == '}')
315  {
316  rbraces ++;
317  t[k] = ' ';
318  }
319  else if (s[k] == ',')
320  {
321  commas ++;
322  t[k] = ' ';
323  }
324  else
325  {
326  t[k] = s[k];
327  }
328  }
329  if (lbraces != rbraces)
330  {
331  cerr << "Unbalanced braces in dvector::dvector(const char* s)\n";
332  cerr << s << "\n";
333  ad_exit(1);
334  }
335  if (lbraces > 1)
336  {
337  cerr << "Only one level of braces allowed in "
338  "dvector::dvector(const char* s)\n";
339  cerr << s << "\n";
340  ad_exit(1);
341  }
342  if (lbraces == 1)
343  {
344  int ncl = 1;
345  int nch = commas + 1;
346 
347  allocate(ncl,nch);
348 
349  char* field = new char[size_t(MAX_FIELD_LENGTH+1)];
350  char* err_ptr;
351  size_t index = 0;
352  size_t length = strlen(t);
353  for (int i=ncl;i<=nch;i++)
354  {
355  char c = t[index];
356  while (c == ' ')
357  {
358  ++index;
359  if (index >= length) break;
360 
361  c = t[index];
362  }
363  int field_index = 0;
364  while (c != ' ')
365  {
366  field[field_index] = c;
367  ++field_index;
368 
369  ++index;
370  if (index >= length) break;
371 
372  c = t[index];
373  }
374  field[field_index] = '\0';
375 
376  v[i] = strtod(field,&err_ptr); // increment column counter
377 
378  if (isalpha((unsigned char)err_ptr[0]))
379  {
380  cerr << "Error decoding field "
381  << " in dvector::dvector(char * filename) " << "\n";
382  cerr << "Error occurred at element " << i << "\n";
383  cerr << "Offending characters start with "
384  << err_ptr[0]
385  << err_ptr[1]
386  << err_ptr[2]
387  << err_ptr[3] << "\n";
388  ad_exit(1);
389  }
390 
391  if (elem(i) == HUGE_VAL ||elem(i) == -HUGE_VAL)
392  {
393  cerr << "Overflow Error decoding field "
394  " in dvector::dvector(char * ) " << "\n";
395  cerr << "Error occurred at element " << i << "\n";
396  ad_exit(1);
397  }
398  }
399  delete [] field;
400  field = 0;
401  }
402  else // if lbraces == 0
403  {
404  const char* filename = s;
405  ifstream infile(filename);
406  if (!infile)
407  {
408  cerr << "Error opening file " << filename << " in dvector constructor "
409  << "dvector::dvector(char * filename)\n";
410  ad_exit(1);
411  }
412 
413  infile.width(MAX_FIELD_LENGTH);
414  char* field = new char[MAX_FIELD_LENGTH + 1];
415  infile.width(MAX_FIELD_LENGTH + 1);
416 
417 /*
418  if (!infile.good())
419  {
420  cerr << "Error reading file " << filename
421  << " in dvector::allocate(char* filename)\n";
422  cerr << "Error appears to have occurred at element"
423  << count + 1 << endl;
424  cerr << "Stream state is " << infile.rdstate() << endl;
425  ad_exit(1);
426  }
427 */
428 
429  int count = 0;
430  char c;
431  infile.get(c);
432  while (!infile.eof())
433  {
434  if (isspace(c))
435  {
436  infile.get(c);
437  }
438  else if (c == ',')
439  {
440  infile.get(c);
441  }
442  else
443  {
444  ++count;
445  do
446  {
447  infile.get(c);
448  } while (!isspace(c) && c != ',');
449  }
450  }
451 
452  infile.clear();
453  infile.seekg(0,ios::beg);
454 
455  if ((v = new double[static_cast<unsigned int>(count + 2)]) ==0)
456  {
457  cerr << " Error trying to allocate memory for dvector\n";
458  ad_exit(21);
459  }
460 #if defined(THREAD_SAFE)
461  if ((shape = new ts_vector_shapex(1, count, v)) == NULL)
462 #else
463  if ((shape = new vector_shapex(1, count, v)) == NULL)
464 #endif
465  {
466  cerr << "Error trying to allocate memory for dvector\n";
467  ad_exit(21);
468  }
469 
470 #ifdef DIAG
471  cout << "Created ncopies with address " << _farptr_tolong(&(shape->ncopies)) << "\n";
472  cout << "Created dvector with address " << _farptr_tolong(v) << "\n";
473 #endif
474  index_min = 1;
475  index_max = count;
476  v -= indexmin();
477  char* err_ptr;
478  infile.width(MAX_FIELD_LENGTH + 1);
479  for (int i = 1; i <= count; ++i)
480  {
481  int index = 0;
482  char c;
483  infile.get(c);
484  while (!infile.eof())
485  {
486  if (isspace(c))
487  {
488  infile.get(c);
489  }
490  else if (c == ',')
491  {
492  infile.get(c);
493  }
494  else
495  {
496  do
497  {
498  field[index] = c;
499 
500  infile.get(c);
501  ++index;
502  } while (!isspace(c) && c != ',');
503  field[index] = '\0';
504  break;
505  }
506  }
507  elem(i) = strtod(field, &err_ptr); // increment column counter
508 
509  if (isalpha((unsigned char)err_ptr[0]))
510  {
511  cerr << "Error decoding field " << filename
512  << " in dvector::dvector(char * filename) " << "\n";
513  cerr << "Error occurred at element " << count << "\n";
514  cerr << "Offending characters start with "
515  << err_ptr[0]
516  << err_ptr[1]
517  << err_ptr[2]
518  << err_ptr[3] << "\n";
519  ad_exit(1);
520  }
521  if (elem(i) == HUGE_VAL || elem(i) == -HUGE_VAL)
522  {
523  cerr << "Overflow Error decoding field " << filename
524  << " in dvector::dvector(char * filename) " << "\n";
525  cerr << "Error occurred at element " << count << "\n";
526  cerr << "Offending characters start with "
527  << err_ptr[0]
528  << err_ptr[1]
529  << err_ptr[2]
530  << err_ptr[3] << "\n";
531  ad_exit(1);
532  }
533  }
534  delete [] field;
535  field = 0;
536 
537  infile.close();
538  }
539  delete [] t;
540  t = 0;
541 }
double * v
pointer to the data
Definition: dvector.h:53
dvector()
Construct a dvector without allocating memory.
Definition: dvector.cpp:378
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
void allocate(void)
Allocate dvector without allocating memory.
Definition: dvector.cpp:495
prnstream & endl(prnstream &)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
unsigned int ncopies
Copy counter to enable shallow copies.
Definition: vector_shapex.h:79
int index_min
minimum valid subscript
Definition: dvector.h:54
Holds &quot;shape&quot; information for vector objects.
Definition: vector_shapex.h:46
vector_shapex * shape
pointer to vector &quot;shape&quot;
Definition: dvector.h:60
int index_max
maximum valid subscript
Definition: dvector.h:55
size_t length(const adstring &t)
Returns the size of adstr.
Definition: string1.cpp:228