Fixposition SDK 0.0.0-heads/main-0-g90a51ff
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 Get file size
51 *
52 * @param[in] path Path to file
53 *
54 * @returns the size of the file in bytes, 0 is only valid if file is readable
55 */
56std::size_t FileSize(const std::string& path);
57
58/**
59 * @brief Output file handle
60 */
62{
63 public:
64 OutputFile();
66
67 /**
68 * @brief Open output file for writing
69 *
70 * @param[in] path The path / filename, can end in ".gz" for compressed output
71 *
72 * @returns true on success, failure otherwise
73 */
74 bool Open(const std::string& path);
75
76 /**
77 * @brief Close output file
78 */
79 void Close();
80
81 /**
82 * @brief Write data to file
83 *
84 * @param[in] data The data to be written
85 *
86 * @returns true on success, false otherwise
87 */
88 bool Write(const std::vector<uint8_t>& data);
89 /**
90 * @brief Write data to file
91 *
92 * @param[in] data The data to be written
93 * @param[in] size The size of the data to be written
94 *
95 * @returns true on success, false otherwise
96 */
97 bool Write(const uint8_t* data, const std::size_t size);
98
99 /**
100 * @brief Get file path
101 *
102 * @returns the file path if the file has been opened before, the empty string otherwise
103 */
104 const std::string& GetPath() const;
105
106 private:
107 std::string path_; //!< File path
108 std::unique_ptr<std::ostream> fh_; //!< File handle
109};
110
111/**
112 * @brief Read entire file into a string
113 *
114 * @param[in] path File path
115 * @param[out] data The data
116 *
117 * @returns true if data was successfully read from file, false otherwise
118 */
119bool FileSlurp(const std::string& path, std::vector<uint8_t>& data);
120
121/**
122 * @brief Write string to file
123 *
124 * @param[in] path File path
125 * @param[out] data The data
126 *
127 * @returns true if data was successfully written to file, false otherwise
128 */
129bool FileSpew(const std::string& path, const std::vector<uint8_t>& data);
130
131/* ****************************************************************************************************************** */
132} // namespace path
133} // namespace common
134} // namespace fpsdk
135#endif // __FPSDK_COMMON_PATH_HPP__
Output file handle.
Definition path.hpp:62
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 PathExists(const std::string &path)
Check if path exists.
std::size_t FileSize(const std::string &path)
Get file size.
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.
Fixposition SDK.