bin_file.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Not buffered low-level binary file
  2. //
  3. // Platform: ISO C++ 98/11 - POSIX
  4. // $Id$
  5. //
  6. // (c) __vic 2006
  7. #ifndef __VIC_BIN_FILE_H
  8. #define __VIC_BIN_FILE_H
  9. #include<__vic/defs.h>
  10. #include<__vic/posix/file.h>
  11. #include<__vic/throw_errno.h>
  12. namespace __vic {
  13. //////////////////////////////////////////////////////////////////////////////
  14. class bin_file
  15. {
  16. posix::file f;
  17. public:
  18. // Constructor tags
  19. enum in_t { in = O_RDONLY };
  20. enum out_t { out = O_WRONLY | O_CREAT | O_TRUNC };
  21. enum append_t { append = O_WRONLY | O_CREAT | O_APPEND };
  22. bin_file() __VIC_DEFAULT_CTR
  23. bin_file(const char *fname, in_t) : f(fname, in) {}
  24. bin_file(const char *fname, out_t) : f(fname, out) {}
  25. bin_file(const char *fname, append_t) : f(fname, append) {}
  26. bool open_in(const char *fname) { return f.open(fname, in); }
  27. bool open_out(const char *fname) { return f.open(fname, out); }
  28. bool open_append(const char *fname) { return f.open(fname, append); }
  29. size_t read_max(void *buf, size_t n) { return f.read_max(buf, n); }
  30. size_t read_some(void *buf, size_t n) { return f.read_some(buf, n); }
  31. void write_all(const void *buf, size_t n) { f.write_all(buf, n); }
  32. bool is_open() const { return f.is_open(); }
  33. void close() { f.close(); }
  34. bool close_nt() noexcept { return f.close_nt(); }
  35. void swap(bin_file &o) noexcept { f.swap(o.f); }
  36. __VIC_NORETURN void throw_last_error(const char *msg) { throw_errno(msg); }
  37. void throw_if_closed(const char *msg)
  38. { if(!is_open()) throw_last_error(msg); }
  39. };
  40. //////////////////////////////////////////////////////////////////////////////
  41. } // namespace
  42. #endif // header guard