Fixposition SDK 0.0.0-heads/main-0-gd0a6ce2
Collection of c++ libraries and apps for use with Fixposition products
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 */
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 * @brief Write data to file
145 *
146 * @param[in] data The data to be written
147 * @param[in] size The size of the data to be written
148 *
149 * @returns true on success, false otherwise
150 */
151 bool Write(const uint8_t* data, const std::size_t size);
152
153 /**
154 * @brief Get file path
155 *
156 * @returns the file path if the file has been opened before, the empty string otherwise
157 */
158 const std::string& GetPath() const;
159
160 private:
161 std::string path_; //!< File path
162 std::unique_ptr<std::ostream> fh_; //!< File handle
163};
164
165/**
166 * @brief Read entire file into a string
167 *
168 * @param[in] path File path
169 * @param[out] data The data
170 *
171 * @returns true if data was successfully read from file, false otherwise
172 */
173bool FileSlurp(const std::string& path, std::vector<uint8_t>& data);
174
175/**
176 * @brief Write string to file
177 *
178 * @param[in] path File path
179 * @param[out] data The data
180 *
181 * @returns true if data was successfully written to file, false otherwise
182 */
183bool FileSpew(const std::string& path, const std::vector<uint8_t>& data);
184
185/* ****************************************************************************************************************** */
186} // namespace path
187} // namespace common
188} // namespace fpsdk
189#endif // __FPSDK_COMMON_PATH_HPP__
Output file handle.
Definition path.hpp:116
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 & GetPath() const
Get file path.
void Close()
Close output file.
bool Write(const std::vector< uint8_t > &data)
Write data to file.
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.