Main Lemma Repository
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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