bin_file.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Not buffered low-level binary file
  2. //
  3. // Platform: ISO C++ 98/11
  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/stdio_file.h>
  11. namespace __vic {
  12. //////////////////////////////////////////////////////////////////////////////
  13. class bin_file
  14. {
  15. stdio_file f;
  16. bool open_(const char * , char );
  17. public:
  18. // Constructor tags
  19. enum in_t { in = 'r' };
  20. enum out_t { out = 'w' };
  21. enum append_t { append = 'a' };
  22. bin_file() __VIC_DEFAULT_CTR
  23. bin_file(const char *fname, in_t) { open_in(fname); }
  24. bin_file(const char *fname, out_t) { open_out(fname); }
  25. bin_file(const char *fname, append_t) { open_append(fname); }
  26. bool open_in(const char *fname) { return open_(fname, in); }
  27. bool open_out(const char *fname) { return open_(fname, out); }
  28. bool open_append(const char *fname) { return open_(fname, append); }
  29. size_t read_max(void * , size_t );
  30. size_t read_some(void *buf, size_t n) { return read_max(buf, n); }
  31. void write_all(const void * , size_t );
  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 * );
  37. void throw_if_closed(const char *msg)
  38. { if(!is_open()) throw_last_error(msg); }
  39. };
  40. //////////////////////////////////////////////////////////////////////////////
  41. } // namespace
  42. #endif // header guard