Ocean
Loading...
Searching...
No Matches
File.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_IO_FILE_H
9#define META_OCEAN_IO_FILE_H
10
11#include "ocean/io/IO.h"
12#include "ocean/io/Path.h"
13
14#include <vector>
15
16namespace Ocean
17{
18
19namespace IO
20{
21
22// Forward declaration.
23class File;
24
25/**
26 * Definition of a vector holding files.
27 * @ingroup io
28 */
29using Files = std::vector<File>;
30
31/**
32 * This class holds a file.
33 * @ingroup io
34 */
35class OCEAN_IO_EXPORT File : public Path
36{
37 public:
38
39 /**
40 * Creates an empty file.
41 */
43
44 /**
45 * Create a new file.
46 * @param filename File name
47 */
48 explicit File(const std::string& filename);
49
50 /**
51 * Returns whether the path is valid.
52 * @see Path::isValid();
53 */
54 bool isValid() const override;
55
56 /**
57 * Returns whether the file exists already.
58 * @see Path::exists(), remove().
59 */
60 bool exists() const override;
61
62 /**
63 * Removes this file from the filesystem.
64 * @return True, if succeeded
65 * @see exists().
66 */
67 bool remove() const;
68
69 /**
70 * Returns the base of this file.
71 * The file's base is the entire file path without file extension (and the character in front of the extension).<br>
72 * In case the file does not have an extension, the base is the entire file path.
73 * <pre>
74 * File: base():
75 * "example.bmp" "example"
76 * "example" "example"
77 * "example.tar.gz" "example.tar"
78 * "/first/second/image.png" "/first/second/image"
79 * "first.second/example" "first.second/example"
80 * </pre>
81 * @return File base
82 */
83 std::string base() const;
84
85 /**
86 * Returns the extension of this file.
87 * A dot within a directory does not separate an extension.
88 * <pre>
89 * File: extension():
90 * "example.bmp" "bmp"
91 * "example" ""
92 * "example.tar.gz" "gz"
93 * "/first/second/image.png" "png"
94 * "first.second/example" ""
95 * </pre>
96 * @return File extension, empty if the file does not have an extension
97 */
98 std::string extension() const;
99
100 /**
101 * Returns the name of this file.
102 * The file's name is the local filename including the file extension without the prefix path.
103 * <pre>
104 * File: name():
105 * "example.bmp" "example.bmp"
106 * "example" "example"
107 * "example.tar.gz" "example.tar.gz"
108 * "/first/second/image.png" "image.png"
109 * "first.second/example" "example"
110 * </pre>
111 * @return File name
112 */
113 std::string name() const;
114
115 /**
116 * Returns the base name of this file.
117 * The base name is the local filename without extension.
118 * <pre>
119 * File: baseName():
120 * "example.bmp" "example"
121 * "example" "example"
122 * "example.tar.gz" "example.tar"
123 * "/first/second/image.png" "image"
124 * "first.second/example" "example"
125 * </pre>
126 * @return File base name
127 */
128 std::string baseName() const;
129
130#if defined(__APPLE__)
131
132 /**
133 * Returns whether a file exists (specialization for Apple platforms).
134 * @param file The file to check
135 * @return True, if so
136 */
137 static bool existsApple(const std::string& file);
138
139#endif // defined(__APPLE__)
140
141 protected:
142
143 /**
144 * Returns the position of the dot separating the extension from the remaining path.
145 * A dot within a directory does not separate an extension, e.g. "first.second/example" does not have an extension.
146 * @param path The path for which the position will be determined
147 * @return The position of the dot, 'npos' if the path does not have an extension
148 */
149 static std::string::size_type extensionPosition(const std::string& path);
150};
151
152/**
153 * This class implements a scoped file object which will delete the underlying file from the filesystem when the scope ends.
154 * @see File
155 * @ingroup io
156 */
157class ScopedFile final : public File
158{
159 public:
160
161 /**
162 * Move constructor.
163 * @param scopedFile The scoped file to be moved
164 */
165 inline ScopedFile(ScopedFile&& scopedFile);
166
167 /**
168 * Create a new scoped file.
169 * @param file The file for which the new scoped object will be created
170 */
171 explicit inline ScopedFile(const File& file);
172
173 /**
174 * Create a new scoped file.
175 * @param filename The name of the file for which a new scoped object will be created
176 */
177 explicit inline ScopedFile(const std::string& filename);
178
179 /**
180 * Destructs this object and deletes the actual file.
181 */
183
184 /**
185 * Move operator.
186 * @param scopedFile The scoped file to be moved
187 * @return Reference to this object
188 */
190
191 protected:
192
193 /**
194 * Disabled copy constructor.
195 * @param scopedFile File which would be copied
196 */
197 ScopedFile(const ScopedFile& scopedFile) = delete;
198
199 /**
200 * Disabled copy constructor.
201 * @param scopedFile File which would be copied
202 * @return Reference to this object
203 */
204 ScopedFile& operator=(const ScopedFile& scopedFile) = delete;
205};
206
207inline ScopedFile::ScopedFile(ScopedFile&& scopedFile) :
208 File()
209{
210 *this = std::move(scopedFile);
211}
212
213inline ScopedFile::ScopedFile(const File& file) :
214 File(file)
215{
216 // nothing to do here
217}
218
219inline ScopedFile::ScopedFile(const std::string& filename) :
220 File(filename)
221{
222 // nothing to do here
223}
224
225}
226
227}
228
229#endif // META_OCEAN_IO_FILE_H
This class holds a file.
Definition File.h:36
std::string baseName() const
Returns the base name of this file.
std::string name() const
Returns the name of this file.
static std::string::size_type extensionPosition(const std::string &path)
Returns the position of the dot separating the extension from the remaining path.
std::string extension() const
Returns the extension of this file.
std::string base() const
Returns the base of this file.
static bool existsApple(const std::string &file)
Returns whether a file exists (specialization for Apple platforms).
bool isValid() const override
Returns whether the path is valid.
bool remove() const
Removes this file from the filesystem.
bool exists() const override
Returns whether the file exists already.
File(const std::string &filename)
Create a new file.
File()
Creates an empty file.
This class holds a path.
Definition Path.h:27
This class implements a scoped file object which will delete the underlying file from the filesystem ...
Definition File.h:158
~ScopedFile()
Destructs this object and deletes the actual file.
ScopedFile & operator=(ScopedFile &&scopedFile)
Move operator.
ScopedFile & operator=(const ScopedFile &scopedFile)=delete
Disabled copy constructor.
ScopedFile(ScopedFile &&scopedFile)
Move constructor.
Definition File.h:207
ScopedFile(const ScopedFile &scopedFile)=delete
Disabled copy constructor.
std::vector< File > Files
Definition of a vector holding files.
Definition File.h:29
The namespace covering the entire Ocean framework.
Definition Accessor.h:15