File include/jlib/fs/file.h changed (mode: 100644) (index cb2d99f..a034ddd) |
27 |
27 |
|
|
28 |
28 |
namespace jl::fs { |
namespace jl::fs { |
29 |
29 |
/** |
/** |
30 |
|
* @brief Get whole file content. |
|
|
30 |
|
* @brief Get whole file binary content. |
31 |
31 |
* |
* |
32 |
32 |
* After this function call is successfull, |
* After this function call is successfull, |
33 |
33 |
* memory must be deallocated by jl::deallocate, when no longer needed. |
* memory must be deallocated by jl::deallocate, when no longer needed. |
|
... |
... |
namespace jl::fs { |
44 |
44 |
*/ |
*/ |
45 |
45 |
[[nodiscard]] bool |
[[nodiscard]] bool |
46 |
46 |
read_file(const char *filename, uint8_t **pp_dst, uint64_t *p_size); |
read_file(const char *filename, uint8_t **pp_dst, uint64_t *p_size); |
|
47 |
|
/** |
|
48 |
|
* @brief Get whole file textual content |
|
49 |
|
* @see read_file |
|
50 |
|
*/ |
|
51 |
|
[[nodiscard]] bool |
|
52 |
|
read_file(const char *filename, char **pp_dst, uint64_t *p_size); |
47 |
53 |
|
|
48 |
54 |
[[nodiscard]] bool |
[[nodiscard]] bool |
49 |
55 |
write_file(const char *filename, const uint8_t *p_src, uint64_t size); |
write_file(const char *filename, const uint8_t *p_src, uint64_t size); |
|
... |
... |
struct jl::fs::stream |
65 |
71 |
WRITE_TO_END = 0b00001000, ///< writing to the end of stream |
WRITE_TO_END = 0b00001000, ///< writing to the end of stream |
66 |
72 |
TEXT = 0b00010000, ///< operate as text stream |
TEXT = 0b00010000, ///< operate as text stream |
67 |
73 |
}; |
}; |
|
74 |
|
using mask = uint8_t; |
68 |
75 |
/** |
/** |
69 |
76 |
* @brief Open file |
* @brief Open file |
70 |
77 |
* After successful file opening, |
* After successful file opening, |
|
... |
... |
struct jl::fs::stream |
76 |
83 |
* @see fs::error_no |
* @see fs::error_no |
77 |
84 |
* @see stream::close |
* @see stream::close |
78 |
85 |
*/ |
*/ |
79 |
|
[[nodiscard]] bool open_file(const char *path, flag flags); |
|
|
86 |
|
[[nodiscard]] bool open_file(const char *path, mask flags); |
80 |
87 |
/** |
/** |
81 |
88 |
* @brief Close stream |
* @brief Close stream |
82 |
89 |
* Must be used only if stream is opened. |
* Must be used only if stream is opened. |
File include/jlib/fs/file.hpp changed (mode: 100644) (index 8635483..3d68bdc) |
... |
... |
namespace jl::fs::details |
58 |
58 |
} |
} |
59 |
59 |
/// Struct with c-string type mode for file opening. |
/// Struct with c-string type mode for file opening. |
60 |
60 |
struct mode { |
struct mode { |
61 |
|
constexpr mode(stream::flag flags) : str() { |
|
|
61 |
|
constexpr mode(stream::mask flags) : str() { |
62 |
62 |
int str_size = 0; |
int str_size = 0; |
63 |
63 |
if (flags & stream::flag::READ) { |
if (flags & stream::flag::READ) { |
64 |
64 |
if (flags & stream::flag::CLEAN) { |
if (flags & stream::flag::CLEAN) { |
|
... |
... |
namespace jl::fs::details |
100 |
100 |
} |
} |
101 |
101 |
} |
} |
102 |
102 |
[[nodiscard]] inline bool jl::fs::stream:: |
[[nodiscard]] inline bool jl::fs::stream:: |
103 |
|
open_file(const char *path, flag flags) { |
|
|
103 |
|
open_file(const char *path, mask flags) { |
104 |
104 |
details::mode mode(flags); |
details::mode mode(flags); |
105 |
105 |
p_file = fopen(path, mode.str); |
p_file = fopen(path, mode.str); |
106 |
106 |
return p_file != nullptr; |
return p_file != nullptr; |
|
... |
... |
write_string(const char *p_src) { |
209 |
209 |
write_string_locked(const char *p_src) { |
write_string_locked(const char *p_src) { |
210 |
210 |
return details::write_str<true>(p_file, p_src); |
return details::write_str<true>(p_file, p_src); |
211 |
211 |
} |
} |
212 |
|
[[nodiscard]] inline jl::fs::stream::flag |
|
213 |
|
operator | (jl::fs::stream::flag _1, jl::fs::stream::flag _2) { |
|
214 |
|
return jl::fs::stream::flag(uint8_t(_1) | _2); |
|
215 |
|
} |
|
216 |
|
[[nodiscard]] inline bool jl::fs:: |
|
217 |
|
read_file(const char *filename, uint8_t **pp_dst, uint64_t *p_size) { |
|
218 |
|
jl::fs::stream file; |
|
219 |
|
if (not file.open_file(filename, jl::fs::stream::flag::READ)) |
|
220 |
|
return false; |
|
221 |
|
if (file.pos_set_to_end()) { |
|
222 |
|
size_t size = (*p_size) = size_t(file.pos()); |
|
223 |
|
if (not file.pos_set(0)) |
|
224 |
|
goto CANCEL; |
|
225 |
|
if (not jl::allocate_bytes(pp_dst, size)) |
|
226 |
|
goto CANCEL; |
|
227 |
|
if (not file.read_bytes(*pp_dst, size)) { |
|
228 |
|
jl::deallocate(pp_dst); |
|
229 |
|
goto CANCEL; |
|
|
212 |
|
namespace jl::fs::detail { |
|
213 |
|
template<int MASK, typename T> |
|
214 |
|
[[nodiscard]] inline bool |
|
215 |
|
read_file(const char *filename, T **pp_dst, uint64_t *p_size) { |
|
216 |
|
jl::fs::stream file; |
|
217 |
|
if (not file.open_file(filename, MASK)) |
|
218 |
|
return false; |
|
219 |
|
if (file.pos_set_to_end()) { |
|
220 |
|
size_t size = (*p_size) = size_t(file.pos()); |
|
221 |
|
if (not file.pos_set(0)) |
|
222 |
|
goto CANCEL; |
|
223 |
|
if (not jl::allocate_bytes(pp_dst, size)) |
|
224 |
|
goto CANCEL; |
|
225 |
|
if (not file.read_bytes(*pp_dst, size)) { |
|
226 |
|
jl::deallocate(pp_dst); |
|
227 |
|
goto CANCEL; |
|
228 |
|
} |
|
229 |
|
file.close(); |
|
230 |
|
return true; |
230 |
231 |
} |
} |
|
232 |
|
CANCEL: |
231 |
233 |
file.close(); |
file.close(); |
232 |
|
return true; |
|
|
234 |
|
return false; |
233 |
235 |
} |
} |
234 |
|
CANCEL: |
|
235 |
|
file.close(); |
|
236 |
|
return false; |
|
|
236 |
|
} |
|
237 |
|
[[nodiscard]] inline bool jl::fs:: |
|
238 |
|
read_file(const char *filename, uint8_t **pp_dst, uint64_t *p_size) { |
|
239 |
|
constexpr static const |
|
240 |
|
stream::mask m = jl::fs::stream::flag::READ; |
|
241 |
|
return detail::read_file<m>(filename, pp_dst, p_size); |
|
242 |
|
} |
|
243 |
|
[[nodiscard]] inline bool jl::fs:: |
|
244 |
|
read_file(const char *filename, char **pp_dst, uint64_t *p_size) { |
|
245 |
|
constexpr static const |
|
246 |
|
stream::mask m = jl::fs::stream::flag::READ | jl::fs::stream::flag::TEXT; |
|
247 |
|
return detail::read_file<m>(filename, pp_dst, p_size); |
237 |
248 |
} |
} |
238 |
249 |
[[nodiscard]] inline bool jl::fs:: |
[[nodiscard]] inline bool jl::fs:: |
239 |
250 |
write_file(const char *filename, const uint8_t *p_src, uint64_t size) { |
write_file(const char *filename, const uint8_t *p_src, uint64_t size) { |
240 |
|
jl::fs::stream file; |
|
241 |
|
auto f = jl::fs::stream::flag::WRITE | jl::fs::stream::flag::CLEAN; |
|
|
251 |
|
stream file; |
|
252 |
|
stream::mask f = jl::fs::stream::flag::WRITE | jl::fs::stream::flag::CLEAN; |
242 |
253 |
if (not file.open_file(filename, f)) |
if (not file.open_file(filename, f)) |
243 |
254 |
return false; |
return false; |
244 |
255 |
bool res = file.write_bytes(p_src, size) == size; |
bool res = file.write_bytes(p_src, size) == size; |