Compare commits

...

2 Commits

Author SHA1 Message Date
Frédéric Mangano
a54bac8f55 Fix the warning on comparison of size_t and long 2024-11-01 10:30:12 +09:00
Frédéric Mangano
3293647e8f Wrap fclose to avoid compiler warnings 2024-11-01 10:20:49 +09:00
2 changed files with 12 additions and 4 deletions

View File

@ -119,13 +119,16 @@ using byte_string_view = std::basic_string_view<uint8_t>;
* \{
*/
/** fclose wrapper for std::unique_ptrs 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) {}
};
/**

View 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 lets 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) + "." };
}