Main Lemma Repository
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 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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
  21. return stream;
  22. }
  23. // ==================== LIFECYCLE =======================
  24. //--------------------------------------------------------------------------------------
  25. // Class: ASCIIParser
  26. // Method: ASCIIParser
  27. // Description: constructor (locked)
  28. //--------------------------------------------------------------------------------------
  29. ASCIIParser::ASCIIParser ( const ctor_key& ) : LemmaObject( ), input(),
  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. return std::make_shared<ASCIIParser>( ctor_key() );
  39. }
  40. //--------------------------------------------------------------------------------------
  41. // Class: ASCIIParser
  42. // Method: Serialize
  43. //--------------------------------------------------------------------------------------
  44. YAML::Node ASCIIParser::Serialize ( ) const {
  45. YAML::Node node = LemmaObject::Serialize();;
  46. node.SetTag( GetName() );
  47. node["CommentString"] = CommentString;
  48. node["BufferSize"] = BufferSize;
  49. return node;
  50. } // ----- end of method ASCIIParser::Serialize -----
  51. //--------------------------------------------------------------------------------------
  52. // Class: ASCIIParser
  53. // Method: DeSerialize
  54. //--------------------------------------------------------------------------------------
  55. std::shared_ptr<ASCIIParser> ASCIIParser::DeSerialize ( const YAML::Node& node ) {
  56. if (node.Tag() != "ASCIIParser") {
  57. throw DeSerializeTypeMismatch( "ASCIIParser", node.Tag());
  58. }
  59. return std::make_shared< ASCIIParser >( node, ctor_key() ); //, ctor_key() );
  60. } // ----- end of method ASCIIParser::DeSerialize -----
  61. //--------------------------------------------------------------------------------------
  62. // Class: ASCIIParser
  63. // Method: ~ASCIIParser
  64. // Description: destructor (protected)
  65. //--------------------------------------------------------------------------------------
  66. ASCIIParser::~ASCIIParser () {
  67. } // ----- end of method ASCIIParser::~ASCIIParser (destructor) -----
  68. //--------------------------------------------------------------------------------------
  69. // Class: ASCIIParser
  70. // Method: ASCIIParser
  71. // Description: DeSerializing constructor (protected)
  72. //--------------------------------------------------------------------------------------
  73. ASCIIParser::ASCIIParser (const YAML::Node& node, const ctor_key& key) : LemmaObject(node) {
  74. this->CommentString = node["CommentString"].as<std::string>();
  75. this->BufferSize = node["BufferSize"].as<int>();
  76. } // ----- end of method ASCIIParser::ASCIIParser (constructor) -----
  77. //--------------------------------------------------------------------------------------
  78. // Class: ASCIIParser
  79. // Method: Open
  80. //--------------------------------------------------------------------------------------
  81. void ASCIIParser::Open ( const std::string& fname ) {
  82. input.open(fname.c_str(), std::ios::in);
  83. if (input.fail()) {
  84. throw GenericFileIOError(this, fname);
  85. }
  86. return ;
  87. } // ----- end of method ASCIIParser::Open -----
  88. //--------------------------------------------------------------------------------------
  89. // Class: ASCIIParser
  90. // Method: Close
  91. //--------------------------------------------------------------------------------------
  92. void ASCIIParser::Close ( ) {
  93. input.close();
  94. return ;
  95. } // ----- end of method ASCIIParser::Close -----
  96. //--------------------------------------------------------------------------------------
  97. // Class: ASCIIParser
  98. // Method: ReadReals
  99. //--------------------------------------------------------------------------------------
  100. std::vector<Real> ASCIIParser::ReadReals ( const int& nr ) {
  101. std::string buf;
  102. char *dump = new char[BufferSize];
  103. std::vector<Real> vals(0);
  104. while (input >> buf) {
  105. if (buf.substr(0, CommentString.size()) == CommentString) {
  106. input.getline(dump, BufferSize);
  107. } else {
  108. vals.push_back( atof(buf.c_str() ));
  109. }
  110. if (static_cast<int>(vals.size()) == nr) {
  111. delete [] dump;
  112. return vals;
  113. }
  114. }
  115. delete [] dump;
  116. return vals;
  117. } // ----- end of method ASCIIParser::ReadReals -----
  118. //--------------------------------------------------------------------------------------
  119. // Class: ASCIIParser
  120. // Method: ReadInts
  121. //--------------------------------------------------------------------------------------
  122. std::vector<int> ASCIIParser::ReadInts ( const int& nr ) {
  123. std::string buf;
  124. char *dump = new char[BufferSize];
  125. std::vector<int> vals(0);
  126. while (input >> buf) {
  127. if (buf.substr(0, CommentString.size()) == CommentString) {
  128. input.getline(dump, BufferSize);
  129. } else {
  130. vals.push_back( atoi(buf.c_str() ));
  131. }
  132. if (static_cast<int>(vals.size()) == nr) {
  133. delete [] dump;
  134. return vals;
  135. }
  136. }
  137. delete [] dump;
  138. return vals;
  139. } // ----- end of method ASCIIParser::ReadInts -----
  140. //--------------------------------------------------------------------------------------
  141. // Class: ASCIIParser
  142. // Method: ReadStings
  143. //--------------------------------------------------------------------------------------
  144. std::vector<std::string> ASCIIParser::ReadStrings ( const int& nr ) {
  145. std::string buf;
  146. char *dump = new char[BufferSize];
  147. std::vector<std::string> vals(0);
  148. while (input >> buf) {
  149. if (buf.substr(0, CommentString.size()) == CommentString) {
  150. input.getline(dump, BufferSize);
  151. } else {
  152. vals.push_back( buf );
  153. }
  154. if (static_cast<int>(vals.size()) == nr) {
  155. delete [] dump;
  156. return vals;
  157. }
  158. }
  159. delete [] dump;
  160. return vals;
  161. } // ----- end of method ASCIIParser::ReadInts -----
  162. //--------------------------------------------------------------------------------------
  163. // Class: ASCIIParser
  164. // Method: SetCommentString
  165. //--------------------------------------------------------------------------------------
  166. void ASCIIParser::SetCommentString ( const std::string& key ) {
  167. CommentString = key;
  168. } // ----- end of method ASCIIParser::SetCommentString -----
  169. //--------------------------------------------------------------------------------------
  170. // Class: ASCIIParser
  171. // Method: SetBufferSize
  172. //--------------------------------------------------------------------------------------
  173. void ASCIIParser::SetBufferSize ( const int& size ) {
  174. BufferSize = size;
  175. } // ----- end of method ASCIIParser::SetCommentString -----
  176. //--------------------------------------------------------------------------------------
  177. // Class: ASCIIParser
  178. // Method: GetFileLocation
  179. //--------------------------------------------------------------------------------------
  180. int ASCIIParser::GetFileLocation ( ) {
  181. return input.tellg();
  182. } // ----- end of method ASCIIParser::GetFileLocation -----
  183. //--------------------------------------------------------------------------------------
  184. // Class: ASCIIParser
  185. // Method: JumpToLocation
  186. //--------------------------------------------------------------------------------------
  187. void ASCIIParser::JumpToLocation ( const int& loc ) {
  188. input.seekg( loc );
  189. return ;
  190. } // ----- end of method ASCIIParser::JumpToLocation -----
  191. } // ----- end of Lemma name -----
  192. /* vim: set tabstop=4 expandtab: */
  193. /* vim: set filetype=cpp syntax=cpp.doxygen: */