gguf : roper closing of file

This commit is contained in:
M. Yusuf Sarıgöz 2023-08-12 21:42:31 +03:00
parent 202eab04d3
commit 60d540831b

View File

@ -105,7 +105,6 @@ struct gguf_file {
GGML_ASSERT(ret == 0); // same
}
size_t write_str(const std::string & val) {
size_t total_written = 0;
const int32_t n = val.size();
@ -127,14 +126,6 @@ struct gguf_file {
return sizeof(val);
}
void write_raw(const void * data, size_t size) {
fwrite(data, size, 1, fp);
}
void read_raw(void * data, size_t size) {
fread(data, size, 1, fp);
}
template<typename T>
void write_val(const std::string & key, enum gguf_type type, const T & val) {
write_str(key);
@ -155,6 +146,7 @@ struct gguf_file {
fwrite((const char *) &n, sizeof(n), 1, fp);
fwrite(val.data(), sizeof(T), n, fp);
}
template<>
void write_val<std::string>(const std::string & key, enum gguf_type type, const std::string & val) {
write_str(key);
@ -188,6 +180,37 @@ struct gguf_file {
fputc(0, fp);
}
}
void read_raw(void * ptr, size_t len) const {
if (len == 0) {
return;
}
errno = 0;
std::size_t ret = std::fread(ptr, len, 1, fp);
if (ferror(fp)) {
throw std::runtime_error(format("read error: %s", strerror(errno)));
}
if (ret != 1) {
throw std::runtime_error(std::string("unexpectedly reached end of file"));
}
}
void write_raw(const void * ptr, size_t len) const {
if (len == 0) {
return;
}
errno = 0;
size_t ret = std::fwrite(ptr, len, 1, fp);
if (ret != 1) {
throw std::runtime_error(format("write error: %s", strerror(errno)));
}
}
~gguf_file() {
if (fp) {
std::fclose(fp);
}
}
};
#if defined(_WIN32)