Main Lemma Repository
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ASCIIParser.h 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. /*
  30. * This key is used to lock the constructors
  31. */
  32. //struct ctor_key {};
  33. public:
  34. // ==================== LIFECYCLE =======================
  35. /** Default constructor
  36. * @note This method is locked
  37. * @see ASCIIParser::NewSP
  38. */
  39. explicit ASCIIParser ( const ctor_key& );
  40. /**
  41. * DeSerializing constructor.
  42. * @note This method is locked, and cannot be called directly.
  43. * The reason that the method is public is to enable the use
  44. * of make_shared whilst enforcing the use of shared_ptr,
  45. * in c++-17, this curiosity may be resolved.
  46. * @see ASCIIParser::DeSerialize
  47. */
  48. explicit ASCIIParser ( const YAML::Node& node, const ctor_key& );
  49. /** Default destructor
  50. * @note This should never be called explicitly, use NewSP
  51. */
  52. virtual ~ASCIIParser ();
  53. /**
  54. * Factory method for generating concrete class.
  55. * @return a std::shared_ptr of type ASCIIParser
  56. */
  57. static std::shared_ptr< ASCIIParser > NewSP();
  58. /**
  59. * Constructs an object from a YAML serialization
  60. * @return a std::shared_ptr of type ASCIIParser
  61. */
  62. static std::shared_ptr< ASCIIParser > DeSerialize( const YAML::Node& node );
  63. /**
  64. * Uses YAML to serialize this object.
  65. * @return a YAML::Node
  66. */
  67. virtual YAML::Node Serialize() const;
  68. // ==================== OPERATORS =======================
  69. // ==================== OPERATIONS =======================
  70. /** Opens the file up for reading. Throws an exception if something bad happens.
  71. @param[in] fname is the filename to be parsed.
  72. */
  73. void Open (const std::string& fname);
  74. /** Closes the file. Throws an exception if something bad happens.
  75. */
  76. void Close ( );
  77. /** Reads a series of Reals.
  78. * @param[in] nr is the number of reals to read. Enter -1 for the entire file
  79. * @todo throw exception if no file is open
  80. */
  81. std::vector<Real> ReadReals( const int& nr);
  82. /** Reads a series of Reals.
  83. * @param[in] nr is the number of ints to read. Enter -1 for the entire file
  84. * @todo throw exception if no file is open
  85. */
  86. std::vector<int> ReadInts( const int& nr);
  87. /** Reads a series of space delimited strings
  88. * @param[in] nr is the number of strings to read. Enter -1 for the entire file
  89. * @todo throw exception if no file is open
  90. */
  91. std::vector< std::string > ReadStrings( const int& nr);
  92. /**
  93. * @param[in] loc is the point in the file to jump to. Uses seekg
  94. */
  95. void JumpToLocation(const int& loc);
  96. // ==================== ACCESS =======================
  97. /** Sets the comment identifier key.
  98. * @param[in] key is a string identifying comments. All text after the key will be
  99. * ignored by the parser. Default is //
  100. */
  101. void SetCommentString( const std::string& key );
  102. /** Sets the buffer size. This affects the maximum number of columns in a line. Defaults
  103. * is 255.
  104. * @param[in] BufferSize is the size of the buffer to use
  105. */
  106. void SetBufferSize( const int& BufferSize);
  107. /**
  108. * @return the current position in the file, as reported by istream::tellg
  109. */
  110. int GetFileLocation();
  111. /** Returns the name of the underlying class, similiar to Python's type */
  112. virtual std::string GetName() const;
  113. // ==================== INQUIRY =======================
  114. protected:
  115. private:
  116. /** Copy constructor */
  117. ASCIIParser( const ASCIIParser& ) = delete;
  118. // ==================== DATA MEMBERS =========================
  119. /** ASCII string representation of the class name */
  120. static constexpr auto CName = "ASCIIParser";
  121. /** c++ style file IO */
  122. std::fstream input;
  123. /** comment string, defaults to c++ style // */
  124. std::string CommentString;
  125. /** Buffer size, max line width supported, defaults to 255 */
  126. int BufferSize;
  127. }; // ----- end of class ASCIIParser -----
  128. } // ----- end of Lemma name -----
  129. #endif // ----- #ifndef ASCIIPARSER_INC -----
  130. /* vim: set tabstop=4 expandtab: */
  131. /* vim: set filetype=cpp syntax=cpp.doxygen: */