};
GGML_API struct gguf_context * gguf_init_empty(void);
+ GGML_API struct gguf_context * gguf_init_from_file_ptr(FILE * file, struct gguf_init_params params);
GGML_API struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_params params);
//GGML_API struct gguf_context * gguf_init_from_buffer(..);
//
// write the entire context to a binary file
+ GGML_API bool gguf_write_to_file_ptr(const struct gguf_context * ctx, FILE * file, bool only_meta);
GGML_API bool gguf_write_to_file(const struct gguf_context * ctx, const char * fname, bool only_meta);
// get the size in bytes of the meta data (header, kv pairs, tensor info) including padding
// expose GGUF internals for test code
GGML_API size_t gguf_type_size(enum gguf_type type);
-GGML_API struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_params params);
GGML_API void gguf_write_to_buf(const struct gguf_context * ctx, std::vector<int8_t> & buf, bool only_meta);
#endif // __cplusplus
return true;
}
-struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_params params) {
+struct gguf_context * gguf_init_from_file_ptr(FILE * file, struct gguf_init_params params) {
+ if (!file) {
+ return nullptr;
+ }
+
const struct gguf_reader gr(file);
struct gguf_context * ctx = new gguf_context;
return nullptr;
}
- struct gguf_context * result = gguf_init_from_file_impl(file, params);
+ struct gguf_context * result = gguf_init_from_file_ptr(file, params);
fclose(file);
return result;
}
gguf_write_out(ctx, gw, only_meta);
}
+bool gguf_write_to_file_ptr(const struct gguf_context * ctx, FILE * file, bool only_meta) {
+ GGML_ASSERT(file);
+
+ try {
+ gguf_writer_file gw(file);
+ gguf_write_out(ctx, gw, only_meta);
+ } catch (const std::runtime_error& ex) {
+ GGML_LOG_ERROR("%s: failed to write GGUF data: %s\n", __func__, ex.what());
+ return false;
+ }
+ return true;
+}
+
bool gguf_write_to_file(const struct gguf_context * ctx, const char * fname, bool only_meta) {
FILE * file = ggml_fopen(fname, "wb");
return false;
}
- try {
- gguf_writer_file gw(file);
- gguf_write_out(ctx, gw, only_meta);
- } catch (const std::runtime_error& ex) {
- GGML_LOG_ERROR("%s: failed to write GGUF data into '%s': %s\n", __func__, fname, ex.what());
- fclose(file);
- return false;
+ const bool success = gguf_write_to_file_ptr(ctx, file, only_meta);
+ if (!success) {
+ GGML_LOG_ERROR("%s: failed to write GGUF data into '%s'\n", __func__, fname);
}
fclose(file);
- return true;
+ return success;
}
size_t gguf_get_meta_size(const struct gguf_context * ctx) {