CommonLib
Loading...
Searching...
No Matches
hedgedev::csl::io::file Namespace Reference

Typedefs

template<typename T>
using hash_progress_callback_t = std::function<bool(T in_hash, uint64_t in_processed_bytes, uint64_t in_total_bytes)>

Functions

template<typename T>
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)
template<hash::hash_provider_t T>
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)
template<typename T>
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)
template<hash::hash_provider_t T>
bool try_compute_hash (const std::filesystem::path &in_path, typename T::type &out_hash, hash_progress_callback_t< typename T::type > in_callback)
template<hash::hash_provider_t T>
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)
template<hash::hash_provider_t T>
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)
ut::encoding::encoding_type get_encoding (const std::filesystem::path &in_path, ut::encoding::encoding_type *out_bom, bool in_native)
template<ut::expr::basic_string T>
T read_all_text (const std::filesystem::path &in_path)
template<ut::expr::any_string T, ut::encoding::encoding_type target_encoding>
bool write_all_text (const std::filesystem::path &in_path, const T &in_str, bool in_write_bom)
template<ut::encoding::encoding_type target_encoding, ut::expr::any_string T>
bool write_all_text (const std::filesystem::path &in_path, const T &in_str, bool in_write_bom)

Variables

constexpr uint64_t k_optimal_buffer_size = 0x10000

Typedef Documentation

◆ hash_progress_callback_t

template<typename T>
using hedgedev::csl::io::file::hash_progress_callback_t = std::function<bool(T in_hash, uint64_t in_processed_bytes, uint64_t in_total_bytes)>

Function Documentation

◆ compare() [1/2]

template<hash::hash_provider_t T>
bool hedgedev::csl::io::file::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 )
inline

Compares the contents of two files.

Template Parameters
TThe provider of the hashes for the files. Uses xxHash64 by default.
Parameters
in_leftThe first file.
in_rightThe second file.
Returns
true if the files exist, and their paths or contents are identical. Otherwise, false.

◆ compare() [2/2]

template<hash::hash_provider_t T>
bool hedgedev::csl::io::file::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 )
inline

Compares the contents of two files.

Template Parameters
TThe provider of the hashes for the files. Uses xxHash64 by default.
Parameters
in_leftThe first file.
in_rightThe second file.
in_from_beginningDetermines whether to seek to the beginning of the stream before computing the hash. The original position will be restored after completion.
Returns
true if the files' contents are identical. Otherwise, false.

◆ get_encoding()

ut::encoding::encoding_type hedgedev::csl::io::file::get_encoding ( const std::filesystem::path & in_path,
ut::encoding::encoding_type * out_bom = nullptr,
bool in_native = false )
inline

Gets the text encoding of a file heuristically.

Parameters
in_pathThe path to the file to check.
out_bomThe byte order mark, if present.
in_nativeDetermines whether to transform the encoding type to the native endianness.
Returns
The encoding of the file.

◆ read_all_text()

template<ut::expr::basic_string T>
T hedgedev::csl::io::file::read_all_text ( const std::filesystem::path & in_path)
inline

Reads a text file into a string.

Template Parameters
TThe string type. If this doesn't match the file encoding, the text will be converted automatically.
Parameters
in_pathThe path to the file to read.
Returns
The text from the file in a new string.

◆ try_compute_hash() [1/4]

template<hash::hash_provider_t T>
bool hedgedev::csl::io::file::try_compute_hash ( const std::filesystem::path & in_path,
typename T::type & out_hash,
hash_progress_callback_t< typename T::type > in_callback = nullptr )
inline

Computes a file hash.

Template Parameters
TThe provider of the hash for the file. Uses xxHash64 by default.
Parameters
in_pathThe path to the file to hash.
out_hashThe hash of the file.
Returns
true if the hash was computed successfully. Otherwise, false.

◆ try_compute_hash() [2/4]

template<typename T>
bool hedgedev::csl::io::file::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 )
inline

Computes a file hash.

Parameters
in_hash_providerThe provider of the hash for the file.
in_pathThe path to the file to hash.
out_hashThe hash of the file.
Returns
true if the hash was computed successfully. Otherwise, false.

◆ try_compute_hash() [3/4]

template<typename T>
bool hedgedev::csl::io::file::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 )
inline

Computes a file hash.

Parameters
in_hash_providerThe provider of the hash for the file.
in_pathThe file to hash.
out_hashThe hash of the file.
in_from_beginningDetermines whether to seek to the beginning of the stream before computing the hash. The original position will be restored after completion.
Returns
true if the hash was computed successfully. Otherwise, false.

◆ try_compute_hash() [4/4]

template<hash::hash_provider_t T>
bool hedgedev::csl::io::file::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 )
inline

Computes a file hash.

Template Parameters
TThe provider of the hash for the file. Uses xxHash64 by default.
Parameters
in_fileThe file to hash.
out_hashThe hash of the file.
in_from_beginningDetermines whether to seek to the beginning of the stream before computing the hash. The original position will be restored after completion.
Returns
true if the hash was computed successfully. Otherwise, false.

◆ write_all_text() [1/2]

template<ut::encoding::encoding_type target_encoding, ut::expr::any_string T>
bool hedgedev::csl::io::file::write_all_text ( const std::filesystem::path & in_path,
const T & in_str,
bool in_write_bom = target_encoding !=ut::encoding::utf8 )
inline

Writes a string to a file.

Template Parameters
target_encodingThe encoding of the file.
TThe string type.
Parameters
in_pathThe path to the file to write.
in_strThe string to write to the file.
in_write_bomDetermines whether to write a byte order mark at the beginning of the file. This behaviour is automatically determined by the encoding type, but can be overridden if needed. Avoid writing a byte order mark for UTF-8, as it is unnecessary.
Returns
true if the file was written successfully. Otherwise, false.

◆ write_all_text() [2/2]

template<ut::expr::any_string T, ut::encoding::encoding_type target_encoding>
bool hedgedev::csl::io::file::write_all_text ( const std::filesystem::path & in_path,
const T & in_str,
bool in_write_bom = target_encoding !=ut::encoding::utf8 )
inline

Writes a string to a file.

Template Parameters
TThe string type.
target_encodingThe encoding of the file inferred from the string type.
Parameters
in_pathThe path to the file to write.
in_strThe string to write to the file.
in_write_bomDetermines whether to write a byte order mark at the beginning of the file. This behaviour is automatically determined by the encoding type, but can be overridden if needed. Avoid writing a byte order mark for UTF-8, as it is unnecessary.
Returns
true if the file was written successfully. Otherwise, false.

Writes a string to a file.

Template Parameters
target_encodingThe encoding of the file.
TThe string type.
Parameters
in_pathThe path to the file to write.
in_strThe string to write to the file.
in_write_bomDetermines whether to write a byte order mark at the beginning of the file. This behaviour is automatically determined by the encoding type, but can be overridden if needed. Avoid writing a byte order mark for UTF-8, as it is unnecessary.
Returns
true if the file was written successfully. Otherwise, false.

Variable Documentation

◆ k_optimal_buffer_size

uint64_t hedgedev::csl::io::file::k_optimal_buffer_size = 0x10000
inlineconstexpr