GeoTessCPP  2.6.1
Software to facilitate storage and retrieval of 3D information about the Earth.
All Classes Namespaces Files Functions Variables Typedefs Friends Macros
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 
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
#define GEOTESS_EXP_IMP
Definition: CPPGlobals.h:71
#define LONG_INT_F
Definition: CPPGlobals.h:112
#define LONG_INT
Definition: CPPGlobals.h:111
An exception class for all GeoTess objects.
Opens ascii file for read and write access.
Definition: IFStreamAscii.h:81
void setWhitespaceDelimiters(const string &wsDelims)
bool readType(byte &b)
void setEndBlockCommentDelimiter(const string &endBlkCmntDelim)
void writeLongNL(LONG_INT l)
void writeShortNL(short s)
bool readLine(string &ln)
const string & getBeginBlockCommentDelimiter() const
void setBeginBlockCommentDelimiter(const string &begBlkCmntDelim)
void writeType(short s)
bool readType(short &s)
void writeFloat(float f)
bool readType(LONG_INT &l)
void writeType(double d)
bool readType(double &d)
void setBlockCommentDelimiters(const string &begBlk, const string &endBlk)
void setStringDelimiter(const string &strgDelim)
void openForWrite(const string &fn)
void setDelimiters(const string &wspcDelims, const string &strgDelim, const string &cmntDelim, const string &begBlk, const string &endBlk)
void tokenize(const string &str, vector< string > &tokens)
void openForRead(const string &fn)
void getLine(string &buf)
void writeTypeNL(float f)
int getBlockCommentLinesRead() const
int getCommentLinesRead() const
void writeTypeNL(const string &s)
void writeType(const string &s)
int getDataLinesRead() const
void writeFloatNL(float f)
void writeByteNL(byte b)
void writeType(LONG_INT l)
void writeTypeNL(LONG_INT l)
void writeLong(LONG_INT l)
void setCommentDelimiter(const string &cmntDelim)
void writeType(float f)
void writeString(const string &s)
const string & getFileName() const
const string & getCommentDelimiter() const
void writeDoubleNL(double d)
const string & getEndBlockCommentDelimiter() const
void getline(string &s)
void writeShort(short s)
bool readType(float &f)
void writeTypeNL(bool b)
void writeTypeNL(short s)
void writeBoolNL(bool b)
int getTotalLinesRead() const
void writeTypeNL(byte b)
void writeStringNL(const string &s)
int getBlankLinesRead() const
void writeDouble(double d)
void writeTypeNL(double d)
const string & getStringDelimiter() const
const string & getWhitespaceDelimiters() const