Main Lemma Repository
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.h 4.5KB

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