mirror of
https://github.com/fmang/opustags.git
synced 2025-02-14 02:58:59 +01:00
Compare commits
2 Commits
d9b051210b
...
a54bac8f55
Author | SHA1 | Date | |
---|---|---|---|
|
a54bac8f55 | ||
|
3293647e8f |
@ -119,13 +119,16 @@ using byte_string_view = std::basic_string_view<uint8_t>;
|
||||
* \{
|
||||
*/
|
||||
|
||||
/** fclose wrapper for std::unique_ptr’s deleter. */
|
||||
void close_file(FILE*);
|
||||
|
||||
/**
|
||||
* Smart auto-closing FILE* handle.
|
||||
*
|
||||
* It implictly converts from an already opened FILE*.
|
||||
*/
|
||||
struct file : std::unique_ptr<FILE, decltype(&fclose)> {
|
||||
file(FILE* f = nullptr) : std::unique_ptr<FILE, decltype(&fclose)>(f, &fclose) {}
|
||||
struct file : std::unique_ptr<FILE, decltype(&close_file)> {
|
||||
file(FILE* f = nullptr) : std::unique_ptr<FILE, decltype(&close_file)>(f, &close_file) {}
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -29,6 +29,11 @@ ot::byte_string_view operator""_bsv(const char* data, size_t size)
|
||||
return ot::byte_string_view(reinterpret_cast<const uint8_t*>(data), size);
|
||||
}
|
||||
|
||||
void ot::close_file(FILE* file)
|
||||
{
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
void ot::partial_file::open(const char* destination)
|
||||
{
|
||||
final_name = destination;
|
||||
@ -122,7 +127,7 @@ ot::byte_string ot::slurp_binary_file(const char* filename)
|
||||
|
||||
byte_string content;
|
||||
long file_size = get_file_size(f.get());
|
||||
if (file_size == -1) {
|
||||
if (file_size < 0) {
|
||||
// Read the input stream block by block and resize the output byte string as needed.
|
||||
uint8_t buffer[4096];
|
||||
while (!feof(f.get())) {
|
||||
@ -135,7 +140,7 @@ ot::byte_string ot::slurp_binary_file(const char* filename)
|
||||
} else {
|
||||
// Lucky! We know the file size, so let’s slurp it at once.
|
||||
content.resize(file_size);
|
||||
if (fread(content.data(), 1, file_size, f.get()) < file_size)
|
||||
if (fread(content.data(), 1, file_size, f.get()) < size_t(file_size))
|
||||
throw status { st::standard_error,
|
||||
"Could not read '"s + filename + "': " + strerror(errno) + "." };
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user