Fixposition SDK 0.0.0-heads/main-0-g97f6014
Collection of c++ libraries and apps for use with Fixposition products on Linux
Loading...
Searching...
No Matches
path.hpp
Go to the documentation of this file.
1/**
2 * \verbatim
3 * ___ ___
4 * \ \ / /
5 * \ \/ / Copyright (c) Fixposition AG (www.fixposition.com) and contributors
6 * / /\ \ License: see the LICENSE file
7 * /__/ \__\
8 * \endverbatim
9 *
10 * @file
11 * @brief Fixposition SDK: File and path utilities
12 *
13 * @page FPSDK_COMMON_PATH File and path utilities
14 *
15 * **API**: fpsdk_common/path.hpp and fpsdk::common::path
16 *
17 */
18#ifndef __FPSDK_COMMON_PATH_HPP__
19#define __FPSDK_COMMON_PATH_HPP__
20
21/* LIBC/STL */
22#include <cstdint>
23#include <iostream>
24#include <memory>
25#include <string>
26#include <vector>
27
28/* EXTERNAL */
29
30/* PACKAGE */
31#include "types.hpp"
32
33namespace fpsdk {
34namespace common {
35/**
36 * @brief File and path utilities
37 */
38namespace path {
39/* ****************************************************************************************************************** */
40
41/**
42 * @brief Check if path exists
43 *
44 * @param[in] path Path (file or directory) to check
45 *
46 * @returns true if path exists, false otherwise
47 */
48bool PathExists(const std::string& path);
49
50/**
51 * @brief Check if path is a directory
52 *
53 * @param[in] path Path to check
54 *
55 * @returns true if path is a directory, false otherwise
56 */
57bool PathIsDirectory(const std::string& path);
58
59/**
60 * @brief Check if path is a regular file
61 *
62 * @param[in] path Path to check
63 *
64 * @returns true if path is a regular file, false otherwise
65 */
66bool PathIsFile(const std::string& path);
67
68/**
69 * @brief Check if path is a symlink
70 *
71 * @param[in] path Path to check
72 *
73 * @returns true if path is a symlink, false otherwise
74 */
75bool PathIsSymlink(const std::string& path);
76
77/**
78 * @brief Check if path is readable
79 *
80 * @param[in] path Path (file or directory) to check
81 *
82 * @returns true if path is readable, false otherwise
83 */
84bool PathIsReadable(const std::string& path);
85
86/**
87 * @brief Check if path is writable
88 *
89 * @param[in] path Path (file or directory) to check
90 *
91 * @returns true if path is writable, false otherwise
92 */
93bool PathIsWritable(const std::string& path);
94
95/**
96 * @brief Check if path is executable
97 *
98 * @param[in] path Path (file or directory) to check
99 *
100 * @returns true if path is executable, false otherwise
101 */
102bool PathIsExecutable(const std::string& path);
103
104/**
105 * @brief Get file size
106 *
107 * @param[in] path Path to file
108 *
109 * @returns the size of the file in bytes, 0 is only valid if file is readable
110 */
111std::size_t FileSize(const std::string& path);
112
113/**
114 * @brief Get directory size
115 *
116 * @param[in] path Path to directory
117 *
118 * @returns the size of all the files in the directory in bytes, 0 is only valid if file is readable
119 */
120std::size_t DirSize(const std::string& path);
121
122/**
123 * @brief Remove, recursively
124 *
125 * Removes the path, including all its subdirectories, recursively.
126 *
127 * @param[in] path The path to remove
128 */
129void RemoveAll(const std::string& path);
130
131/**
132 * @brief Output file handle
133 */
134class OutputFile : private types::NoCopyNoMove
135{
136 public:
137 OutputFile();
138 ~OutputFile();
139
140 /**
141 * @brief Open output file for writing
142 *
143 * @param[in] path The path / filename, can end in ".gz" for compressed output
144 *
145 * @returns true on success, failure otherwise
146 */
147 bool Open(const std::string& path);
148
149 /**
150 * @brief Close output file
151 */
152 void Close();
153
154 /**
155 * @brief Write data to file
156 *
157 * @param[in] data The data to be written
158 *
159 * @returns true on success, false otherwise
160 */
161 bool Write(const std::vector<uint8_t>& data);
162
163 /**
164 * @brief Write data to file
165 *
166 * @param[in] data The data to be written
167 * @param[in] size The size of the data to be written
168 *
169 * @returns true on success, false otherwise
170 */
171 bool Write(const uint8_t* data, const std::size_t size);
172
173 /**
174 * @brief Write data to file
175 *
176 * @param[in] data The data to be written
177 *
178 * @returns true on success, false otherwise
179 */
180 bool Write(const std::string& data);
181
182 /**
183 * @brief Get file path
184 *
185 * @returns the file path if the file has been opened before, the empty string otherwise
186 */
187 const std::string& Path() const;
188
189 /**
190 * @brief Check if output file is open
191 *
192 * @returns true if the file is open, false otherwise
193 */
194 bool IsOpen() const;
195
196 /**
197 * @brief Get file size
198 *
199 * @returns the size written to the file so far
200 */
201 std::size_t Size() const;
202
203 /**
204 * @brief Get error
205 *
206 * @returns an error string in case of error (e.g. after Write() failed), the empty string if there's no error
207 */
208 const std::string& Error() const;
209
210 private:
211 std::string path_; //!< File path
212 std::unique_ptr<std::ostream> fh_; //!< File handle
213 std::size_t size_ = 0; //!< Size written
214 std::string error_; //!< Last error
215};
216
217/**
218 * @brief Input file handle
219 */
220class InputFile
221{
222 public:
223 InputFile();
224 ~InputFile();
225
226 /**
227 * @brief Open input file for reading
228 *
229 * @param[in] path The path / filename, can end in ".gz" for compressed output
230 *
231 * @returns true on success, failure otherwise
232 */
233 bool Open(const std::string& path);
234
235 /**
236 * @brief Close input file
237 */
238 void Close();
239
240 /**
241 * @brief Read data from file
242 *
243 * @param[out] data Buffer to read into
244 * @param[in] size Size of buffer (> 0)
245 *
246 * @returns the size read, 0 if at end of file
247 */
248 std::size_t Read(uint8_t* data, const std::size_t size);
249
250 /**
251 * @brief Get file path
252 *
253 * @returns the file path if the file has been opened before, the empty string otherwise
254 */
255 const std::string& Path() const;
256
257 /**
258 * @brief Check if output file is open
259 *
260 * @returns true if the file is open, false otherwise
261 */
262 bool IsOpen() const;
263
264 /**
265 * @brief Get file size
266 *
267 * @returns the file size
268 */
269 std::size_t Size() const;
270
271 /**
272 * @brief Get file position
273 *
274 * @returns the current file position
275 */
276 std::size_t Tell() const;
277
278 /**
279 * @brief Seek to position
280 *
281 * @param[in] pos Position to seek to
282 *
283 * @returns true on success, false otherwise (our of range, cannot seek)
284 */
285 bool Seek(const std::size_t pos);
286
287 /**
288 * @brief Check if handle can seek
289 *
290 * @returns true if Seek() is possible, false if not (e.g. compressed file)
291 */
292 bool CanSeek() const;
293
294 /**
295 * @brief Get error
296 *
297 * @returns an error string in case of error (e.g. after Open() failed), the empty string if there's no error
298 */
299 const std::string& Error() const;
300
301 private:
302 std::string path_; //!< File path
303 std::unique_ptr<std::istream> fh_; //!< File handle
304 std::size_t size_ = 0; //!< File size
305 std::size_t pos_ = 0; //!< File position
306 std::string error_; //!< Last error
307 bool can_seek_ = false; //!< Seek possible?
308};
309
310/**
311 * @brief Read entire file into a string
312 *
313 * @param[in] path File path
314 * @param[out] data The data
315 *
316 * @returns true if data was successfully read from file, false otherwise
317 */
318bool FileSlurp(const std::string& path, std::vector<uint8_t>& data);
319
320/**
321 * @brief Write string to file
322 *
323 * @param[in] path File path
324 * @param[out] data The data
325 *
326 * @returns true if data was successfully written to file, false otherwise
327 */
328bool FileSpew(const std::string& path, const std::vector<uint8_t>& data);
329
330/* ****************************************************************************************************************** */
331} // namespace path
332} // namespace common
333} // namespace fpsdk
334#endif // __FPSDK_COMMON_PATH_HPP__
bool Seek(const std::size_t pos)
Seek to position.
const std::string & Path() const
Get file path.
std::size_t Tell() const
Get file position.
std::size_t Read(uint8_t *data, const std::size_t size)
Read data from file.
void Close()
Close input file.
bool IsOpen() const
Check if output file is open.
bool CanSeek() const
Check if handle can seek.
const std::string & Error() const
Get error.
bool Open(const std::string &path)
Open input file for reading.
std::size_t Size() const
Get file size.
bool Open(const std::string &path)
Open output file for writing.
bool Write(const uint8_t *data, const std::size_t size)
Write data to file.
const std::string & Path() const
Get file path.
bool IsOpen() const
Check if output file is open.
bool Write(const std::string &data)
Write data to file.
std::size_t Size() const
Get file size.
void Close()
Close output file.
bool Write(const std::vector< uint8_t > &data)
Write data to file.
const std::string & Error() const
Get error.
Base class to prevent copy or move.
Definition types.hpp:97
File and path utilities.
Definition path.hpp:38
bool PathIsFile(const std::string &path)
Check if path is a regular file.
bool PathIsSymlink(const std::string &path)
Check if path is a symlink.
bool PathIsExecutable(const std::string &path)
Check if path is executable.
bool PathIsWritable(const std::string &path)
Check if path is writable.
std::size_t DirSize(const std::string &path)
Get directory size.
bool PathExists(const std::string &path)
Check if path exists.
std::size_t FileSize(const std::string &path)
Get file size.
void RemoveAll(const std::string &path)
Remove, recursively.
bool PathIsDirectory(const std::string &path)
Check if path is a directory.
bool FileSlurp(const std::string &path, std::vector< uint8_t > &data)
Read entire file into a string.
bool FileSpew(const std::string &path, const std::vector< uint8_t > &data)
Write string to file.
bool PathIsReadable(const std::string &path)
Check if path is readable.
Fixposition SDK: Common library.
Definition doc.hpp:21
Fixposition SDK.
Fixposition SDK: Common types and type helpers.