FileStream.h 954 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #pragma once
  2. #include <ByteStream.h> // for ByteStream
  3. #include <stdint.h> // for uint8_t
  4. #include <stdio.h> // for size_t, FILE
  5. #include <string> // for string
  6. /*
  7. * FileStream
  8. *
  9. * A class for reading and writing to files implementing the ByteStream interface.
  10. *
  11. */
  12. namespace bell {
  13. class FileStream : public ByteStream {
  14. public:
  15. FileStream(const std::string& path, std::string mode);
  16. ~FileStream();
  17. FILE* file;
  18. /*
  19. * Reads data from the stream.
  20. *
  21. * @param buf The buffer to read data into.
  22. * @param nbytes The size of the buffer.
  23. * @return The number of bytes read.
  24. * @throws std::runtime_error if the stream is closed.
  25. */
  26. size_t read(uint8_t* buf, size_t nbytes);
  27. /*
  28. * Skips nbytes bytes in the stream.
  29. */
  30. size_t skip(size_t nbytes);
  31. size_t position();
  32. size_t size();
  33. // Closes the connection
  34. void close();
  35. };
  36. } // namespace bell