GeoTessCPP  2.2
Software to facilitate storage and retrieval of 3D information about the Earth.
IFStreamAscii.h
Go to the documentation of this file.
1 //- ****************************************************************************
2 //-
3 //- Copyright 2009 Sandia Corporation. Under the terms of Contract
4 //- DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
5 //- retains certain rights in this software.
6 //-
7 //- BSD Open Source License.
8 //- All rights reserved.
9 //-
10 //- Redistribution and use in source and binary forms, with or without
11 //- modification, are permitted provided that the following conditions are met:
12 //-
13 //- * Redistributions of source code must retain the above copyright notice,
14 //- this list of conditions and the following disclaimer.
15 //- * Redistributions in binary form must reproduce the above copyright
16 //- notice, this list of conditions and the following disclaimer in the
17 //- documentation and/or other materials provided with the distribution.
18 //- * Neither the name of Sandia National Laboratories nor the names of its
19 //- contributors may be used to endorse or promote products derived from
20 //- this software without specific prior written permission.
21 //-
22 //- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 //- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 //- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 //- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26 //- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 //- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 //- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 //- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 //- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 //- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 //- POSSIBILITY OF SUCH DAMAGE.
33 //-
34 //- ****************************************************************************
35 
36 #ifndef IFSTREAMASCII_OBJECT_H
37 #define IFSTREAMASCII_OBJECT_H
38 
39 // **** _SYSTEM INCLUDES_ ******************************************************
40 
41 #include <iostream>
42 #include <string>
43 #include <fstream>
44 #include <vector>
45 #include <sstream>
46 
47 // use standard library objects
48 using namespace std;
49 
50 // **** _LOCAL INCLUDES_ *******************************************************
51 
52 #include "CPPUtils.h"
53 #include "GeoTessUtils.h"
54 #include "GeoTessException.h"
55 
56 // **** _BEGIN GEOTESS NAMESPACE_ **********************************************
57 
58 namespace geotess {
59 
60 // **** _FORWARD REFERENCES_ ***************************************************
61 
62 // **** _CLASS DEFINITION_ *****************************************************
63 
81 {
82  private:
83 
87  ifstream ifs;
88 
92  ofstream ofs;
93 
109  string strDelim[5];
110 
114  string strFileName;
115 
116  // The total number of lines read from this stream
117  int strTotlLinesRead;
118  // The total number of "data" lines read from this stream
119  int strDataLinesRead;
120  // The total number of "blank" lines read from this stream
121  int strBlankLinesRead;
122  // The total number of "comment" lines read from this stream
123  int strCommentLinesRead;
124  // The total number of "block comment" lines read from this stream
125  int strBlkCommentLinesRead;
126  // The total number of "bytes" read from this stream
127  int strBytesRead;
128 
133  bool strBlkCommntSet;
134 
142  int strTokenPtr;
143 
154  vector<string> strTokens;
155 
156  public:
157 
161  void getLine(string& buf);
162 
167  void getline(string& s) { std::getline(ifs, s); };
168 
172  IFStreamAscii() : strFileName(""), strTotlLinesRead(0),
173  strDataLinesRead(0), strBlankLinesRead(0),
174  strCommentLinesRead(0), strBlkCommentLinesRead(0),
175  strBytesRead(0), strBlkCommntSet(false),
176  strTokenPtr(0)
177  {setDefaultDelimiters(); setCommentDelimiter("#"); };
178 
182  virtual ~IFStreamAscii() { close(); };
183 
187  void openForRead(const string& fn);
188  void openForWrite(const string& fn);
189 
193  bool isOpen() { return (ifs.is_open() || ofs.is_open()) ? true : false; }
194 
198  void close()
199  {
200  if (ifs.is_open())
201  ifs.close();
202  else if (ofs.is_open())
203  ofs.close();
204  }
205 
209  void flush()
210  {
211  if (ofs.is_open())
212  ofs.flush();
213  }
214 
219  bool read(string& token);
220 
225  bool readLine(string& ln);
226 
231  void tokenize(const string& str, vector<string>& tokens);
232 
236  const string& getFileName() const { return strFileName; };
237 
241  void setDefaultDelimiters();
242 
246  void resetReader();
247 
252  void setDelimiters(const string& wspcDelims,
253  const string& strgDelim,
254  const string& cmntDelim,
255  const string& begBlk,
256  const string& endBlk)
257  {
258  strDelim[0] = wspcDelims;
259  strDelim[1] = strgDelim;
260  strDelim[2] = cmntDelim;
261  strDelim[3] = begBlk;
262  strDelim[4] = endBlk;
263  }
264 
268  void setWhitespaceDelimiters(const string& wsDelims)
269  { strDelim[0] = wsDelims; };
270 
274  const string& getWhitespaceDelimiters() const
275  { return strDelim[0]; };
276 
280  void setStringDelimiter(const string& strgDelim)
281  { strDelim[1] = strgDelim; };
282 
286  const string& getStringDelimiter() const
287  { return strDelim[1]; };
288 
292  void setCommentDelimiter(const string& cmntDelim)
293  { strDelim[2] = cmntDelim; };
294 
298  const string& getCommentDelimiter() const
299  { return strDelim[2]; };
300 
305  void setBlockCommentDelimiters(const string& begBlk,
306  const string& endBlk)
307  {
308  strDelim[3] = begBlk;
309  strDelim[4] = endBlk;
310  }
311 
315  void setBeginBlockCommentDelimiter(const string& begBlkCmntDelim)
316  { strDelim[3] = begBlkCmntDelim; };
317 
321  const string& getBeginBlockCommentDelimiter() const
322  { return strDelim[3]; };
323 
327  void setEndBlockCommentDelimiter(const string& endBlkCmntDelim)
328  { strDelim[4] = endBlkCmntDelim; };
329 
333  const string& getEndBlockCommentDelimiter() const
334  { return strDelim[4]; };
335 
339  int getTotalLinesRead() const
340  { return strTotlLinesRead; };
341 
345  int getDataLinesRead() const
346  { return strDataLinesRead; };
347 
351  int getBlankLinesRead() const
352  { return strBlankLinesRead; };
353 
358  { return strCommentLinesRead; };
359 
364  { return strBlkCommentLinesRead; };
365 
369  int getBytesRead() const
370  { return strBytesRead; };
371 
375  bool isEOF() const;
376 
380  bool next();
381 
385  string readString();
386 
390  bool readString(string& s);
391 
395  byte readByte();
396 
400  bool readType(byte& b) { return readByte(b); };
401 
405  bool readByte(byte& b);
406 
410  short readShort();
411 
415  bool readType(short& s) { return readShort(s); };
416 
420  bool readShort(short& s);
421 
425  int readInteger();
426 
430  bool readType(int& i) { return readInteger(i); };
431 
435  bool readInteger(int& i);
436 
440  LONG_INT readLong();
441 
445  bool readType(LONG_INT& l) { return readLong(l); };
446 
450  bool readLong(LONG_INT& l);
451 
455  float readFloat();
456 
460  bool readType(float& f) { return readFloat(f); };
461 
465  bool readFloat(float& f);
466 
470  double readDouble();
471 
475  bool readType(double& d) { return readDouble(d); };
476 
480  bool readDouble(double& d);
481 
482  // ***** Write functions **************************************************
483 
484  void writeString(const string& s) {ofs << s;}
485  void writeStringNL(const string& s) {ofs << s << endl;}
486  void writeType(const string& s) {ofs << s;}
487  void writeTypeNL(const string& s) {ofs << s << endl;}
488  void writeBool(bool b) {ofs << b;}
489  void writeBoolNL(bool b) {ofs << b << endl;}
490  void writeType(bool b) {ofs << b;}
491  void writeTypeNL(bool b) {ofs << b << endl;}
492  void writeByte(byte b) {ofs << (int)b;}
493  void writeByteNL(byte b) {ofs << (int)b << endl;}
494  void writeType(byte b) {ofs << (int)b;}
495  void writeTypeNL(byte b) {ofs << (int)b << endl;}
496  void writeShort(short s) {ofs << s;}
497  void writeShortNL(short s) {ofs << s << endl;}
498  void writeType(short s) {ofs << s;};
499  void writeTypeNL(short s) {ofs << s << endl;};
500  void writeInt(int i) {ofs << i;}
501  void writeIntNL(int i) {ofs << i << endl;}
502  void writeType(int i) {ofs << i;}
503  void writeTypeNL(int i) {ofs << i << endl;}
504  void writeLong(LONG_INT l) {ofs << l;}
505  void writeLongNL(LONG_INT l) {ofs << l << endl;}
506  void writeType(LONG_INT l) {ofs << l;}
507  void writeTypeNL(LONG_INT l) {ofs << l << endl;}
508  void writeFloat(float f) {ofs << f;}
509  void writeFloatNL(float f) {ofs << f << endl;}
510  void writeType(float f) {ofs << f;}
511  void writeTypeNL(float f) {ofs << f << endl;}
512  void writeDouble(double d) {ofs << d;}
513  void writeDoubleNL(double d) {ofs << d << endl;}
514  void writeType(double d) {ofs << d;}
515  void writeTypeNL(double d) {ofs << d << endl;}
516  void writeNL() {ofs << endl;}
517 
518 }; // end class IFStreamAscii
519 
526 inline bool IFStreamAscii::read(string& token)
527 {
528  string ln;
529 
530  // if the strTokenPtr is >= than strTokens.size() then get more tokens
531 
532  if (strTokenPtr >= (int) strTokens.size())
533  {
534  // get more tokens ... reset token pointer and list
535 
536  strTokenPtr = 0;
537  strTokens.clear();
538 
539  // read another data line from the stream ... if eof() is reached return
540  // true
541 
542  if (!readLine(ln)) return false;
543 
544  // tokennize the line into the tokens list
545 
546  tokenize(ln, strTokens);
547  }
548 
549  // get the token ... increment the pointer ... and return false for success
550 
551  token = strTokens[strTokenPtr++];
552  return true;
553 }
554 
558 inline bool IFStreamAscii::isEOF() const
559 {
560  return (ifs.eof() && (strTokenPtr >= (int) strTokens.size()));
561 }
562 
566 inline string IFStreamAscii::readString()
567 {
568  string s;
569  read(s);
570  return s;
571 }
572 
576 inline bool IFStreamAscii::readString(string& s)
577 {
578  return read(s);
579 }
580 
584 inline bool IFStreamAscii::next()
585 {
586  string s;
587  return read(s);
588 }
589 
593 inline byte IFStreamAscii::readByte()
594 {
595  byte b = 0;
596  readByte(b);
597  return b;
598 }
599 
603 inline bool IFStreamAscii::readByte(byte& b)
604 {
605  string sb;
606 
607  // read token into sb ... if eof or stream not open return false
608 
609  if (!read(sb)) return false;
610 
611  // attempt to scan token into b ... if unable issue error and return false
612 
613  if (sscanf(sb.c_str(), "%hhd", &b) != 1)
614  {
615  ostringstream os;
616  os << endl << "ERROR in IFStreamAscii::readByte" << endl
617  << " Could Not Scan Byte From Token = " << sb << endl
618  << " On File Line: " << strTotlLinesRead << " ..." << endl;
619  throw GeoTessException(os, __FILE__, __LINE__, 9201);
620  }
621 
622  // successful ... return true
623 
624  return true;
625 }
626 
630 inline short IFStreamAscii::readShort()
631 {
632  short s = 0;
633  readShort(s);
634  return s;
635 }
636 
640 inline bool IFStreamAscii::readShort(short& s)
641 {
642  string ss;
643 
644  // read token into ss ... if eof or stream not open return false
645 
646  if (!read(ss)) return false;
647 
648  // attempt to scan token into s ... if unable issue error and return false
649 
650  if (sscanf(ss.c_str(), "%hd", &s) != 1)
651  {
652  ostringstream os;
653  os << endl << "ERROR in IFStreamAscii::readShort" << endl
654  << " Could Not Scan Short From Token = " << ss << endl
655  << " On File Line: " << strTotlLinesRead << " ..." << endl;
656  throw GeoTessException(os, __FILE__, __LINE__, 9202);
657  }
658 
659  // successful ... return true
660 
661  return true;
662 }
663 
667 inline int IFStreamAscii::readInteger()
668 {
669  int i = 0;
670  readInteger(i);
671  return i;
672 }
673 
677 inline bool IFStreamAscii::readInteger(int& i)
678 {
679  string si;
680 
681  // read token into si ... if eof or stream not open return false
682 
683  if (!read(si)) return false;
684 
685  // attempt to scan token into i ... if unable issue error and return false
686 
687  if (sscanf(si.c_str(), "%d", &i) != 1)
688  {
689  ostringstream os;
690  os << endl << "ERROR in IFStreamAscii::readInteger" << endl
691  << " Could Not Scan Integer From Token = " << si << endl
692  << " On File Line: " << strTotlLinesRead << " ..." << endl;
693  throw GeoTessException(os, __FILE__, __LINE__, 9203);
694  }
695 
696  // successful ... return true
697 
698  return true;
699 }
700 
704 inline LONG_INT IFStreamAscii::readLong()
705 {
706  LONG_INT l = 0;
707  readLong(l);
708  return l;
709 }
710 
714 inline bool IFStreamAscii::readLong(LONG_INT& l)
715 {
716  string sl;
717 
718  // read token into sl ... if eof or stream not open return false
719 
720  if (!read(sl)) return false;
721 
722  // attempt to scan token into l ... if unable issue error and return false
723 
724  if (sscanf(sl.c_str(), LONG_INT_F, &l) != 1)
725  {
726  ostringstream os;
727  os << endl << "ERROR in IFStreamAscii::readLong" << endl
728  << " Could Not Scan Long From Token = " << sl << endl
729  << " On File Line: " << strTotlLinesRead << " ..." << endl;
730  throw GeoTessException(os, __FILE__, __LINE__, 9204);
731  }
732 
733  // successful ... return true
734 
735  return true;
736 }
737 
741 inline float IFStreamAscii::readFloat()
742 {
743  float f = 0;
744  readFloat(f);
745  return f;
746 }
747 
751 inline bool IFStreamAscii::readFloat(float& f)
752 {
753  string sf;
754 
755  // read token into sf ... if eof or stream not open return false
756 
757  if (!read(sf)) return false;
758 
759  // attempt to scan token into f ... if unable issue error and return false
760 
761  if (sscanf(sf.c_str(), "%f", &f) != 1)
762  {
763  ostringstream os;
764  os << endl << "ERROR in IFStreamAscii::readFloat" << endl
765  << " Could Not Scan Float From Token = " << sf << endl
766  << " On File Line: " << strTotlLinesRead << " ..." << endl;
767  throw GeoTessException(os, __FILE__, __LINE__, 9205);
768  }
769 
770  // successful ... return true
771 
772  return true;
773 }
774 
778 inline double IFStreamAscii::readDouble()
779 {
780  double d = 0;
781  readDouble(d);
782  return d;
783 }
784 
788 inline bool IFStreamAscii::readDouble(double& d)
789 {
790  string sd;
791 
792  // read token into sd ... if eof or stream not open return false
793 
794  if (!read(sd)) return false;
795 
796  // attempt to scan token into d ... if unable issue error and return false
797 
798  if (sscanf(sd.c_str(), "%lf", &d) != 1)
799  {
800  ostringstream os;
801  os << endl << "ERROR in IFStreamAscii::readDouble" << endl
802  << " Could Not Scan Double From Token = " << sd << endl
803  << " On File Line: " << strTotlLinesRead << " ..." << endl;
804  throw GeoTessException(os, __FILE__, __LINE__, 9206);
805  }
806 
807  // successful ... return true
808 
809  return true;
810 }
811 
812 } // end namespace geotess
813 
814 #endif // INTERPOLATOR_OBJECT_H
void writeBool(bool b)
Definition: IFStreamAscii.h:488
bool readType(byte &b)
Read the next byte.
Definition: IFStreamAscii.h:400
int getDataLinesRead() const
Return the current number of data lines read from the input stream.
Definition: IFStreamAscii.h:345
void writeTypeNL(bool b)
Definition: IFStreamAscii.h:491
void writeNL()
Definition: IFStreamAscii.h:516
void writeTypeNL(byte b)
Definition: IFStreamAscii.h:495
void writeTypeNL(float f)
Definition: IFStreamAscii.h:511
void writeType(float f)
Definition: IFStreamAscii.h:510
Definition: ArrayReuse.h:55
const string & getEndBlockCommentDelimiter() const
Return the end block comment delimiter string.
Definition: IFStreamAscii.h:333
void writeDoubleNL(double d)
Definition: IFStreamAscii.h:513
const string & getFileName() const
Return the opened file name.
Definition: IFStreamAscii.h:236
void writeTypeNL(int i)
Definition: IFStreamAscii.h:503
int getBlockCommentLinesRead() const
Return the current number of block comment lines read from the input stream.
Definition: IFStreamAscii.h:363
void writeTypeNL(short s)
Definition: IFStreamAscii.h:499
void writeLongNL(LONG_INT l)
Definition: IFStreamAscii.h:505
void setWhitespaceDelimiters(const string &wsDelims)
Set the whitespace delimiter string.
Definition: IFStreamAscii.h:268
int getBlankLinesRead() const
Return the current number of blank lines read from the input stream.
Definition: IFStreamAscii.h:351
void close()
Close the input stream if it is open.
Definition: IFStreamAscii.h:198
const string & getWhitespaceDelimiters() const
Return the whitespace delimiter string.
Definition: IFStreamAscii.h:274
void writeType(const string &s)
Definition: IFStreamAscii.h:486
bool isOpen()
Returns true if the stream is open.
Definition: IFStreamAscii.h:193
void writeFloatNL(float f)
Definition: IFStreamAscii.h:509
void writeType(double d)
Definition: IFStreamAscii.h:514
int getBytesRead() const
Return the current number of bytes read from the input stream.
Definition: IFStreamAscii.h:369
const string & getCommentDelimiter() const
Return the comment delimiter string.
Definition: IFStreamAscii.h:298
An exception class for all GeoTess objects.
Definition: GeoTessException.h:65
void writeInt(int i)
Definition: IFStreamAscii.h:500
int getTotalLinesRead() const
Return the current number of total lines read from the input stream.
Definition: IFStreamAscii.h:339
void setEndBlockCommentDelimiter(const string &endBlkCmntDelim)
Set the end block comment delimiter string.
Definition: IFStreamAscii.h:327
void writeTypeNL(const string &s)
Definition: IFStreamAscii.h:487
void writeShortNL(short s)
Definition: IFStreamAscii.h:497
void writeShort(short s)
Definition: IFStreamAscii.h:496
#define byte
signed-byte typedef
Definition: CPPGlobals.h:94
Opens ascii file for read and write access.
Definition: IFStreamAscii.h:80
void setCommentDelimiter(const string &cmntDelim)
Set the comment delimiter string.
Definition: IFStreamAscii.h:292
void writeTypeNL(double d)
Definition: IFStreamAscii.h:515
void writeType(byte b)
Definition: IFStreamAscii.h:494
void writeType(bool b)
Definition: IFStreamAscii.h:490
void writeDouble(double d)
Definition: IFStreamAscii.h:512
int getCommentLinesRead() const
Return the current number of comment lines read from the input stream.
Definition: IFStreamAscii.h:357
#define LONG_INT
Definition: CPPGlobals.h:111
void writeLong(LONG_INT l)
Definition: IFStreamAscii.h:504
void writeFloat(float f)
Definition: IFStreamAscii.h:508
const string & getStringDelimiter() const
Return the string delimiter string.
Definition: IFStreamAscii.h:286
void setStringDelimiter(const string &strgDelim)
Set the string delimiter string.
Definition: IFStreamAscii.h:280
void setBlockCommentDelimiters(const string &begBlk, const string &endBlk)
Sets the begin and end block comment strings for this IFStreamAscii object.
Definition: IFStreamAscii.h:305
const string & getBeginBlockCommentDelimiter() const
Return the begin block comment delimiter string.
Definition: IFStreamAscii.h:321
IFStreamAscii()
Default constructor.
Definition: IFStreamAscii.h:172
void writeByte(byte b)
Definition: IFStreamAscii.h:492
void writeType(short s)
Definition: IFStreamAscii.h:498
bool readType(double &d)
Read the next double.
Definition: IFStreamAscii.h:475
void writeIntNL(int i)
Definition: IFStreamAscii.h:501
bool readType(int &i)
Read the next int.
Definition: IFStreamAscii.h:430
void writeType(LONG_INT l)
Definition: IFStreamAscii.h:506
bool readType(float &f)
Read the next float.
Definition: IFStreamAscii.h:460
void writeTypeNL(LONG_INT l)
Definition: IFStreamAscii.h:507
bool readType(short &s)
Read the next short.
Definition: IFStreamAscii.h:415
void setDelimiters(const string &wspcDelims, const string &strgDelim, const string &cmntDelim, const string &begBlk, const string &endBlk)
Sets the whitespace, string, comment, and block comment delimiters for this IFStreamAscii object...
Definition: IFStreamAscii.h:252
virtual ~IFStreamAscii()
Destructor.
Definition: IFStreamAscii.h:182
#define GEOTESS_EXP_IMP
Definition: CPPGlobals.h:71
#define LONG_INT_F
Definition: CPPGlobals.h:112
void writeBoolNL(bool b)
Definition: IFStreamAscii.h:489
void writeByteNL(byte b)
Definition: IFStreamAscii.h:493
bool readType(LONG_INT &l)
Read the next long.
Definition: IFStreamAscii.h:445
void getline(string &s)
Read a single line from the underlying istream.
Definition: IFStreamAscii.h:167
void writeString(const string &s)
Definition: IFStreamAscii.h:484
void writeStringNL(const string &s)
Definition: IFStreamAscii.h:485
void setBeginBlockCommentDelimiter(const string &begBlkCmntDelim)
Set the begin block comment delimiter string.
Definition: IFStreamAscii.h:315
void writeType(int i)
Definition: IFStreamAscii.h:502
void flush()
Close the input stream if it is open.
Definition: IFStreamAscii.h:209