mirror of
https://github.com/ggerganov/llama.cpp.git
synced 2025-01-08 11:46:53 +01:00
gguf : roper closing of file
This commit is contained in:
parent
202eab04d3
commit
60d540831b
41
gguf-util.h
41
gguf-util.h
@ -105,7 +105,6 @@ struct gguf_file {
|
|||||||
GGML_ASSERT(ret == 0); // same
|
GGML_ASSERT(ret == 0); // same
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
size_t write_str(const std::string & val) {
|
size_t write_str(const std::string & val) {
|
||||||
size_t total_written = 0;
|
size_t total_written = 0;
|
||||||
const int32_t n = val.size();
|
const int32_t n = val.size();
|
||||||
@ -127,14 +126,6 @@ struct gguf_file {
|
|||||||
return sizeof(val);
|
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>
|
template<typename T>
|
||||||
void write_val(const std::string & key, enum gguf_type type, const T & val) {
|
void write_val(const std::string & key, enum gguf_type type, const T & val) {
|
||||||
write_str(key);
|
write_str(key);
|
||||||
@ -155,6 +146,7 @@ struct gguf_file {
|
|||||||
fwrite((const char *) &n, sizeof(n), 1, fp);
|
fwrite((const char *) &n, sizeof(n), 1, fp);
|
||||||
fwrite(val.data(), sizeof(T), n, fp);
|
fwrite(val.data(), sizeof(T), n, fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
void write_val<std::string>(const std::string & key, enum gguf_type type, const std::string & val) {
|
void write_val<std::string>(const std::string & key, enum gguf_type type, const std::string & val) {
|
||||||
write_str(key);
|
write_str(key);
|
||||||
@ -188,6 +180,37 @@ struct gguf_file {
|
|||||||
fputc(0, fp);
|
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)
|
#if defined(_WIN32)
|
||||||
|
Loading…
Reference in New Issue
Block a user