Main Lemma Repository
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ASCIIParser.h 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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:31:24 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. #ifndef ASCIIPARSER_INC
  17. #define ASCIIPARSER_INC
  18. #include "LemmaObject.h"
  19. #include <fstream>
  20. namespace Lemma {
  21. /**
  22. \brief Provides ASCII input file parsing
  23. \details Able to accomodate various inputs and comment styles
  24. */
  25. class ASCIIParser : public LemmaObject {
  26. friend std::ostream &operator<<(std::ostream &stream,
  27. const ASCIIParser &ob);
  28. public:
  29. // ==================== LIFECYCLE =======================
  30. /**
  31. * Factory method for generating concrete class.
  32. * @return a std::shared_ptr of type ASCIIParser
  33. */
  34. static std::shared_ptr< ASCIIParser > NewSP();
  35. /**
  36. * Constructs an object from a YAML serialization
  37. * @return a std::shared_ptr of type ASCIIParser
  38. */
  39. static std::shared_ptr< ASCIIParser > DeSerialize( const YAML::Node& node );
  40. /**
  41. * Uses YAML to serialize this object.
  42. * @return a YAML::Node
  43. */
  44. YAML::Node Serialize() const;
  45. // ==================== OPERATORS =======================
  46. // ==================== OPERATIONS =======================
  47. /** Opens the file up for reading. Throws an exception if something bad happens.
  48. @param[in] fname is the filename to be parsed.
  49. */
  50. void Open (const std::string& fname);
  51. /** Closes the file. Throws an exception if something bad happens.
  52. */
  53. void Close ( );
  54. /** Reads a series of Reals.
  55. * @param[in] nr is the number of reals to read. Enter -1 for the entire file
  56. * @todo throw exception if no file is open
  57. */
  58. std::vector<Real> ReadReals( const int& nr);
  59. /** Reads a series of Reals.
  60. * @param[in] nr is the number of ints to read. Enter -1 for the entire file
  61. * @todo throw exception if no file is open
  62. */
  63. std::vector<int> ReadInts( const int& nr);
  64. /** Reads a series of space delimited strings
  65. * @param[in] nr is the number of strings to read. Enter -1 for the entire file
  66. * @todo throw exception if no file is open
  67. */
  68. std::vector< std::string > ReadStrings( const int& nr);
  69. /**
  70. * @param[in] loc is the point in the file to jump to. Uses seekg
  71. */
  72. void JumpToLocation(const int& loc);
  73. // ==================== ACCESS =======================
  74. /** Sets the comment identifier key.
  75. * @param[in] key is a string identifying comments. All text after the key will be
  76. * ignored by the parser. Default is //
  77. */
  78. void SetCommentString( const std::string& key );
  79. /** Sets the buffer size. This affects the maximum number of column in a line. Defaults
  80. * is 255.
  81. * @param[in] BufferSize is the size of the buffer to use
  82. */
  83. void SetBufferSize( const int& BufferSize);
  84. /**
  85. * @return the current position in the file, as reported by istream::tellg
  86. */
  87. int GetFileLocation();
  88. /** Returns the name of the underlying class, similiar to Python's type */
  89. virtual inline std::string GetName() const {
  90. return CName;
  91. }
  92. // ==================== INQUIRY =======================
  93. protected:
  94. // ==================== LIFECYCLE =======================
  95. /** Default protected constructor, use New */
  96. ASCIIParser ( );
  97. /** Constructor using YAML::Node */
  98. ASCIIParser ( const YAML::Node& node );
  99. /** Default protected destructor, use Delete */
  100. ~ASCIIParser ();
  101. /**
  102. * @copybrief LemmaObject::Release()
  103. * @copydetails LemmaObject::Release()
  104. */
  105. void Release();
  106. private:
  107. // ==================== DATA MEMBERS =========================
  108. /** ASCII string representation of the class name */
  109. static constexpr auto CName = "ASCIIParser";
  110. /** c++ style file IO */
  111. std::fstream input;
  112. /** comment string, defaults to c++ style // */
  113. std::string CommentString;
  114. /** Buffer size, max line width supported, defaults to 255 */
  115. int BufferSize;
  116. }; // ----- end of class ASCIIParser -----
  117. } // ----- end of Lemma name -----
  118. #endif // ----- #ifndef ASCIIPARSER_INC -----