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

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