Cleanup: sorting.
[libdcp.git] / src / file.h
1 /*
2     Copyright (C) 2022 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libdcp is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34
35 #ifndef LIBDCP_FILE_H
36 #define LIBDCP_FILE_H
37
38
39 #include <boost/filesystem/path.hpp>
40
41
42 namespace dcp {
43
44
45 /** A wrapper for stdio files that gives RAII and allows us to open files with
46  *  UTF-8 names on Windows.
47  */
48 class File
49 {
50 public:
51         /** @param path Path to open
52          *  @param mode mode flags, as for fopen(3)
53          */
54         File(boost::filesystem::path, std::string mode);
55
56         File(File&& other);
57         File& operator=(File&& other);
58
59         ~File();
60
61         File(File const&) = delete;
62         File& operator=(File const&) = delete;
63
64         operator bool() const;
65
66         /** fwrite() wrapper */
67         size_t write(const void *ptr, size_t size, size_t nmemb);
68         /** fread() wrapper */
69         size_t read(void *ptr, size_t size, size_t nmemb);
70         /** feof() wrapper */
71         int eof();
72         /** fgets() wrapper */
73         char *gets(char *s, int size);
74         /** fputs() wrapper */
75         int puts(char const *s);
76         /** fseek/fseeki64 wrapper */
77         int seek(int64_t offset, int whence);
78         /** ftell/ftelli64 wrapper */
79         int64_t tell();
80         /** ferror wrapper */
81         int error();
82
83         void checked_write(void const * ptr, size_t size);
84         void checked_read(void* ptr, size_t size);
85
86         /** Close the file; it is not necessary to call this as the
87          *  destructor will do it if required.
88          */
89         void close();
90
91         boost::filesystem::path path() const {
92                 return _path;
93         }
94
95         /** Take ownership of the underlying FILE*;
96          *  the File object will not closed it after this call.
97          */
98         FILE* take();
99
100         FILE* get() {
101                 return _file;
102         }
103
104         /** @return Error returned by the fopen / _wfopen call;
105          *  errno on POSIX, GetLastError() on Windows.
106          */
107         int open_error() const {
108                 return _open_error;
109         }
110
111 private:
112         boost::filesystem::path _path;
113         FILE* _file = nullptr;
114         int _open_error = 0;
115 };
116
117
118 boost::filesystem::path fix_long_path(boost::filesystem::path long_path);
119
120
121 }
122
123
124 #endif