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 9.6KB

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