diff options
| author | Carl Hetherington <cth@carlh.net> | 2022-04-13 23:19:17 +0200 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2022-05-05 21:46:30 +0200 |
| commit | 6387446a183cf99803beb1bc68af8788601b73ce (patch) | |
| tree | f73077b20b1ad3235da68967f6b1e924a9f1662f /src | |
| parent | 75002393c81ca0d26b07069e6f92e6f82753882f (diff) | |
Add checked_{read,write} from DoM.
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; }; |
