Remove Fade{Up,Down}Time tags when writing text FOR_CCAP.
[libdcp.git] / src / file.cc
index ce7a4c5e224a9365e403092e9771a736515893e8..36102f72081d979b7a61bac5ca4609805beb87d8 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2022 Carl Hetherington <cth@carlh.net>
 
     This file is part of libdcp.
 
     files in the program, then also delete it here.
 */
 
-/** @file  src/file.cc
- *  @brief File class.
- */
 
-#include "file.h"
-#include "util.h"
 #include "dcp_assert.h"
-#include "compose.hpp"
+#include "file.h"
+#include "filesystem.h"
+#ifdef LIBDCP_WINDOWS
+#include <errhandlingapi.h>
+#endif
 #include <stdio.h>
 
+
 using namespace dcp;
 
-/** Read a file into memory.
- *  @param file to read.
- */
-File::File (boost::filesystem::path file)
-{
-       _size = boost::filesystem::file_size (file);
-       _data = new uint8_t[_size];
-       FILE* f = dcp::fopen_boost (file, "rb");
-       DCP_ASSERT (f);
-       int const N = fread (_data, 1, _size, f);
-       if (N != _size) {
-               if (ferror(f)) {
-                       fclose (f);
-                       throw FileError (String::compose("fread error %1", errno), file, errno);
+
+File::~File()
+{
+       close();
+}
+
+
+File::File(boost::filesystem::path path, std::string mode)
+       : _path(path)
+{
+#ifdef LIBDCP_WINDOWS
+       SetLastError(0);
+       std::wstring mode_wide(mode.begin(), mode.end());
+       /* c_str() here should give a UTF-16 string */
+       _file = _wfopen(dcp::filesystem::fix_long_path(path).c_str(), mode_wide.c_str());
+       if (!_file) {
+               _open_error = GetLastError();
+       }
+#else
+        _file = fopen(path.c_str(), mode.c_str());
+       if (!_file) {
+               _open_error = errno;
+       }
+#endif
+}
+
+
+File::File(File&& other)
+       : _path(other._path)
+       , _file(other._file)
+       , _open_error(other._open_error)
+{
+       other._file = nullptr;
+}
+
+
+File&
+File::operator=(File&& other)
+{
+       if (*this != other) {
+               close();
+               _file = other._file;
+               _open_error = other._open_error;
+               other._file = nullptr;
+       }
+       return *this;
+}
+
+
+void
+File::close()
+{
+       if (_file) {
+               fclose(_file);
+               _file = nullptr;
+       }
+}
+
+
+size_t
+File::write(const void *ptr, size_t size, size_t nmemb)
+{
+       DCP_ASSERT(_file);
+       return fwrite(ptr, size, nmemb, _file);
+}
+
+
+size_t
+File::read(void *ptr, size_t size, size_t nmemb)
+{
+       DCP_ASSERT(_file);
+       return fread(ptr, size, nmemb, _file);
+}
+
+
+int
+File::eof()
+{
+       DCP_ASSERT(_file);
+       return feof(_file);
+}
+
+
+char *
+File::gets(char* s, int size)
+{
+       DCP_ASSERT(_file);
+       return fgets(s, size, _file);
+}
+
+
+int
+File::puts(char const* s)
+{
+       DCP_ASSERT(_file);
+       return fputs(s, _file);
+}
+
+
+File::operator bool() const
+{
+       return _file != nullptr;
+}
+
+
+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 {
-                       fclose (f);
-                       throw FileError ("unexpected short read", file, -1);
+                       throw FileError("Unexpected short write", _path, 0);
                }
        }
-       fclose (f);
 }
 
-/** File destructor */
-File::~File ()
+
+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);
+               }
+       }
+}
+
+
+FILE*
+File::take()
+{
+       auto give = _file;
+       _file = nullptr;
+       return give;
+}
+
+
+int
+File::seek(int64_t offset, int whence)
+{
+       DCP_ASSERT(_file);
+#ifdef LIBDCP_WINDOWS
+       return _fseeki64(_file, offset, whence);
+#else
+       return fseek(_file, offset, whence);
+#endif
+}
+
+
+int64_t
+File::tell()
+{
+       DCP_ASSERT(_file);
+#ifdef LIBDCP_WINDOWS
+       return _ftelli64(_file);
+#else
+       return ftell(_file);
+#endif
+}
+
+
+
+int
+File::error ()
 {
-       delete[] _data;
+       DCP_ASSERT(_file);
+       return ferror(_file);
 }