Lemma is an Electromagnetics API
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ASCIIParser.cpp 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* This file is part of Lemma, a geophysical modelling and inversion API.
  2. * More information is available at http://lemmasoftware.org
  3. */
  4. /* This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  7. */
  8. /**
  9. * @file
  10. * @date 09/23/2013 02:33:41 PM
  11. * @version $Id$
  12. * @author Trevor Irons (ti)
  13. * @email Trevor.Irons@xri-geo.com
  14. * @copyright Copyright (c) 2013, Trevor Irons
  15. */
  16. #include "ASCIIParser.h"
  17. namespace Lemma {
  18. // ==================== FRIEND METHODS =====================
  19. std::ostream &operator << (std::ostream &stream, const ASCIIParser &ob) {
  20. stream << ob.Serialize() << "\n---\n"; // End of doc --- as a direct stream should encapulste thingy
  21. return stream;
  22. }
  23. // ==================== LIFECYCLE =======================
  24. //--------------------------------------------------------------------------------------
  25. // Class: ASCIIParser
  26. // Method: ASCIIParser
  27. // Description: constructor (protected)
  28. //--------------------------------------------------------------------------------------
  29. ASCIIParser::ASCIIParser ( ) : LemmaObject( ),
  30. CommentString("//"), BufferSize(255) {
  31. } // ----- end of method ASCIIParser::ASCIIParser (constructor) -----
  32. //--------------------------------------------------------------------------------------
  33. // Class: ASCIIParser
  34. // Method: NewSP()
  35. // Description: public smart pointer factory constructor
  36. //--------------------------------------------------------------------------------------
  37. std::shared_ptr< ASCIIParser > ASCIIParser::NewSP() {
  38. std::shared_ptr<ASCIIParser> sp(new ASCIIParser( ), LemmaObjectDeleter() );
  39. return sp;
  40. //return std::make_shared<ASCIIParser>();
  41. }
  42. //--------------------------------------------------------------------------------------
  43. // Class: ASCIIParser
  44. // Method: ~ASCIIParser
  45. // Description: destructor (protected)
  46. //--------------------------------------------------------------------------------------
  47. ASCIIParser::~ASCIIParser () {
  48. } // ----- end of method ASCIIParser::~ASCIIParser (destructor) -----
  49. //--------------------------------------------------------------------------------------
  50. // Class: ASCIIParser
  51. // Method: Release
  52. // Description: destructor (protected)
  53. //--------------------------------------------------------------------------------------
  54. void ASCIIParser::Release() {
  55. delete this;
  56. }
  57. //--------------------------------------------------------------------------------------
  58. // Class: ASCIIParser
  59. // Method: Open
  60. //--------------------------------------------------------------------------------------
  61. void ASCIIParser::Open ( const std::string& fname ) {
  62. input.open(fname.c_str(), std::ios::in);
  63. if (input.fail()) {
  64. throw GenericFileIOError(this, fname);
  65. }
  66. return ;
  67. } // ----- end of method ASCIIParser::Open -----
  68. //--------------------------------------------------------------------------------------
  69. // Class: ASCIIParser
  70. // Method: Close
  71. //--------------------------------------------------------------------------------------
  72. void ASCIIParser::Close ( ) {
  73. input.close();
  74. return ;
  75. } // ----- end of method ASCIIParser::Close -----
  76. //--------------------------------------------------------------------------------------
  77. // Class: ASCIIParser
  78. // Method: ReadReals
  79. //--------------------------------------------------------------------------------------
  80. std::vector<Real> ASCIIParser::ReadReals ( const int& nr ) {
  81. std::string buf;
  82. char *dump = new char[BufferSize];
  83. std::vector<Real> vals(0);
  84. while (input >> buf) {
  85. if (buf.substr(0, CommentString.size()) == CommentString) {
  86. input.getline(dump, BufferSize);
  87. } else {
  88. vals.push_back( atof(buf.c_str() ));
  89. }
  90. if (static_cast<int>(vals.size()) == nr) {
  91. delete [] dump;
  92. return vals;
  93. }
  94. }
  95. delete [] dump;
  96. return vals;
  97. } // ----- end of method ASCIIParser::ReadReals -----
  98. //--------------------------------------------------------------------------------------
  99. // Class: ASCIIParser
  100. // Method: ReadInts
  101. //--------------------------------------------------------------------------------------
  102. std::vector<int> ASCIIParser::ReadInts ( const int& nr ) {
  103. std::string buf;
  104. char *dump = new char[BufferSize];
  105. std::vector<int> vals(0);
  106. while (input >> buf) {
  107. if (buf.substr(0, CommentString.size()) == CommentString) {
  108. input.getline(dump, BufferSize);
  109. } else {
  110. vals.push_back( atoi(buf.c_str() ));
  111. }
  112. if (static_cast<int>(vals.size()) == nr) {
  113. delete [] dump;
  114. return vals;
  115. }
  116. }
  117. delete [] dump;
  118. return vals;
  119. } // ----- end of method ASCIIParser::ReadInts -----
  120. //--------------------------------------------------------------------------------------
  121. // Class: ASCIIParser
  122. // Method: ReadStings
  123. //--------------------------------------------------------------------------------------
  124. std::vector<std::string> ASCIIParser::ReadStrings ( const int& nr ) {
  125. std::string buf;
  126. char *dump = new char[BufferSize];
  127. std::vector<std::string> vals(0);
  128. while (input >> buf) {
  129. if (buf.substr(0, CommentString.size()) == CommentString) {
  130. input.getline(dump, BufferSize);
  131. } else {
  132. vals.push_back( buf );
  133. }
  134. if (static_cast<int>(vals.size()) == nr) {
  135. delete [] dump;
  136. return vals;
  137. }
  138. }
  139. delete [] dump;
  140. return vals;
  141. } // ----- end of method ASCIIParser::ReadInts -----
  142. //--------------------------------------------------------------------------------------
  143. // Class: ASCIIParser
  144. // Method: SetCommentString
  145. //--------------------------------------------------------------------------------------
  146. void ASCIIParser::SetCommentString ( const std::string& key ) {
  147. CommentString = key;
  148. } // ----- end of method ASCIIParser::SetCommentString -----
  149. //--------------------------------------------------------------------------------------
  150. // Class: ASCIIParser
  151. // Method: SetBufferSize
  152. //--------------------------------------------------------------------------------------
  153. void ASCIIParser::SetBufferSize ( const int& size ) {
  154. BufferSize = size;
  155. } // ----- end of method ASCIIParser::SetCommentString -----
  156. //--------------------------------------------------------------------------------------
  157. // Class: ASCIIParser
  158. // Method: GetFileLocation
  159. //--------------------------------------------------------------------------------------
  160. int ASCIIParser::GetFileLocation ( ) {
  161. return input.tellg();
  162. } // ----- end of method ASCIIParser::GetFileLocation -----
  163. //--------------------------------------------------------------------------------------
  164. // Class: ASCIIParser
  165. // Method: JumpToLocation
  166. //--------------------------------------------------------------------------------------
  167. void ASCIIParser::JumpToLocation ( const int& loc ) {
  168. input.seekg( loc );
  169. return ;
  170. } // ----- end of method ASCIIParser::JumpToLocation -----
  171. } // ----- end of Lemma name -----