CommonLib
Loading...
Searching...
No Matches
file.h
Go to the documentation of this file.
1#pragma once
2
3#include <bit>
4#include <filesystem>
5#include <fstream>
6#include <functional>
7
8#include "hash/hash.h"
9#include "hash/hash_provider.h"
10#include "hash/xxhash64_provider.h"
11#include "ut/expr/string_expr.h"
12#include "ut/encoding.h"
13
14namespace hedgedev::csl::io::file
15{
16 template <typename T>
17 using hash_progress_callback_t = std::function<bool(T in_hash, uint64_t in_processed_bytes, uint64_t in_total_bytes)>;
18
19 ///
20 /// Computes a file hash.
21 ///
22 /// \param in_hash_provider The provider of the hash for the file.
23 /// \param in_path The file to hash.
24 /// \param out_hash The hash of the file.
25 /// \param in_from_beginning Determines whether to seek to the beginning of the
26 /// stream before computing the hash. The original position
27 /// will be restored after completion.
28 ///
29 /// \returns `true` if the hash was computed successfully. Otherwise, `false`.
30 ///
31 template <typename T>
32 inline bool try_compute_hash(hash::hash_provider<T>& in_hash_provider, std::ifstream& in_file, T& out_hash, bool in_from_beginning = true, hash_progress_callback_t<T> in_callback = nullptr);
33
34 ///
35 /// Computes a file hash.
36 ///
37 /// \tparam T The provider of the hash for the file. Uses xxHash64 by default.
38 ///
39 /// \param in_file The file to hash.
40 /// \param out_hash The hash of the file.
41 /// \param in_from_beginning Determines whether to seek to the beginning of the
42 /// stream before computing the hash. The original position
43 /// will be restored after completion.
44 ///
45 /// \returns `true` if the hash was computed successfully. Otherwise, `false`.
46 ///
47 template <hash::hash_provider_t T = hash::xxhash64_provider>
48 inline bool try_compute_hash(std::ifstream& in_file, typename T::type& out_hash, bool in_from_beginning = true, hash_progress_callback_t<typename T::type> in_callback = nullptr);
49
50 ///
51 /// Computes a file hash.
52 ///
53 /// \param in_hash_provider The provider of the hash for the file.
54 /// \param in_path The path to the file to hash.
55 /// \param out_hash The hash of the file.
56 ///
57 /// \returns `true` if the hash was computed successfully. Otherwise, `false`.
58 ///
59 template <typename T>
60 inline bool try_compute_hash(hash::hash_provider<T>& in_hash_provider, const std::filesystem::path& in_path, T& out_hash, hash_progress_callback_t<T> in_callback = nullptr);
61
62 ///
63 /// Computes a file hash.
64 ///
65 /// \tparam T The provider of the hash for the file. Uses xxHash64 by default.
66 ///
67 /// \param in_path The path to the file to hash.
68 /// \param out_hash The hash of the file.
69 ///
70 /// \returns `true` if the hash was computed successfully. Otherwise, `false`.
71 ///
72 template <hash::hash_provider_t T = hash::xxhash64_provider>
73 inline bool try_compute_hash(const std::filesystem::path& in_path, typename T::type& out_hash, hash_progress_callback_t<typename T::type> in_callback = nullptr);
74
75 ///
76 /// Compares the contents of two files.
77 ///
78 /// \tparam T The provider of the hashes for the files. Uses xxHash64 by default.
79 ///
80 /// \param in_left The first file.
81 /// \param in_right The second file.
82 /// \param in_from_beginning Determines whether to seek to the beginning of the
83 /// stream before computing the hash. The original position
84 /// will be restored after completion.
85 ///
86 /// \returns `true` if the files' contents are identical. Otherwise, `false`.
87 ///
88 template <hash::hash_provider_t T = hash::xxhash64_provider>
89 inline bool compare(std::ifstream& in_left, std::ifstream& in_right, bool in_from_beginning = true, hash_progress_callback_t<typename T::type> in_left_callback = nullptr, hash_progress_callback_t<typename T::type> in_right_callback = nullptr);
90
91 ///
92 /// Compares the contents of two files.
93 ///
94 /// \tparam T The provider of the hashes for the files. Uses xxHash64 by default.
95 ///
96 /// \param in_left The first file.
97 /// \param in_right The second file.
98 ///
99 /// \returns `true` if the files exist, and their paths or contents are identical. Otherwise, `false`.
100 ///
101 template <hash::hash_provider_t T = hash::xxhash64_provider>
102 inline bool compare(const std::filesystem::path& in_left, const std::filesystem::path& in_right, hash_progress_callback_t<typename T::type> in_left_callback = nullptr, hash_progress_callback_t<typename T::type> in_right_callback = nullptr);
103
104 ///
105 /// Gets the text encoding of a file heuristically.
106 ///
107 /// \param in_path The path to the file to check.
108 /// \param out_bom The byte order mark, if present.
109 /// \param in_native Determines whether to transform the encoding type to the
110 /// native endianness.
111 ///
112 /// \returns The encoding of the file.
113 ///
114 inline ut::encoding::encoding_type get_encoding(const std::filesystem::path& in_path, ut::encoding::encoding_type* out_bom = nullptr, bool in_native = false);
115
116 ///
117 /// Reads a text file into a string.
118 ///
119 /// \tparam T The string type. If this doesn't match the file encoding, the text
120 /// will be converted automatically.
121 ///
122 /// \param in_path The path to the file to read.
123 ///
124 /// \returns The text from the file in a new string.
125 ///
126 template <ut::expr::basic_string T>
127 inline T read_all_text(const std::filesystem::path& in_path);
128
129 ///
130 /// Writes a string to a file.
131 ///
132 /// \tparam T The string type.
133 /// \tparam target_encoding The encoding of the file inferred from the string type.
134 ///
135 /// \param in_path The path to the file to write.
136 /// \param in_str The string to write to the file.
137 /// \param in_write_bom Determines whether to write a byte order mark at the
138 /// beginning of the file. This behaviour is automatically
139 /// determined by the encoding type, but can be overridden
140 /// if needed. Avoid writing a byte order mark for UTF-8,
141 /// as it is unnecessary.
142 ///
143 /// \returns `true` if the file was written successfully. Otherwise, `false`.
144 ///
145 template <ut::expr::any_string T, ut::encoding::encoding_type target_encoding = ut::expr::get_char_encoding_type<T>()>
146 inline bool write_all_text(const std::filesystem::path& in_path, const T& in_str, bool in_write_bom = target_encoding != ut::encoding::utf8);
147
148 ///
149 /// Writes a string to a file.
150 ///
151 /// \tparam target_encoding The encoding of the file.
152 /// \tparam T The string type.
153 ///
154 /// \param in_path The path to the file to write.
155 /// \param in_str The string to write to the file.
156 /// \param in_write_bom Determines whether to write a byte order mark at the
157 /// beginning of the file. This behaviour is automatically
158 /// determined by the encoding type, but can be overridden
159 /// if needed. Avoid writing a byte order mark for UTF-8,
160 /// as it is unnecessary.
161 ///
162 /// \returns `true` if the file was written successfully. Otherwise, `false`.
163 ///
164 template <ut::encoding::encoding_type target_encoding, ut::expr::any_string T>
165 inline bool write_all_text(const std::filesystem::path& in_path, const T& in_str, bool in_write_bom = target_encoding != ut::encoding::utf8);
166}
167
168#include "file.inl"
Definition hash_provider.h:9
Definition xxhash64_provider.inl:4
bool write_all_text(const std::filesystem::path &in_path, const T &in_str, bool in_write_bom)
Definition file.inl:301
bool compare(std::ifstream &in_left, std::ifstream &in_right, bool in_from_beginning, hash_progress_callback_t< typename T::type > in_left_callback, hash_progress_callback_t< typename T::type > in_right_callback)
Definition file.inl:99
ut::encoding::encoding_type get_encoding(const std::filesystem::path &in_path, ut::encoding::encoding_type *out_bom, bool in_native)
Definition file.inl:213
bool try_compute_hash(hash::hash_provider< T > &in_hash_provider, std::ifstream &in_file, T &out_hash, bool in_from_beginning, hash_progress_callback_t< T > in_callback)
Definition file.inl:23
bool try_compute_hash(hash::hash_provider< T > &in_hash_provider, const std::filesystem::path &in_path, T &out_hash, hash_progress_callback_t< T > in_callback)
Definition file.inl:78
std::function< bool(T in_hash, uint64_t in_processed_bytes, uint64_t in_total_bytes)> hash_progress_callback_t
Definition file.h:17
bool try_compute_hash(const std::filesystem::path &in_path, typename T::type &out_hash, hash_progress_callback_t< typename T::type > in_callback)
Definition file.inl:91
T read_all_text(const std::filesystem::path &in_path)
Definition file.inl:249
bool try_compute_hash(std::ifstream &in_file, typename T::type &out_hash, bool in_from_beginning, hash_progress_callback_t< typename T::type > in_callback)
Definition file.inl:70
bool compare(const std::filesystem::path &in_left, const std::filesystem::path &in_right, hash_progress_callback_t< typename T::type > in_left_callback, hash_progress_callback_t< typename T::type > in_right_callback)
Definition file.inl:193
Definition file.inl:19
Definition encoding.h:10
encoding_type
Definition encoding_type.h:6
@ utf8
Definition encoding_type.h:8
Definition expr.h:7