Lemma is an Electromagnetics API
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ASCIIParser.h 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. * Constructs an object from a string representation of a YAML::Node. This is primarily
  64. * used in Python wrapping
  65. */
  66. static std::shared_ptr<ASCIIParser> DeSerialize( const std::string& node ) {
  67. return ASCIIParser::DeSerialize(YAML::Load(node));
  68. }
  69. /**
  70. * Uses YAML to serialize this object.
  71. * @return a YAML::Node
  72. */
  73. virtual YAML::Node Serialize() const;
  74. // ==================== OPERATORS =======================
  75. // ==================== OPERATIONS =======================
  76. /** Opens the file up for reading. Throws an exception if something bad happens.
  77. @param[in] fname is the filename to be parsed.
  78. */
  79. void Open (const std::string& fname);
  80. /** Closes the file. Throws an exception if something bad happens.
  81. */
  82. void Close ( );
  83. /** Reads a series of Reals.
  84. * @param[in] nr is the number of reals to read. Enter -1 for the entire file
  85. * @todo throw exception if no file is open
  86. */
  87. std::vector<Real> ReadReals( const int& nr);
  88. /** Reads a series of Reals.
  89. * @param[in] nr is the number of ints to read. Enter -1 for the entire file
  90. * @todo throw exception if no file is open
  91. */
  92. std::vector<int> ReadInts( const int& nr);
  93. /** Reads a series of space delimited strings
  94. * @param[in] nr is the number of strings to read. Enter -1 for the entire file
  95. * @todo throw exception if no file is open
  96. */
  97. std::vector< std::string > ReadStrings( const int& nr);
  98. /**
  99. * @param[in] loc is the point in the file to jump to. Uses seekg
  100. */
  101. void JumpToLocation(const std::streamoff& loc);
  102. // ==================== ACCESS =======================
  103. /** Sets the comment identifier key.
  104. * @param[in] key is a string identifying comments. All text after the key will be
  105. * ignored by the parser. Default is //
  106. */
  107. void SetCommentString( const std::string& key );
  108. /** Sets the buffer size. This affects the maximum number of columns in a line. Defaults
  109. * is 255.
  110. * @param[in] BufferSize is the size of the buffer to use
  111. */
  112. void SetBufferSize( const int& BufferSize);
  113. /**
  114. * @return the current position in the file, as reported by istream::tellg
  115. */
  116. std::streamoff GetFileLocation();
  117. /** Returns the name of the underlying class, similiar to Python's type */
  118. virtual std::string GetName() const;
  119. // ==================== INQUIRY =======================
  120. protected:
  121. private:
  122. /** Copy constructor */
  123. ASCIIParser( const ASCIIParser& ) = delete;
  124. // ==================== DATA MEMBERS =========================
  125. /** ASCII string representation of the class name */
  126. static constexpr auto CName = "ASCIIParser";
  127. /** c++ style file IO */
  128. std::fstream input;
  129. /** comment string, defaults to c++ style // */
  130. std::string CommentString;
  131. /** Buffer size, max line width supported, defaults to 255 */
  132. int BufferSize;
  133. }; // ----- end of class ASCIIParser -----
  134. } // ----- end of Lemma name -----
  135. #endif // ----- #ifndef ASCIIPARSER_INC -----
  136. /* vim: set tabstop=4 expandtab: */
  137. /* vim: set filetype=cpp syntax=cpp.doxygen */