|
| 1 | +#ifndef YSTDLIB_IO_INTERFACE_READERINTERFACE_HPP |
| 2 | +#define YSTDLIB_IO_INTERFACE_READERINTERFACE_HPP |
| 3 | + |
| 4 | +#include <cstddef> |
| 5 | +#include <string> |
| 6 | + |
| 7 | +#include "ErrorCode.hpp" |
| 8 | + |
| 9 | +namespace ystdlib::io_interface { |
| 10 | +class ReaderInterface { |
| 11 | +public: |
| 12 | + // Types |
| 13 | + class OperationFailed : public std::exception { |
| 14 | + public: |
| 15 | + OperationFailed(ErrorCode error_code) {} |
| 16 | + }; |
| 17 | + |
| 18 | + // Destructor |
| 19 | + virtual ~ReaderInterface() = default; |
| 20 | + |
| 21 | + // Methods |
| 22 | + virtual ErrorCode try_read(char* buf, size_t num_bytes_to_read, size_t& num_bytes_read) = 0; |
| 23 | + virtual ErrorCode try_seek_from_begin(size_t pos) = 0; |
| 24 | + virtual ErrorCode try_get_pos(size_t& pos) = 0; |
| 25 | + |
| 26 | + /** |
| 27 | + * Tries to read up to the next delimiter and stores it in the given string. |
| 28 | + * NOTE: Implementations should override this if they can achieve better performance. |
| 29 | + * @param delim The delimiter to stop at |
| 30 | + * @param keep_delimiter Whether to include the delimiter in the output string or not |
| 31 | + * @param append Whether to append to the given string or replace its contents |
| 32 | + * @param str The string read |
| 33 | + * @return ErrorCode_Success on success |
| 34 | + * @return Same as ReaderInterface::try_read otherwise |
| 35 | + */ |
| 36 | + virtual ErrorCode |
| 37 | + try_read_to_delimiter(char delim, bool keep_delimiter, bool append, std::string& str); |
| 38 | + |
| 39 | + /** |
| 40 | + * Reads up to a given number of bytes |
| 41 | + * @param buf |
| 42 | + * @param num_bytes_to_read The number of bytes to try and read |
| 43 | + * @param num_bytes_read The actual number of bytes read |
| 44 | + * @return false on EOF |
| 45 | + * @return true otherwise |
| 46 | + */ |
| 47 | + bool read(char* buf, size_t num_bytes_to_read, size_t& num_bytes_read); |
| 48 | + |
| 49 | + /** |
| 50 | + * Reads up to the next delimiter and stores it in the given string |
| 51 | + * @param delim The delimiter to stop at |
| 52 | + * @param keep_delimiter Whether to include the delimiter in the output string or not |
| 53 | + * @param append Whether to append to the given string or replace its contents |
| 54 | + * @param str The string read |
| 55 | + * @return false on EOF |
| 56 | + * @return true on success |
| 57 | + */ |
| 58 | + bool read_to_delimiter(char delim, bool keep_delimiter, bool append, std::string& str); |
| 59 | + |
| 60 | + /** |
| 61 | + * Tries to read a number of bytes |
| 62 | + * @param buf |
| 63 | + * @param num_bytes Number of bytes to read |
| 64 | + * @return Same as the underlying medium's try_read method |
| 65 | + * @return ErrorCode_Truncated if 0 < # bytes read < num_bytes |
| 66 | + */ |
| 67 | + ErrorCode try_read_exact_length(char* buf, size_t num_bytes); |
| 68 | + /** |
| 69 | + * Reads a number of bytes |
| 70 | + * @param buf |
| 71 | + * @param num_bytes Number of bytes to read |
| 72 | + * @param eof_possible If EOF should be possible (without reading any bytes) |
| 73 | + * @return false if EOF is possible and EOF was hit |
| 74 | + * @return true on success |
| 75 | + */ |
| 76 | + bool read_exact_length(char* buf, size_t num_bytes, bool eof_possible); |
| 77 | + |
| 78 | + /** |
| 79 | + * Tries to read a numeric value from a file |
| 80 | + * @param value The read value |
| 81 | + * @return Same as FileReader::try_read_exact_length's return values |
| 82 | + */ |
| 83 | + template <typename ValueType> |
| 84 | + ErrorCode try_read_numeric_value(ValueType& value); |
| 85 | + /** |
| 86 | + * Reads a numeric value |
| 87 | + * @param value The read value |
| 88 | + * @param eof_possible If EOF should be possible (without reading any bytes) |
| 89 | + * @return false if EOF is possible and EOF was hit |
| 90 | + * @return true on success |
| 91 | + */ |
| 92 | + template <typename ValueType> |
| 93 | + bool read_numeric_value(ValueType& value, bool eof_possible); |
| 94 | + |
| 95 | + /** |
| 96 | + * Tries to read a string |
| 97 | + * @param str_length |
| 98 | + * @param str The string read |
| 99 | + * @return Same as ReaderInterface::try_read_exact_length |
| 100 | + */ |
| 101 | + ErrorCode try_read_string(size_t str_length, std::string& str); |
| 102 | + /** |
| 103 | + * Reads a string |
| 104 | + * @param str_length |
| 105 | + * @param str The string read |
| 106 | + * @param eof_possible If EOF should be possible (without reading any bytes) |
| 107 | + * @return false if EOF is possible and EOF was hit |
| 108 | + * @return true on success |
| 109 | + */ |
| 110 | + bool read_string(size_t str_length, std::string& str, bool eof_possible); |
| 111 | + |
| 112 | + /** |
| 113 | + * Seeks from the beginning to the given position |
| 114 | + * @param pos |
| 115 | + */ |
| 116 | + void seek_from_begin(size_t pos); |
| 117 | + |
| 118 | + /** |
| 119 | + * Gets the current position of the read head |
| 120 | + * @return Position of the read head |
| 121 | + */ |
| 122 | + size_t get_pos(); |
| 123 | +}; |
| 124 | + |
| 125 | +template <typename ValueType> |
| 126 | +ErrorCode ReaderInterface::try_read_numeric_value(ValueType& value) { |
| 127 | + ErrorCode error_code = try_read_exact_length(reinterpret_cast<char*>(&value), sizeof(value)); |
| 128 | + if (ErrorCode_Success != error_code) { |
| 129 | + return error_code; |
| 130 | + } |
| 131 | + return ErrorCode_Success; |
| 132 | +} |
| 133 | + |
| 134 | +template <typename ValueType> |
| 135 | +bool ReaderInterface::read_numeric_value(ValueType& value, bool eof_possible) { |
| 136 | + ErrorCode error_code = try_read_numeric_value(value); |
| 137 | + if (ErrorCode_EndOfFile == error_code && eof_possible) { |
| 138 | + return false; |
| 139 | + } |
| 140 | + if (ErrorCode_Success != error_code) { |
| 141 | + throw OperationFailed(error_code); |
| 142 | + } |
| 143 | + return true; |
| 144 | +} |
| 145 | +} // namespace ystdlib::io_interface |
| 146 | + |
| 147 | +#endif // YSTDLIB_IO_INTERFACE_READERINTERFACE_HPP |
0 commit comments