diff options
| author | Loic Guegan <manzerbredes@mailbox.org> | 2022-01-26 12:03:24 +0100 |
|---|---|---|
| committer | Loic Guegan <manzerbredes@mailbox.org> | 2022-01-26 12:03:24 +0100 |
| commit | bb914f047b0d96464f3e55234907df4e7c416e97 (patch) | |
| tree | 82963c3a4c9ea272163365ddf3144bd46fe78e82 /src/LargeFileStream.hpp | |
| parent | 8a770f9133957ad4356810efa4ede6e20dd01d26 (diff) | |
- Enable NAG parsing
- Add the LargeFileStream interface to handle large file
- Debug parsing
Diffstat (limited to 'src/LargeFileStream.hpp')
| -rw-r--r-- | src/LargeFileStream.hpp | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/LargeFileStream.hpp b/src/LargeFileStream.hpp new file mode 100644 index 0000000..6ac87e8 --- /dev/null +++ b/src/LargeFileStream.hpp @@ -0,0 +1,52 @@ +#define BUFFER_SIZE (1024 * 1024 / 2) + +#include <fstream> +#include <iostream> +#include <string> + +namespace pgnp { +using namespace std; + +class LargeFileStream { + /// @brief File to load + ifstream file; + /// @brief In memory buffer + char buffer[BUFFER_SIZE]; + /// @brief Number of chuck read minus 1 + long chuck_count; + /// @brief Number of byte read during the last file access + long last_read_size; + /// @brief Keep track of the file offset (to prevent backward read) + long last_loc; + /// @brief Use a string as file content + std::string content; + /// @brief Use to shortcut some methods + bool use_string; + /// @brief End Of File ? + bool eof; + + /// @brief Load the next chuck of data from disk to memory + void ReadNextChunk(); + +public: + LargeFileStream(); + void FromFile(std::string filepath); + /// @brief Emulate file access with a string + void FromString(std::string content); + /// @brief Allow array like access to the file + char operator[](long loc); + /// @brief Check if we reach the EOF + bool IsEOF(long loc); + + // Various Exceptions + struct BackwardRead : public std::exception { + const char *what() const throw() { + return "LargeFileStream cannot read backward"; + } + }; + struct ReadToFar : public std::exception { + const char *what() const throw() { return "You reach the end of the file"; } + }; +}; + +} // namespace pgnp
\ No newline at end of file |
