File::File(boost::filesystem::path path, std::string mode)
+ : _path(path)
{
#ifdef LIBDCP_WINDOWS
std::wstring mode_wide(mode.begin(), mode.end());
}
+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:
/** 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;
};