Fixposition SDK 0.0.0-heads/main-0-g408dc89
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
32namespace fpsdk {
33namespace common {
34/**
35 * @brief File and path utilities
36 */
37namespace path {
38/* ****************************************************************************************************************** */
39
40/**
41 * @brief Check if path exists
42 *
43 * @param[in] path Path (file or directory) to check
44 *
45 * @returns true if path exists, false otherwise
46 */
47bool PathExists(const std::string& path);
48
49/**
50 * @brief Check if path is a directory
51 *
52 * @param[in] path Path to check
53 *
54 * @returns true if path is a directory, false otherwise
55 */
56bool PathIsDirectory(const std::string& path);
57
58/**
59 * @brief Check if path is a regular file
60 *
61 * @param[in] path Path to check
62 *
63 * @returns true if path is a regular file, false otherwise
64 */
65bool PathIsFile(const std::string& path);
66
67/**
68 * @brief Check if path is a symlink
69 *
70 * @param[in] path Path to check
71 *
72 * @returns true if path is a symlink, false otherwise
73 */
74bool PathIsSymlink(const std::string& path);
75
76/**
77 * @brief Check if path is readable
78 *
79 * @param[in] path Path (file or directory) to check
80 *
81 * @returns true if path is readable, false otherwise
82 */
83bool PathIsReadable(const std::string& path);
84
85/**
86 * @brief Check if path is writable
87 *
88 * @param[in] path Path (file or directory) to check
89 *
90 * @returns true if path is writable, false otherwise
91 */
92bool PathIsWritable(const std::string& path);
93
94/**
95 * @brief Check if path is executable
96 *
97 * @param[in] path Path (file or directory) to check
98 *
99 * @returns true if path is executable, false otherwise
100 */
101bool PathIsExecutable(const std::string& path);
102
103/**
104 * @brief Get file size
105 *
106 * @param[in] path Path to file
107 *
108 * @returns the size of the file in bytes, 0 is only valid if file is readable
109 */
110std::size_t FileSize(const std::string& path);
111
112/**
113 * @brief Output file handle
114 */
115class OutputFile
116{
117 public:
118 OutputFile();
119 ~OutputFile();
120
121 /**
122 * @brief Open output file for writing
123 *
124 * @param[in] path The path / filename, can end in ".gz" for compressed output
125 *
126 * @returns true on success, failure otherwise
127 */
128 bool Open(const std::string& path);
129
130 /**
131 * @brief Close output file
132 */
133 void Close();
134
135 /**
136 * @brief Write data to file
137 *
138 * @param[in] data The data to be written
139 *
140 * @returns true on success, false otherwise
141 */
142 bool Write(const std::vector<uint8_t>& data);
143
144 /**
145 * @brief Write data to file
146 *
147 * @param[in] data The data to be written
148 * @param[in] size The size of the data to be written
149 *
150 * @returns true on success, false otherwise
151 */
152 bool Write(const uint8_t* data, const std::size_t size);
153
154 /**
155 * @brief Get file path
156 *
157 * @returns the file path if the file has been opened before, the empty string otherwise
158 */
159 const std::string& Path() const;
160
161 /**
162 * @brief Check if output file is open
163 *
164 * @returns true if the file is open, false otherwise
165 */
166 bool IsOpen() const;
167
168 /**
169 * @brief Get file size
170 *
171 * @returns the size written to the file so far
172 */
173 std::size_t Size() const;
174
175 /**
176 * @brief Get error
177 *
178 * @returns an error string in case of error (e.g. after Write() failed), the empty string if there's no error
179 */
180 const std::string& Error() const;
181
182 private:
183 std::string path_; //!< File path
184 std::unique_ptr<std::ostream> fh_; //!< File handle
185 std::size_t size_ = 0; //!< Size written
186 std::string error_; //!< Last error
187};
188
189/**
190 * @brief Input file handle
191 */
192class InputFile
193{
194 public:
195 InputFile();
196 ~InputFile();
197
198 /**
199 * @brief Open input file for reading
200 *
201 * @param[in] path The path / filename, can end in ".gz" for compressed output
202 *
203 * @returns true on success, failure otherwise
204 */
205 bool Open(const std::string& path);
206
207 /**
208 * @brief Close input file
209 */
210 void Close();
211
212 /**
213 * @brief Read data from file
214 *
215 * @param[out] data Buffer to read into
216 * @param[in] size Size of buffer (> 0)
217 *
218 * @returns the size read, 0 if at end of file
219 */
220 std::size_t Read(uint8_t* data, const std::size_t size);
221
222 /**
223 * @brief Get file path
224 *
225 * @returns the file path if the file has been opened before, the empty string otherwise
226 */
227 const std::string& Path() const;
228
229 /**
230 * @brief Check if output file is open
231 *
232 * @returns true if the file is open, false otherwise
233 */
234 bool IsOpen() const;
235
236 /**
237 * @brief Get file size
238 *
239 * @returns the file size
240 */
241 std::size_t Size() const;
242
243 /**
244 * @brief Get file position
245 *
246 * @returns the current file position
247 */
248 std::size_t Tell() const;
249
250 /**
251 * @brief Seek to position
252 *
253 * @param[in] pos Position to seek to
254 *
255 * @returns true on success, false otherwise (our of range, cannot seek)
256 */
257 bool Seek(const std::size_t pos);
258
259 /**
260 * @brief Check if handle can seek
261 *
262 * @returns true if Seek() is possible, false if not (e.g. compressed file)
263 */
264 bool CanSeek() const;
265
266 /**
267 * @brief Get error
268 *
269 * @returns an error string in case of error (e.g. after Open() failed), the empty string if there's no error
270 */
271 const std::string& Error() const;
272
273 private:
274 std::string path_; //!< File path
275 std::unique_ptr<std::istream> fh_; //!< File handle
276 std::size_t size_ = 0; //!< File size
277 std::size_t pos_ = 0; //!< File position
278 std::string error_; //!< Last error
279 bool can_seek_ = false; //!< Seek possible?
280};
281
282/**
283 * @brief Read entire file into a string
284 *
285 * @param[in] path File path
286 * @param[out] data The data
287 *
288 * @returns true if data was successfully read from file, false otherwise
289 */
290bool FileSlurp(const std::string& path, std::vector<uint8_t>& data);
291
292/**
293 * @brief Write string to file
294 *
295 * @param[in] path File path
296 * @param[out] data The data
297 *
298 * @returns true if data was successfully written to file, false otherwise
299 */
300bool FileSpew(const std::string& path, const std::vector<uint8_t>& data);
301
302/* ****************************************************************************************************************** */
303} // namespace path
304} // namespace common
305} // namespace fpsdk
306#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.
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.
File and path utilities.
Definition path.hpp:37
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.
bool PathExists(const std::string &path)
Check if path exists.
std::size_t FileSize(const std::string &path)
Get file size.
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.