GeoTessCPP
2.0.0
Software to facilitate storage and retrieval of 3D information about the Earth.
|
00001 //- **************************************************************************** 00002 //- 00003 //- Copyright 2009 Sandia Corporation. Under the terms of Contract 00004 //- DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government 00005 //- retains certain rights in this software. 00006 //- 00007 //- BSD Open Source License. 00008 //- All rights reserved. 00009 //- 00010 //- Redistribution and use in source and binary forms, with or without 00011 //- modification, are permitted provided that the following conditions are met: 00012 //- 00013 //- * Redistributions of source code must retain the above copyright notice, 00014 //- this list of conditions and the following disclaimer. 00015 //- * Redistributions in binary form must reproduce the above copyright 00016 //- notice, this list of conditions and the following disclaimer in the 00017 //- documentation and/or other materials provided with the distribution. 00018 //- * Neither the name of Sandia National Laboratories nor the names of its 00019 //- contributors may be used to endorse or promote products derived from 00020 //- this software without specific prior written permission. 00021 //- 00022 //- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00023 //- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00024 //- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00025 //- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 00026 //- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00027 //- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00028 //- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00029 //- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00030 //- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00031 //- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 //- POSSIBILITY OF SUCH DAMAGE. 00033 //- 00034 //- **************************************************************************** 00035 00036 #ifndef IFSTREAMASCII_OBJECT_H 00037 #define IFSTREAMASCII_OBJECT_H 00038 00039 // **** _SYSTEM INCLUDES_ ****************************************************** 00040 00041 #include <iostream> 00042 #include <string> 00043 #include <fstream> 00044 #include <vector> 00045 #include <sstream> 00046 00047 // use standard library objects 00048 using namespace std; 00049 00050 // **** _LOCAL INCLUDES_ ******************************************************* 00051 00052 #include "CPPUtils.h" 00053 #include "GeoTessUtils.h" 00054 #include "GeoTessException.h" 00055 00056 // **** _BEGIN GEOTESS NAMESPACE_ ********************************************** 00057 00058 namespace geotess { 00059 00060 // **** _FORWARD REFERENCES_ *************************************************** 00061 00062 // **** _CLASS DEFINITION_ ***************************************************** 00063 00080 class GEOTESS_EXP_IMP IFStreamAscii 00081 { 00082 private: 00083 00087 ifstream ifs; 00088 00092 ofstream ofs; 00093 00109 string strDelim[5]; 00110 00114 string strFileName; 00115 00116 // The total number of lines read from this stream 00117 int strTotlLinesRead; 00118 // The total number of "data" lines read from this stream 00119 int strDataLinesRead; 00120 // The total number of "blank" lines read from this stream 00121 int strBlankLinesRead; 00122 // The total number of "comment" lines read from this stream 00123 int strCommentLinesRead; 00124 // The total number of "block comment" lines read from this stream 00125 int strBlkCommentLinesRead; 00126 // The total number of "bytes" read from this stream 00127 int strBytesRead; 00128 00133 bool strBlkCommntSet; 00134 00142 int strTokenPtr; 00143 00154 vector<string> strTokens; 00155 00156 public: 00157 00161 void getLine(string& buf); 00162 00167 void getline(string& s) { std::getline(ifs, s); }; 00168 00172 IFStreamAscii() : strFileName(""), strTotlLinesRead(0), 00173 strDataLinesRead(0), strBlankLinesRead(0), 00174 strCommentLinesRead(0), strBlkCommentLinesRead(0), 00175 strBytesRead(0), strBlkCommntSet(false), 00176 strTokenPtr(0) 00177 {setDefaultDelimiters(); setCommentDelimiter("#"); }; 00178 00182 virtual ~IFStreamAscii() { close(); }; 00183 00187 void openForRead(const string& fn); 00188 void openForWrite(const string& fn); 00189 00193 bool isOpen() { return (ifs.is_open() || ofs.is_open()) ? true : false; } 00194 00198 void close() 00199 { 00200 if (ifs.is_open()) 00201 ifs.close(); 00202 else if (ofs.is_open()) 00203 ofs.close(); 00204 } 00205 00209 void flush() 00210 { 00211 if (ofs.is_open()) 00212 ofs.flush(); 00213 } 00214 00219 bool read(string& token); 00220 00225 bool readLine(string& ln); 00226 00231 void tokenize(const string& str, vector<string>& tokens); 00232 00236 const string& getFileName() const { return strFileName; }; 00237 00241 void setDefaultDelimiters(); 00242 00246 void resetReader(); 00247 00252 void setDelimiters(const string& wspcDelims, 00253 const string& strgDelim, 00254 const string& cmntDelim, 00255 const string& begBlk, 00256 const string& endBlk) 00257 { 00258 strDelim[0] = wspcDelims; 00259 strDelim[1] = strgDelim; 00260 strDelim[2] = cmntDelim; 00261 strDelim[3] = begBlk; 00262 strDelim[4] = endBlk; 00263 } 00264 00268 void setWhitespaceDelimiters(const string& wsDelims) 00269 { strDelim[0] = wsDelims; }; 00270 00274 const string& getWhitespaceDelimiters() const 00275 { return strDelim[0]; }; 00276 00280 void setStringDelimiter(const string& strgDelim) 00281 { strDelim[1] = strgDelim; }; 00282 00286 const string& getStringDelimiter() const 00287 { return strDelim[1]; }; 00288 00292 void setCommentDelimiter(const string& cmntDelim) 00293 { strDelim[2] = cmntDelim; }; 00294 00298 const string& getCommentDelimiter() const 00299 { return strDelim[2]; }; 00300 00305 void setBlockCommentDelimiters(const string& begBlk, 00306 const string& endBlk) 00307 { 00308 strDelim[3] = begBlk; 00309 strDelim[4] = endBlk; 00310 } 00311 00315 void setBeginBlockCommentDelimiter(const string& begBlkCmntDelim) 00316 { strDelim[3] = begBlkCmntDelim; }; 00317 00321 const string& getBeginBlockCommentDelimiter() const 00322 { return strDelim[3]; }; 00323 00327 void setEndBlockCommentDelimiter(const string& endBlkCmntDelim) 00328 { strDelim[4] = endBlkCmntDelim; }; 00329 00333 const string& getEndBlockCommentDelimiter() const 00334 { return strDelim[4]; }; 00335 00339 int getTotalLinesRead() const 00340 { return strTotlLinesRead; }; 00341 00345 int getDataLinesRead() const 00346 { return strDataLinesRead; }; 00347 00351 int getBlankLinesRead() const 00352 { return strBlankLinesRead; }; 00353 00357 int getCommentLinesRead() const 00358 { return strCommentLinesRead; }; 00359 00363 int getBlockCommentLinesRead() const 00364 { return strBlkCommentLinesRead; }; 00365 00369 int getBytesRead() const 00370 { return strBytesRead; }; 00371 00375 bool isEOF() const; 00376 00380 bool next(); 00381 00385 string readString(); 00386 00390 bool readString(string& s); 00391 00395 byte readByte(); 00396 00400 bool readType(byte& b) { return readByte(b); }; 00401 00405 bool readByte(byte& b); 00406 00410 short readShort(); 00411 00415 bool readType(short& s) { return readShort(s); }; 00416 00420 bool readShort(short& s); 00421 00425 int readInteger(); 00426 00430 bool readType(int& i) { return readInteger(i); }; 00431 00435 bool readInteger(int& i); 00436 00440 LONG_INT readLong(); 00441 00445 bool readType(LONG_INT& l) { return readLong(l); }; 00446 00450 bool readLong(LONG_INT& l); 00451 00455 float readFloat(); 00456 00460 bool readType(float& f) { return readFloat(f); }; 00461 00465 bool readFloat(float& f); 00466 00470 double readDouble(); 00471 00475 bool readType(double& d) { return readDouble(d); }; 00476 00480 bool readDouble(double& d); 00481 00482 // ***** Write functions ************************************************** 00483 00484 void writeString(const string& s) {ofs << s;} 00485 void writeStringNL(const string& s) {ofs << s << endl;} 00486 void writeType(const string& s) {ofs << s;} 00487 void writeTypeNL(const string& s) {ofs << s << endl;} 00488 void writeBool(bool b) {ofs << b;} 00489 void writeBoolNL(bool b) {ofs << b << endl;} 00490 void writeType(bool b) {ofs << b;} 00491 void writeTypeNL(bool b) {ofs << b << endl;} 00492 void writeByte(byte b) {ofs << (int)b;} 00493 void writeByteNL(byte b) {ofs << (int)b << endl;} 00494 void writeType(byte b) {ofs << (int)b;} 00495 void writeTypeNL(byte b) {ofs << (int)b << endl;} 00496 void writeShort(short s) {ofs << s;} 00497 void writeShortNL(short s) {ofs << s << endl;} 00498 void writeType(short s) {ofs << s;}; 00499 void writeTypeNL(short s) {ofs << s << endl;}; 00500 void writeInt(int i) {ofs << i;} 00501 void writeIntNL(int i) {ofs << i << endl;} 00502 void writeType(int i) {ofs << i;} 00503 void writeTypeNL(int i) {ofs << i << endl;} 00504 void writeLong(LONG_INT l) {ofs << l;} 00505 void writeLongNL(LONG_INT l) {ofs << l << endl;} 00506 void writeType(LONG_INT l) {ofs << l;} 00507 void writeTypeNL(LONG_INT l) {ofs << l << endl;} 00508 void writeFloat(float f) {ofs << f;} 00509 void writeFloatNL(float f) {ofs << f << endl;} 00510 void writeType(float f) {ofs << f;} 00511 void writeTypeNL(float f) {ofs << f << endl;} 00512 void writeDouble(double d) {ofs << d;} 00513 void writeDoubleNL(double d) {ofs << d << endl;} 00514 void writeType(double d) {ofs << d;} 00515 void writeTypeNL(double d) {ofs << d << endl;} 00516 void writeNL() {ofs << endl;} 00517 00518 }; // end class IFStreamAscii 00519 00526 inline bool IFStreamAscii::read(string& token) 00527 { 00528 string ln; 00529 00530 // if the strTokenPtr is >= than strTokens.size() then get more tokens 00531 00532 if (strTokenPtr >= (int) strTokens.size()) 00533 { 00534 // get more tokens ... reset token pointer and list 00535 00536 strTokenPtr = 0; 00537 strTokens.clear(); 00538 00539 // read another data line from the stream ... if eof() is reached return 00540 // true 00541 00542 if (!readLine(ln)) return false; 00543 00544 // tokennize the line into the tokens list 00545 00546 tokenize(ln, strTokens); 00547 } 00548 00549 // get the token ... increment the pointer ... and return false for success 00550 00551 token = strTokens[strTokenPtr++]; 00552 return true; 00553 } 00554 00558 inline bool IFStreamAscii::isEOF() const 00559 { 00560 return (ifs.eof() && (strTokenPtr >= (int) strTokens.size())); 00561 } 00562 00566 inline string IFStreamAscii::readString() 00567 { 00568 string s; 00569 read(s); 00570 return s; 00571 } 00572 00576 inline bool IFStreamAscii::readString(string& s) 00577 { 00578 return read(s); 00579 } 00580 00584 inline bool IFStreamAscii::next() 00585 { 00586 string s; 00587 return read(s); 00588 } 00589 00593 inline byte IFStreamAscii::readByte() 00594 { 00595 byte b = 0; 00596 readByte(b); 00597 return b; 00598 } 00599 00603 inline bool IFStreamAscii::readByte(byte& b) 00604 { 00605 string sb; 00606 00607 // read token into sb ... if eof or stream not open return false 00608 00609 if (!read(sb)) return false; 00610 00611 // attempt to scan token into b ... if unable issue error and return false 00612 00613 if (sscanf(sb.c_str(), "%hhd", &b) != 1) 00614 { 00615 ostringstream os; 00616 os << endl << "ERROR in IFStreamAscii::readByte" << endl 00617 << " Could Not Scan Byte From Token = " << sb << endl 00618 << " On File Line: " << strTotlLinesRead << " ..." << endl; 00619 throw GeoTessException(os, __FILE__, __LINE__, 9201); 00620 } 00621 00622 // successful ... return true 00623 00624 return true; 00625 } 00626 00630 inline short IFStreamAscii::readShort() 00631 { 00632 short s = 0; 00633 readShort(s); 00634 return s; 00635 } 00636 00640 inline bool IFStreamAscii::readShort(short& s) 00641 { 00642 string ss; 00643 00644 // read token into ss ... if eof or stream not open return false 00645 00646 if (!read(ss)) return false; 00647 00648 // attempt to scan token into s ... if unable issue error and return false 00649 00650 if (sscanf(ss.c_str(), "%hd", &s) != 1) 00651 { 00652 ostringstream os; 00653 os << endl << "ERROR in IFStreamAscii::readShort" << endl 00654 << " Could Not Scan Short From Token = " << ss << endl 00655 << " On File Line: " << strTotlLinesRead << " ..." << endl; 00656 throw GeoTessException(os, __FILE__, __LINE__, 9202); 00657 } 00658 00659 // successful ... return true 00660 00661 return true; 00662 } 00663 00667 inline int IFStreamAscii::readInteger() 00668 { 00669 int i = 0; 00670 readInteger(i); 00671 return i; 00672 } 00673 00677 inline bool IFStreamAscii::readInteger(int& i) 00678 { 00679 string si; 00680 00681 // read token into si ... if eof or stream not open return false 00682 00683 if (!read(si)) return false; 00684 00685 // attempt to scan token into i ... if unable issue error and return false 00686 00687 if (sscanf(si.c_str(), "%d", &i) != 1) 00688 { 00689 ostringstream os; 00690 os << endl << "ERROR in IFStreamAscii::readInteger" << endl 00691 << " Could Not Scan Integer From Token = " << si << endl 00692 << " On File Line: " << strTotlLinesRead << " ..." << endl; 00693 throw GeoTessException(os, __FILE__, __LINE__, 9203); 00694 } 00695 00696 // successful ... return true 00697 00698 return true; 00699 } 00700 00704 inline LONG_INT IFStreamAscii::readLong() 00705 { 00706 LONG_INT l = 0; 00707 readLong(l); 00708 return l; 00709 } 00710 00714 inline bool IFStreamAscii::readLong(LONG_INT& l) 00715 { 00716 string sl; 00717 00718 // read token into sl ... if eof or stream not open return false 00719 00720 if (!read(sl)) return false; 00721 00722 // attempt to scan token into l ... if unable issue error and return false 00723 00724 if (sscanf(sl.c_str(), LONG_INT_F, &l) != 1) 00725 { 00726 ostringstream os; 00727 os << endl << "ERROR in IFStreamAscii::readLong" << endl 00728 << " Could Not Scan Long From Token = " << sl << endl 00729 << " On File Line: " << strTotlLinesRead << " ..." << endl; 00730 throw GeoTessException(os, __FILE__, __LINE__, 9204); 00731 } 00732 00733 // successful ... return true 00734 00735 return true; 00736 } 00737 00741 inline float IFStreamAscii::readFloat() 00742 { 00743 float f = 0; 00744 readFloat(f); 00745 return f; 00746 } 00747 00751 inline bool IFStreamAscii::readFloat(float& f) 00752 { 00753 string sf; 00754 00755 // read token into sf ... if eof or stream not open return false 00756 00757 if (!read(sf)) return false; 00758 00759 // attempt to scan token into f ... if unable issue error and return false 00760 00761 if (sscanf(sf.c_str(), "%f", &f) != 1) 00762 { 00763 ostringstream os; 00764 os << endl << "ERROR in IFStreamAscii::readFloat" << endl 00765 << " Could Not Scan Float From Token = " << sf << endl 00766 << " On File Line: " << strTotlLinesRead << " ..." << endl; 00767 throw GeoTessException(os, __FILE__, __LINE__, 9205); 00768 } 00769 00770 // successful ... return true 00771 00772 return true; 00773 } 00774 00778 inline double IFStreamAscii::readDouble() 00779 { 00780 double d = 0; 00781 readDouble(d); 00782 return d; 00783 } 00784 00788 inline bool IFStreamAscii::readDouble(double& d) 00789 { 00790 string sd; 00791 00792 // read token into sd ... if eof or stream not open return false 00793 00794 if (!read(sd)) return false; 00795 00796 // attempt to scan token into d ... if unable issue error and return false 00797 00798 if (sscanf(sd.c_str(), "%lf", &d) != 1) 00799 { 00800 ostringstream os; 00801 os << endl << "ERROR in IFStreamAscii::readDouble" << endl 00802 << " Could Not Scan Double From Token = " << sd << endl 00803 << " On File Line: " << strTotlLinesRead << " ..." << endl; 00804 throw GeoTessException(os, __FILE__, __LINE__, 9206); 00805 } 00806 00807 // successful ... return true 00808 00809 return true; 00810 } 00811 00812 } // end namespace geotess 00813 00814 #endif // INTERPOLATOR_OBJECT_H