GeoTessCPP: include/IFStreamAscii.h Source File

GeoTessCPP 2.0.0 Software to facilitate storage and retrieval of 3D information about the Earth.

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
80 class GEOTESS_EXP_IMP IFStreamAscii
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
166 IFStreamAscii() : strFileName(""), strTotlLinesRead(0),
167 strDataLinesRead(0), strBlankLinesRead(0),
168 strCommentLinesRead(0), strBlkCommentLinesRead(0),
169 strBytesRead(0), strBlkCommntSet(false),
170 strTokenPtr(0)
171 {setDefaultDelimiters(); setCommentDelimiter("#"); };
172
176 virtual ~IFStreamAscii() { close(); };
177
181 void openForRead(const string& fn);
182 void openForWrite(const string& fn);
183
187 bool isOpen() { return (ifs.is_open() || ofs.is_open()) ? true : false; }
188
192 void close()
193 {
194 if (ifs.is_open())
195 ifs.close();
196 else if (ofs.is_open())
197 ofs.close();
198 }
199
203 void flush()
204 {
205 if (ofs.is_open())
206 ofs.flush();
207 }
208
213 bool read(string& token);
214
219 bool readLine(string& ln);
220
225 void tokenize(const string& str, vector<string>& tokens);
226
230 const string& getFileName() const { return strFileName; };
231
235 void setDefaultDelimiters();
236
240 void resetReader();
241
246 void setDelimiters(const string& wspcDelims,
247 const string& strgDelim,
248 const string& cmntDelim,
249 const string& begBlk,
250 const string& endBlk)
251 {
252 strDelim[0] = wspcDelims;
253 strDelim[1] = strgDelim;
254 strDelim[2] = cmntDelim;
255 strDelim[3] = begBlk;
256 strDelim[4] = endBlk;
257 }
258
262 void setWhitespaceDelimiters(const string& wsDelims)
263 { strDelim[0] = wsDelims; };
264
268 const string& getWhitespaceDelimiters() const
269 { return strDelim[0]; };
270
274 void setStringDelimiter(const string& strgDelim)
275 { strDelim[1] = strgDelim; };
276
280 const string& getStringDelimiter() const
281 { return strDelim[1]; };
282
286 void setCommentDelimiter(const string& cmntDelim)
287 { strDelim[2] = cmntDelim; };
288
292 const string& getCommentDelimiter() const
293 { return strDelim[2]; };
294
299 void setBlockCommentDelimiters(const string& begBlk,
300 const string& endBlk)
301 {
302 strDelim[3] = begBlk;
303 strDelim[4] = endBlk;
304 }
305
309 void setBeginBlockCommentDelimiter(const string& begBlkCmntDelim)
310 { strDelim[3] = begBlkCmntDelim; };
311
315 const string& getBeginBlockCommentDelimiter() const
316 { return strDelim[3]; };
317
321 void setEndBlockCommentDelimiter(const string& endBlkCmntDelim)
322 { strDelim[4] = endBlkCmntDelim; };
323
327 const string& getEndBlockCommentDelimiter() const
328 { return strDelim[4]; };
329
333 int getTotalLinesRead() const
334 { return strTotlLinesRead; };
335
339 int getDataLinesRead() const
340 { return strDataLinesRead; };
341
345 int getBlankLinesRead() const
346 { return strBlankLinesRead; };
347
351 int getCommentLinesRead() const
352 { return strCommentLinesRead; };
353
357 int getBlockCommentLinesRead() const
358 { return strBlkCommentLinesRead; };
359
363 int getBytesRead() const
364 { return strBytesRead; };
365
369 bool isEOF() const;
370
374 bool next();
375
379 string readString();
380
384 bool readString(string& s);
385
389 byte readByte();
390
394 bool readType(byte& b) { return readByte(b); };
395
399 bool readByte(byte& b);
400
404 short readShort();
405
409 bool readType(short& s) { return readShort(s); };
410
414 bool readShort(short& s);
415
419 int readInteger();
420
424 bool readType(int& i) { return readInteger(i); };
425
429 bool readInteger(int& i);
430
434 LONG_INT readLong();
435
439 bool readType(LONG_INT& l) { return readLong(l); };
440
444 bool readLong(LONG_INT& l);
445
449 float readFloat();
450
454 bool readType(float& f) { return readFloat(f); };
455
459 bool readFloat(float& f);
460
464 double readDouble();
465
469 bool readType(double& d) { return readDouble(d); };
470
474 bool readDouble(double& d);
475
476 // ***** Write functions **************************************************
477
478 void writeString(const string& s) {ofs << s;}
479 void writeStringNL(const string& s) {ofs << s << endl;}
480 void writeType(const string& s) {ofs << s;}
481 void writeTypeNL(const string& s) {ofs << s << endl;}
482 void writeBool(bool b) {ofs << b;}
483 void writeBoolNL(bool b) {ofs << b << endl;}
484 void writeType(bool b) {ofs << b;}
485 void writeTypeNL(bool b) {ofs << b << endl;}
486 void writeByte(byte b) {ofs << (int)b;}
487 void writeByteNL(byte b) {ofs << (int)b << endl;}
488 void writeType(byte b) {ofs << (int)b;}
489 void writeTypeNL(byte b) {ofs << (int)b << endl;}
490 void writeShort(short s) {ofs << s;}
491 void writeShortNL(short s) {ofs << s << endl;}
492 void writeType(short s) {ofs << s;};
493 void writeTypeNL(short s) {ofs << s << endl;};
494 void writeInt(int i) {ofs << i;}
495 void writeIntNL(int i) {ofs << i << endl;}
496 void writeType(int i) {ofs << i;}
497 void writeTypeNL(int i) {ofs << i << endl;}
498 void writeLong(LONG_INT l) {ofs << l;}
499 void writeLongNL(LONG_INT l) {ofs << l << endl;}
500 void writeType(LONG_INT l) {ofs << l;}
501 void writeTypeNL(LONG_INT l) {ofs << l << endl;}
502 void writeFloat(float f) {ofs << f;}
503 void writeFloatNL(float f) {ofs << f << endl;}
504 void writeType(float f) {ofs << f;}
505 void writeTypeNL(float f) {ofs << f << endl;}
506 void writeDouble(double d) {ofs << d;}
507 void writeDoubleNL(double d) {ofs << d << endl;}
508 void writeType(double d) {ofs << d;}
509 void writeTypeNL(double d) {ofs << d << endl;}
510 void writeNL() {ofs << endl;}
511
512 }; // end class IFStreamAscii
513
520 inline bool IFStreamAscii::read(string& token)
521 {
522 string ln;
523
524 // if the strTokenPtr is >= than strTokens.size() then get more tokens
525
526 if (strTokenPtr >= (int) strTokens.size())
527 {
528 // get more tokens ... reset token pointer and list
529
530 strTokenPtr = 0;
531 strTokens.clear();
532
533 // read another data line from the stream ... if eof() is reached return
534 // true
535
536 if (!readLine(ln)) return false;
537
538 // tokennize the line into the tokens list
539
540 tokenize(ln, strTokens);
541 }
542
543 // get the token ... increment the pointer ... and return false for success
544
545 token = strTokens[strTokenPtr++];
546 return true;
547 }
548
552 inline bool IFStreamAscii::isEOF() const
553 {
554 return (ifs.eof() && (strTokenPtr >= (int) strTokens.size()));
555 }
556
560 inline string IFStreamAscii::readString()
561 {
562 string s;
563 read(s);
564 return s;
565 }
566
570 inline bool IFStreamAscii::readString(string& s)
571 {
572 return read(s);
573 }
574
578 inline bool IFStreamAscii::next()
579 {
580 string s;
581 return read(s);
582 }
583
587 inline byte IFStreamAscii::readByte()
588 {
589 byte b = 0;
590 readByte(b);
591 return b;
592 }
593
597 inline bool IFStreamAscii::readByte(byte& b)
598 {
599 string sb;
600
601 // read token into sb ... if eof or stream not open return false
602
603 if (!read(sb)) return false;
604
605 // attempt to scan token into b ... if unable issue error and return false
606
607 if (sscanf(sb.c_str(), "%hhd", &b) != 1)
608 {
609 ostringstream os;
610 os << endl << "ERROR in IFStreamAscii::readByte" << endl
611 << " Could Not Scan Byte From Token = " << sb << endl
612 << " On File Line: " << strTotlLinesRead << " ..." << endl;
613 throw GeoTessException(os, __FILE__, __LINE__, 9201);
614 }
615
616 // successful ... return true
617
618 return true;
619 }
620
624 inline short IFStreamAscii::readShort()
625 {
626 short s = 0;
627 readShort(s);
628 return s;
629 }
630
634 inline bool IFStreamAscii::readShort(short& s)
635 {
636 string ss;
637
638 // read token into ss ... if eof or stream not open return false
639
640 if (!read(ss)) return false;
641
642 // attempt to scan token into s ... if unable issue error and return false
643
644 if (sscanf(ss.c_str(), "%hd", &s) != 1)
645 {
646 ostringstream os;
647 os << endl << "ERROR in IFStreamAscii::readShort" << endl
648 << " Could Not Scan Short From Token = " << ss << endl
649 << " On File Line: " << strTotlLinesRead << " ..." << endl;
650 throw GeoTessException(os, __FILE__, __LINE__, 9202);
651 }
652
653 // successful ... return true
654
655 return true;
656 }
657
661 inline int IFStreamAscii::readInteger()
662 {
663 int i = 0;
664 readInteger(i);
665 return i;
666 }
667
671 inline bool IFStreamAscii::readInteger(int& i)
672 {
673 string si;
674
675 // read token into si ... if eof or stream not open return false
676
677 if (!read(si)) return false;
678
679 // attempt to scan token into i ... if unable issue error and return false
680
681 if (sscanf(si.c_str(), "%d", &i) != 1)
682 {
683 ostringstream os;
684 os << endl << "ERROR in IFStreamAscii::readInteger" << endl
685 << " Could Not Scan Integer From Token = " << si << endl
686 << " On File Line: " << strTotlLinesRead << " ..." << endl;
687 throw GeoTessException(os, __FILE__, __LINE__, 9203);
688 }
689
690 // successful ... return true
691
692 return true;
693 }
694
698 inline LONG_INT IFStreamAscii::readLong()
699 {
700 LONG_INT l = 0;
701 readLong(l);
702 return l;
703 }
704
708 inline bool IFStreamAscii::readLong(LONG_INT& l)
709 {
710 string sl;
711
712 // read token into sl ... if eof or stream not open return false
713
714 if (!read(sl)) return false;
715
716 // attempt to scan token into l ... if unable issue error and return false
717
718 if (sscanf(sl.c_str(), LONG_INT_F, &l) != 1)
719 {
720 ostringstream os;
721 os << endl << "ERROR in IFStreamAscii::readLong" << endl
722 << " Could Not Scan Long From Token = " << sl << endl
723 << " On File Line: " << strTotlLinesRead << " ..." << endl;
724 throw GeoTessException(os, __FILE__, __LINE__, 9204);
725 }
726
727 // successful ... return true
728
729 return true;
730 }
731
735 inline float IFStreamAscii::readFloat()
736 {
737 float f = 0;
738 readFloat(f);
739 return f;
740 }
741
745 inline bool IFStreamAscii::readFloat(float& f)
746 {
747 string sf;
748
749 // read token into sf ... if eof or stream not open return false
750
751 if (!read(sf)) return false;
752
753 // attempt to scan token into f ... if unable issue error and return false
754
755 if (sscanf(sf.c_str(), "%f", &f) != 1)
756 {
757 ostringstream os;
758 os << endl << "ERROR in IFStreamAscii::readFloat" << endl
759 << " Could Not Scan Float From Token = " << sf << endl
760 << " On File Line: " << strTotlLinesRead << " ..." << endl;
761 throw GeoTessException(os, __FILE__, __LINE__, 9205);
762 }
763
764 // successful ... return true
765
766 return true;
767 }
768
772 inline double IFStreamAscii::readDouble()
773 {
774 double d = 0;
775 readDouble(d);
776 return d;
777 }
778
782 inline bool IFStreamAscii::readDouble(double& d)
783 {
784 string sd;
785
786 // read token into sd ... if eof or stream not open return false
787
788 if (!read(sd)) return false;
789
790 // attempt to scan token into d ... if unable issue error and return false
791
792 if (sscanf(sd.c_str(), "%lf", &d) != 1)
793 {
794 ostringstream os;
795 os << endl << "ERROR in IFStreamAscii::readDouble" << endl
796 << " Could Not Scan Double From Token = " << sd << endl
797 << " On File Line: " << strTotlLinesRead << " ..." << endl;
798 throw GeoTessException(os, __FILE__, __LINE__, 9206);
799 }
800
801 // successful ... return true
802
803 return true;
804 }
805
806 } // end namespace geotess
807
808 #endif // INTERPOLATOR_OBJECT_H