ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cifstrem.cpp
Go to the documentation of this file.
1 
9 #include <sstream>
10 using std::istringstream;
11 
12 #include <fvar.hpp>
13 
14 #if defined(__GNUC__) && (__GNUC__ < 3)
15  #pragma implementation "cifstrem.h"
16 #endif
17 
18 #include "cifstrem.h"
19 
20 #ifdef DEBUG
21 #include <cassert>
22 #endif
23 
28 {
29 }
31 {
32 #ifdef __BCPLUSPLUS__
33  int current_state = rdstate();
34  setstate(current_state | ios::eofbit);
35 #endif
36 #ifdef __ZTC__
37  int current_state = rdstate();
38  clear(current_state | ios::eofbit);
39 #endif
40 }
45 {
46  if (strlen(signature_line) <= 0)
47  {
48  int c = bp->sgetc();
49 
50  int n = 0;
51  while (c != '\n' && c != '\r' && c != EOF)
52  {
53  //cout << (char)c << endl;
54  if (n < SIGNATURE_LENGTH)
55  {
56  signature_line[n] = (char)c;
57  //cout << signature_line << endl;
58  ++n;
59  }
60  c = bp->snextc();
61  }
62  signature_line[n] = '\0';
63 
65 
66  // just position buffer to first character of next line
67  if (c == '\r')
68  {
69  c = bp->snextc();
70  }
71  if (c == '\n')
72  {
73  c = bp->snextc();
74  }
75 
76  line++;
77  }
78  return signature_line;
79 }
80 
82 {
83  return file_name;
84 }
85 
86 cifstream::cifstream(const char* fn, int open_m, char cc)
87 #if defined (_MSC_VER) || defined (__WAT32__)
88  : ifstream(fn, ios::in | open_m) , file_name(fn)
89 #elif defined(__BCPLUSPLUS__)
90  : ifstream(fn, ios::in | open_m) , file_name(fn)
91 #elif defined (__NDPX__)
92  : ifstream(fn, ios::in | open_m) , file_name(fn)
93 #elif defined (__INTEL_COMPILER)
94  : ifstream(fn) , file_name(fn)
95 #elif defined(__SUNPRO_CC) && (__SUNPRO_CC < 0x5140)
96  : ifstream(fn, ios::in | open_m) , file_name(fn)
97 #elif defined (__ZTC__)
98  : ios(&buffer), ifstream(fn, ios::in | open_m) , file_name(fn)
99 #else
100  : ifstream(fn, ios::in | std::ios::openmode(open_m)) , file_name(fn)
101 #endif
102 {
103  bp = rdbuf();
104  COMMENT_CHAR = cc;
105  if (this->good())
106  {
107  line = 1;
108  field = 0;
109  ignore_eof = 1;
110  }
111  else
112  {
113  line = 0;
114  field = 0;
115  ignore_eof = 0;
116  }
117  memset(comment_line, '\0', SIGNATURE_LENGTH);
118  memset(signature_line, '\0', SIGNATURE_LENGTH);
119 }
125 {
126  int c = bp->sgetc();
127  do
128  {
129  if (c == '#')
130  {
131  int n = 0;
132  while (c != '\r' && c != '\n' && c != EOF)
133  {
134  if (n < SIGNATURE_LENGTH)
135  {
136  comment_line[n] = (char)c;
137  ++n;
138  }
139  c = bp->snextc();
140  }
141  comment_line[n] = '\0';
142  if (line == 1)
143  {
144  strcpy(signature_line, comment_line);
145  }
146  ++line;
147  field = 0;
148  }
149  else if (isspace(c))
150  {
151  c = bp->snextc();
152  }
153  else
154  {
155  break;
156  }
157  } while (c != EOF);
158  if (c == EOF)
159  {
160  set_eof_bit();
161  report_error(
162  "function: void cifstream::filter(); premature end of file?");
163  }
164 }
171 void cifstream::get_field(char* s, int space_flag)
172 {
173  filter();
174 
175  int testc = bp->sgetc();
176 
177  int n = 0;
178  if (!space_flag)
179  {
180  while (n < FILTER_BUF_SIZE && !isspace(testc))
181  {
182  s[n++] = (char)testc;
183  testc = bp->snextc();
184  if (!good() || testc == EOF)
185  {
186  break;
187  }
188  }
189  }
190  else
191  {
192  while (n < FILTER_BUF_SIZE)
193  {
194  s[n++] = (char)testc;
195  testc = bp->snextc();
196  if (!good() || testc == EOF)
197  {
198  break;
199  }
200  }
201  }
202  if (n>=FILTER_BUF_SIZE)
203  {
204  report_error("function: void cifstream::prefilter();"
205  " Buffer size exceeded?");
206  }
207  if (!good() || testc == EOF)
208  {
209  if (testc == EOF)
210  set_eof_bit();
211  report_error(
212  "function: void cifstream::prefilter(); premature end of file?");
213  }
214  s[n++] = '\0';
215  field++;
216 }
223 {
224  char * t = new char[FILTER_BUF_SIZE];
225  (*this) >> t;
226  s.realloc(t);
227  delete [] t;
228  return (*this);
229 }
236 {
237  adstring& s = (adstring&) _s;
238  char * t = new char[FILTER_BUF_SIZE];
239  (*this) >> t;
240  s.realloc(t);
241  delete [] t;
242  return (*this);
243 }
250 {
251  filter();
252  char line[256];
253  getline(line, 256);
254  str.realloc(&line[0]);
255  return *this;
256 }
263 {
264  return operator>>(const_cast<line_adstring&>(str));
265 }
272 {
273  get_field(c);
274  return *this;
275 }
282 {
283  get_field(const_cast<char*>(c));
284  return *this;
285 }
292 {
293  char * s = new char[FILTER_BUF_SIZE];
294  get_field(s);
295  istringstream is(s);
296  is >> (long&) i;
297 #ifdef __NDPX__
298  if (is.eof()) is.clear();
299 #endif
300  if (!is)
301  {
302  this->clear(is.rdstate());
303  report_error("lont int extraction operator");
304  }
305  delete []s;
306  return *this;
307 }
308 
309 #if defined(__ADSGI__)
310 istream& istream::operator>>(long long & x)
311 {
312  long int i;
313  (*this) >> i;
314  x=i;
315  return *this;
316 }
317 #else
318 
323 cifstream& cifstream::operator>>(const long long& _i)
324 {
325  ADUNCONST(long long,i)
326  char * s = new char[FILTER_BUF_SIZE];
327  get_field(s);
328  istringstream is(s);
329  is >> i;
330 #ifdef __NDPX__
331  if (is.eof()) is.clear();
332 #endif
333  if (!is)
334  {
335  this->clear(is.rdstate());
336  report_error("lont int extraction operator");
337  }
338  delete []s;
339  return *this;
340 }
347 {
348  char * s = new char[FILTER_BUF_SIZE];
349  get_field(s);
350  istringstream is(s);
351  is >> i;
352 #ifdef __NDPX__
353  if (is.eof()) is.clear();
354 #endif
355  if (!is)
356  {
357  this->clear(is.rdstate());
358  report_error("lont int extraction operator");
359  }
360  delete []s;
361  return *this;
362 }
363 #endif
364 
366 {
367  size_t n = strlen(s) - 1;
368  size_t i = 0;
369 
370  while ((i < n) && (s[i]=='0') )
371  {
372  s[i] = ' ';
373  i++;
374  }
375 }
382 {
383  char * s = new char[FILTER_BUF_SIZE];
384  get_field(s);
385  //cout << "cifstream& cifstream::operator >> (int& i) s = '" << s
386  // << "'" << endl;
388  istringstream is(s);
389  is >> (int&)i;
390 #ifdef __NDPX__
391  if (is.eof()) is.clear();
392 #endif
393  if (!is)
394  {
395  this->clear(is.rdstate());
396  report_error("int extraction operator");
397  }
398  delete []s;
399  return *this;
400 }
407 {
408  double& x = (double&)(_x);
409  //char * s = new char[FILTER_BUF_SIZE];
410  char* s = (char*)malloc(8000*sizeof(char));
411  if (s)
412  {
413  get_field(s);
414 
415 #ifdef DEBUG
416  //assert(s[0]!='#' && s[1] != '\0');
417  assert(s[0] != '#');
418 #endif
419 
420 #if !defined(__BORLANDC__)
421  istringstream is(s);
422  if (!is)
423  {
424  this->clear(is.rdstate());
425  report_error("double extraction operator");
426  }
427  is >> x;
428 
429  #ifdef __NDPX__
430  if (is.eof()) is.clear();
431  #endif
432  if (!is)
433  {
434  this->clear(is.rdstate());
435  report_error("double extraction operator");
436  }
437 #else
438  char* end=0;
439  x=strtod(s,&end);
440 #endif
441 
442  //delete [] s;
443  free(s);
444  s = 0;
445  }
446  return *this;
447 }
454 {
455  char * s = new char[FILTER_BUF_SIZE];
456  get_field(s);
457  istringstream is(s);
458  is >> (float&)x;
459 #ifdef __NDPX__
460  if (is.eof()) is.clear();
461 #endif
462  if (!is)
463  {
464  this->clear(is.rdstate());
465  report_error("float extraction operator");
466  }
467  delete []s;
468  return *this;
469 }
473 cifstream& cifstream::getline(char* s, int k, char d)
474 {
475  filter();
476 
477  s[0] = '\0';
478  int n = 0;
479  int testc = bp->sbumpc();
480  while ( (!eof()) && (n < k) && (testc != d) && (testc != EOF))
481  {
482  s[n++] = (char)testc;
483  testc = bp->sbumpc();
484  }
485  s[n++] = '\0';
486 
487  return *this;
488 }
489 
490 /*
491 void cifstream::report_error(const char * msg)
492 {
493  int ss = rdstate();
494  //cout << "stream state = " << ss << endl;
495 #ifdef __BCPLUSPLUS___
496  int end = eof();
497 #endif
498 #ifdef __ZTC__
499  int end = (ss < 4);
500 #endif
501  //cout << "eof() = " << end << endl;
502  //cout << "ignore_eof = " << ignore_eof << endl;
503 
504  if (!end || (end && ignore_eof))
505  {
506  cerr << "\n** error reading file '" << file_name
507  << "' at line " << line
508  << ", field " << field
509  << endl;
510  if (msg)
511  cerr << " " << msg << endl;
512  if (end)
513  cerr << " end of file" << endl;
514  //exit(1);
515  }
516 }
517 */
518 
519 void cifstream::report_error(const char * msg) {;}
520 
521 /*
522 #if !defined(__ADSGI__)
523  void strnset(char * comment_line, char x, size_t len)
524 #else
525  void strnset(char * comment_line, const char x, size_t len)
526 #endif
527  {
528  unsigned int tlen;
529  //len=100;
530  //tlen=strlen(comment_line);
531  tlen=strlen("x");
532  if (tlen>len)tlen=len;
533  memset(comment_line,x,tlen);
534  }
535 */
virtual ~cifstream()
Destructor.
Definition: cifstrem.cpp:27
#define x
char comment_line[SIGNATURE_LENGTH+1]
Definition: cifstrem.h:121
#define ADUNCONST(type, obj)
Creates a shallow copy of obj that is not CONST.
Definition: fvar.hpp:140
cifstream(const char *, int=std::ios_base::in, char cc= '#')
Definition: cifstrem.cpp:86
void realloc(const char *t)
Definition: string1.cpp:77
#define FILTER_BUF_SIZE
Definition: cifstrem.h:114
char * signature()
Get the signature line of inputfile.
Definition: cifstrem.cpp:44
int line
Definition: cifstrem.h:124
void get_field(char *s, int space_flag=0)
Extract a single field into s from data file.
Definition: cifstrem.cpp:171
void set_eof_bit(void)
Definition: cifstrem.cpp:30
cifstream & operator>>(const dvariable &z)
Reads input into var from comment formatted input file.
Definition: dfadcif.cpp:25
cifstream & getline(char *, int, char= '\n')
Definition: cifstrem.cpp:473
void filter()
Move file pointer past comments and empty lines to next data field.
Definition: cifstrem.cpp:124
#define SIGNATURE_LENGTH
Definition: cifstrem.h:115
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
int field
Definition: cifstrem.h:125
adstring str(double x, int minwidth=17, int decplaces=-1)
Convert x to adstring with minimum width and total number of decimal places.
Definition: str.cpp:25
adstring get_file_name(void)
Definition: cifstrem.cpp:81
istream & operator>>(const istream &input, const d3_array &arr3)
Read values from input stream into arr3.
Definition: d3_io.cpp:60
Definition of the cifstream class.
streambuf * bp
Definition: cifstrem.h:119
void report_error(const char *s=NULL)
Definition: cifstrem.cpp:519
adstring file_name
Definition: cifstrem.h:123
static double cc
Definition: cnorlogmix.cpp:13
void js_strip_leading_zeros(char *s)
Definition: cifstrem.cpp:365
char signature_line[SIGNATURE_LENGTH+1]
Definition: cifstrem.h:122