| 1 | // Aseprite Document IO Library |
| 2 | // Copyright (c) 2017-2018 David Capello |
| 3 | // |
| 4 | // This file is released under the terms of the MIT license. |
| 5 | // Read LICENSE.txt for more information. |
| 6 | |
| 7 | #ifndef DIO_FILE_INTERFACE_H_INCLUDED |
| 8 | #define DIO_FILE_INTERFACE_H_INCLUDED |
| 9 | #pragma once |
| 10 | |
| 11 | #include <cstddef> |
| 12 | #include <cstdint> |
| 13 | #include <cstdio> |
| 14 | |
| 15 | namespace dio { |
| 16 | |
| 17 | class FileInterface { |
| 18 | public: |
| 19 | virtual ~FileInterface() { } |
| 20 | |
| 21 | // Returns true if we can read/write bytes from/into the file |
| 22 | virtual bool ok() const = 0; |
| 23 | |
| 24 | // Current position in the file |
| 25 | virtual size_t tell() = 0; |
| 26 | |
| 27 | // Jump to the given position in the file |
| 28 | virtual void seek(size_t absPos) = 0; |
| 29 | |
| 30 | // Returns the next byte in the file or 0 if ok() = false |
| 31 | virtual uint8_t read8() = 0; |
| 32 | virtual size_t readBytes(uint8_t* buf, size_t n) = 0; |
| 33 | |
| 34 | // Writes one byte in the file (or do nothing if ok() = false) |
| 35 | virtual void write8(uint8_t value) = 0; |
| 36 | |
| 37 | }; |
| 38 | |
| 39 | class StdioFileInterface : public FileInterface { |
| 40 | public: |
| 41 | StdioFileInterface(FILE* file); |
| 42 | bool ok() const override; |
| 43 | size_t tell() override; |
| 44 | void seek(size_t absPos) override; |
| 45 | uint8_t read8() override; |
| 46 | size_t readBytes(uint8_t* buf, size_t n) override; |
| 47 | void write8(uint8_t value) override; |
| 48 | private: |
| 49 | FILE* m_file; |
| 50 | bool m_ok; |
| 51 | }; |
| 52 | |
| 53 | } // namespace dio |
| 54 | |
| 55 | #endif |
| 56 | |