diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/file.cc | 29 | ||||
| -rw-r--r-- | src/file.h | 4 |
2 files changed, 33 insertions, 0 deletions
diff --git a/src/file.cc b/src/file.cc index c80f5e58..8759c7c1 100644 --- a/src/file.cc +++ b/src/file.cc @@ -46,6 +46,7 @@ File::~File() File::File(boost::filesystem::path path, std::string mode) + : _path(path) { #ifdef LIBDCP_WINDOWS std::wstring mode_wide(mode.begin(), mode.end()); @@ -105,6 +106,34 @@ File::operator bool() const } +void +File::checked_write(void const * ptr, size_t size) +{ + size_t N = write(ptr, 1, size); + if (N != size) { + if (ferror(_file)) { + throw FileError("fwrite error", _path, errno); + } else { + throw FileError("Unexpected short write", _path, 0); + } + } +} + + +void +File::checked_read(void* ptr, size_t size) +{ + size_t N = read(ptr, 1, size); + if (N != size) { + if (ferror(_file)) { + throw FileError("fread error %1", _path, errno); + } else { + throw FileError("Unexpected short read", _path, 0); + } + } +} + + /** Windows can't "by default" cope with paths longer than 260 characters, so if you pass such a path to * any boost::filesystem method it will fail. There is a "fix" for this, which is to prepend * the string \\?\ to the path. This will make it work, so long as: @@ -64,12 +64,16 @@ public: /** fgets() wrapper */ char *gets(char *s, int size); + void checked_write(void const * ptr, size_t size); + void checked_read(void* ptr, size_t size); + /** Close the file; it is not necessary to call this as the * destructor will do it if required. */ void close(); private: + boost::filesystem::path _path; FILE* _file = nullptr; }; |
