Lemma is an Electromagnetics API
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ASCIIParser.cpp 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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, XRI Geophysics, LLC
  15. * @copyright Copyright (c) 2013, Trevor Irons
  16. */
  17. #include "ASCIIParser.h"
  18. namespace Lemma {
  19. // ==================== FRIEND METHODS =====================
  20. std::ostream &operator<<(std::ostream &stream, const ASCIIParser &ob) {
  21. stream << *(LemmaObject*)(&ob);
  22. return stream;
  23. }
  24. // ==================== LIFECYCLE =======================
  25. //--------------------------------------------------------------------------------------
  26. // Class: ASCIIParser
  27. // Method: ASCIIParser
  28. // Description: constructor (protected)
  29. //--------------------------------------------------------------------------------------
  30. ASCIIParser::ASCIIParser (const std::string& name) : LemmaObject(name),
  31. CommentString("//"), BufferSize(255) {
  32. } // ----- end of method ASCIIParser::ASCIIParser (constructor) -----
  33. //--------------------------------------------------------------------------------------
  34. // Class: ASCIIParser
  35. // Method: New()
  36. // Description: public constructor
  37. //--------------------------------------------------------------------------------------
  38. ASCIIParser* ASCIIParser::New() {
  39. ASCIIParser* Obj = new ASCIIParser("ASCIIParser");
  40. Obj->AttachTo(Obj);
  41. return Obj;
  42. }
  43. //--------------------------------------------------------------------------------------
  44. // Class: ASCIIParser
  45. // Method: ~ASCIIParser
  46. // Description: destructor (protected)
  47. //--------------------------------------------------------------------------------------
  48. ASCIIParser::~ASCIIParser () {
  49. } // ----- end of method ASCIIParser::~ASCIIParser (destructor) -----
  50. //--------------------------------------------------------------------------------------
  51. // Class: ASCIIParser
  52. // Method: Delete
  53. // Description: public destructor
  54. //--------------------------------------------------------------------------------------
  55. void ASCIIParser::Delete() {
  56. this->DetachFrom(this);
  57. }
  58. //--------------------------------------------------------------------------------------
  59. // Class: ASCIIParser
  60. // Method: Release
  61. // Description: destructor (protected)
  62. //--------------------------------------------------------------------------------------
  63. void ASCIIParser::Release() {
  64. delete this;
  65. }
  66. //--------------------------------------------------------------------------------------
  67. // Class: ASCIIParser
  68. // Method: Open
  69. //--------------------------------------------------------------------------------------
  70. void ASCIIParser::Open ( const std::string& fname ) {
  71. input.open(fname.c_str(), std::ios::in);
  72. if (input.fail()) {
  73. throw GenericFileIOError(this, fname);
  74. }
  75. return ;
  76. } // ----- end of method ASCIIParser::Open -----
  77. //--------------------------------------------------------------------------------------
  78. // Class: ASCIIParser
  79. // Method: Close
  80. //--------------------------------------------------------------------------------------
  81. void ASCIIParser::Close ( ) {
  82. input.close();
  83. return ;
  84. } // ----- end of method ASCIIParser::Close -----
  85. //--------------------------------------------------------------------------------------
  86. // Class: ASCIIParser
  87. // Method: ReadReals
  88. //--------------------------------------------------------------------------------------
  89. std::vector<Real> ASCIIParser::ReadReals ( const int& nr ) {
  90. std::string buf;
  91. char *dump = new char[BufferSize];
  92. std::vector<Real> vals(0);
  93. while (input >> buf) {
  94. if (buf.substr(0, CommentString.size()) == CommentString) {
  95. input.getline(dump, BufferSize);
  96. } else {
  97. vals.push_back( atof(buf.c_str() ));
  98. }
  99. if (static_cast<int>(vals.size()) == nr) {
  100. delete [] dump;
  101. return vals;
  102. }
  103. }
  104. delete [] dump;
  105. return vals;
  106. } // ----- end of method ASCIIParser::ReadReals -----
  107. //--------------------------------------------------------------------------------------
  108. // Class: ASCIIParser
  109. // Method: ReadInts
  110. //--------------------------------------------------------------------------------------
  111. std::vector<int> ASCIIParser::ReadInts ( const int& nr ) {
  112. std::string buf;
  113. char *dump = new char[BufferSize];
  114. std::vector<int> 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( atoi(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::ReadInts -----
  129. //--------------------------------------------------------------------------------------
  130. // Class: ASCIIParser
  131. // Method: ReadStings
  132. //--------------------------------------------------------------------------------------
  133. std::vector<std::string> ASCIIParser::ReadStrings ( const int& nr ) {
  134. std::string buf;
  135. char *dump = new char[BufferSize];
  136. std::vector<std::string> 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( buf );
  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: SetCommentString
  154. //--------------------------------------------------------------------------------------
  155. void ASCIIParser::SetCommentString ( const std::string& key ) {
  156. CommentString = key;
  157. } // ----- end of method ASCIIParser::SetCommentString -----
  158. //--------------------------------------------------------------------------------------
  159. // Class: ASCIIParser
  160. // Method: SetBufferSize
  161. //--------------------------------------------------------------------------------------
  162. void ASCIIParser::SetBufferSize ( const int& size ) {
  163. BufferSize = size;
  164. } // ----- end of method ASCIIParser::SetCommentString -----
  165. //--------------------------------------------------------------------------------------
  166. // Class: ASCIIParser
  167. // Method: GetFileLocation
  168. //--------------------------------------------------------------------------------------
  169. int ASCIIParser::GetFileLocation ( ) {
  170. return input.tellg();
  171. } // ----- end of method ASCIIParser::GetFileLocation -----
  172. //--------------------------------------------------------------------------------------
  173. // Class: ASCIIParser
  174. // Method: JumpToLocation
  175. //--------------------------------------------------------------------------------------
  176. void ASCIIParser::JumpToLocation ( const int& loc ) {
  177. input.seekg( loc );
  178. return ;
  179. } // ----- end of method ASCIIParser::JumpToLocation -----
  180. } // ----- end of Lemma name -----