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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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: GetName
  71. // Description: Class identifier
  72. //--------------------------------------------------------------------------------------
  73. inline std::string ASCIIParser::GetName ( ) const {
  74. return CName;
  75. } // ----- end of method ASCIIParser::GetName -----
  76. //--------------------------------------------------------------------------------------
  77. // Class: ASCIIParser
  78. // Method: ASCIIParser
  79. // Description: DeSerializing constructor (protected)
  80. //--------------------------------------------------------------------------------------
  81. ASCIIParser::ASCIIParser (const YAML::Node& node, const ctor_key& key) : LemmaObject(node) {
  82. this->CommentString = node["CommentString"].as<std::string>();
  83. this->BufferSize = node["BufferSize"].as<int>();
  84. } // ----- end of method ASCIIParser::ASCIIParser (constructor) -----
  85. //--------------------------------------------------------------------------------------
  86. // Class: ASCIIParser
  87. // Method: Open
  88. //--------------------------------------------------------------------------------------
  89. void ASCIIParser::Open ( const std::string& fname ) {
  90. input.open(fname.c_str(), std::ios::in);
  91. if (input.fail()) {
  92. throw GenericFileIOError(this, fname);
  93. }
  94. return ;
  95. } // ----- end of method ASCIIParser::Open -----
  96. //--------------------------------------------------------------------------------------
  97. // Class: ASCIIParser
  98. // Method: Close
  99. //--------------------------------------------------------------------------------------
  100. void ASCIIParser::Close ( ) {
  101. input.close();
  102. return ;
  103. } // ----- end of method ASCIIParser::Close -----
  104. //--------------------------------------------------------------------------------------
  105. // Class: ASCIIParser
  106. // Method: ReadReals
  107. //--------------------------------------------------------------------------------------
  108. std::vector<Real> ASCIIParser::ReadReals ( const int& nr ) {
  109. std::string buf;
  110. char *dump = new char[BufferSize];
  111. std::vector<Real> vals(0);
  112. while (input >> buf) {
  113. if (buf.substr(0, CommentString.size()) == CommentString) {
  114. input.getline(dump, BufferSize);
  115. } else {
  116. vals.push_back( atof(buf.c_str() ));
  117. }
  118. if (static_cast<int>(vals.size()) == nr) {
  119. delete [] dump;
  120. return vals;
  121. }
  122. }
  123. delete [] dump;
  124. return vals;
  125. } // ----- end of method ASCIIParser::ReadReals -----
  126. //--------------------------------------------------------------------------------------
  127. // Class: ASCIIParser
  128. // Method: ReadInts
  129. //--------------------------------------------------------------------------------------
  130. std::vector<int> ASCIIParser::ReadInts ( const int& nr ) {
  131. std::string buf;
  132. char *dump = new char[BufferSize];
  133. std::vector<int> vals(0);
  134. while (input >> buf) {
  135. if (buf.substr(0, CommentString.size()) == CommentString) {
  136. input.getline(dump, BufferSize);
  137. } else {
  138. vals.push_back( atoi(buf.c_str() ));
  139. }
  140. if (static_cast<int>(vals.size()) == nr) {
  141. delete [] dump;
  142. return vals;
  143. }
  144. }
  145. delete [] dump;
  146. return vals;
  147. } // ----- end of method ASCIIParser::ReadInts -----
  148. //--------------------------------------------------------------------------------------
  149. // Class: ASCIIParser
  150. // Method: ReadStings
  151. //--------------------------------------------------------------------------------------
  152. std::vector<std::string> ASCIIParser::ReadStrings ( const int& nr ) {
  153. std::string buf;
  154. char *dump = new char[BufferSize];
  155. std::vector<std::string> vals(0);
  156. while (input >> buf) {
  157. if (buf.substr(0, CommentString.size()) == CommentString) {
  158. input.getline(dump, BufferSize);
  159. } else {
  160. vals.push_back( buf );
  161. }
  162. if (static_cast<int>(vals.size()) == nr) {
  163. delete [] dump;
  164. return vals;
  165. }
  166. }
  167. delete [] dump;
  168. return vals;
  169. } // ----- end of method ASCIIParser::ReadInts -----
  170. //--------------------------------------------------------------------------------------
  171. // Class: ASCIIParser
  172. // Method: SetCommentString
  173. //--------------------------------------------------------------------------------------
  174. void ASCIIParser::SetCommentString ( const std::string& key ) {
  175. CommentString = key;
  176. } // ----- end of method ASCIIParser::SetCommentString -----
  177. //--------------------------------------------------------------------------------------
  178. // Class: ASCIIParser
  179. // Method: SetBufferSize
  180. //--------------------------------------------------------------------------------------
  181. void ASCIIParser::SetBufferSize ( const int& size ) {
  182. BufferSize = size;
  183. } // ----- end of method ASCIIParser::SetCommentString -----
  184. //--------------------------------------------------------------------------------------
  185. // Class: ASCIIParser
  186. // Method: GetFileLocation
  187. //--------------------------------------------------------------------------------------
  188. int ASCIIParser::GetFileLocation ( ) {
  189. return input.tellg();
  190. } // ----- end of method ASCIIParser::GetFileLocation -----
  191. //--------------------------------------------------------------------------------------
  192. // Class: ASCIIParser
  193. // Method: JumpToLocation
  194. //--------------------------------------------------------------------------------------
  195. void ASCIIParser::JumpToLocation ( const int& loc ) {
  196. input.seekg( loc );
  197. return ;
  198. } // ----- end of method ASCIIParser::JumpToLocation -----
  199. } // ----- end of Lemma name -----
  200. /* vim: set tabstop=4 expandtab: */
  201. /* vim: set filetype=cpp syntax=cpp.doxygen: */