Lemma is an Electromagnetics API
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 5.0KB

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