address gbnf-validator unused fread warning (switched to C++ / ifstream)

This commit is contained in:
Olivier Chafik 2024-06-10 17:38:36 +01:00
parent b8436395b4
commit f9cfd04bd4
2 changed files with 18 additions and 19 deletions

View file

@ -12,6 +12,7 @@ BUILD_TARGETS = \
llama-eval-callback \ llama-eval-callback \
llama-export-lora \ llama-export-lora \
llama-finetune \ llama-finetune \
llama-gbnf-validator \
llama-gguf \ llama-gguf \
llama-gguf-split \ llama-gguf-split \
llama-gritlm \ llama-gritlm \

View file

@ -7,6 +7,8 @@
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <sstream>
#include <fstream>
#include <string> #include <string>
#include <vector> #include <vector>
@ -69,13 +71,14 @@ int main(int argc, char** argv) {
return 1; return 1;
} }
fseek(grammar_file, 0, SEEK_END); std::string grammar_str;
size_t grammar_size = ftell(grammar_file); {
fseek(grammar_file, 0, SEEK_SET); std::ifstream grammar_file(grammar_filename);
GGML_ASSERT(grammar_file.is_open() && "Failed to open grammar file");
std::string grammar_str(grammar_size, ' '); std::stringstream buffer;
fread(&grammar_str[0], 1, grammar_size, grammar_file); buffer << grammar_file.rdbuf();
fclose(grammar_file); grammar_str = buffer.str();
}
// Parse the GBNF grammar // Parse the GBNF grammar
auto parsed_grammar = grammar_parser::parse(grammar_str.c_str()); auto parsed_grammar = grammar_parser::parse(grammar_str.c_str());
@ -100,20 +103,15 @@ int main(int argc, char** argv) {
grammar_rules.size(), parsed_grammar.symbol_ids.at("root")); grammar_rules.size(), parsed_grammar.symbol_ids.at("root"));
// Read the input file // Read the input file
FILE* input_file = fopen(input_filename.c_str(), "r"); std::string input_str;
if (!input_file) { {
fprintf(stdout, "Failed to open input file: %s\n", input_filename.c_str()); std::ifstream input_file(input_filename);
return 1; GGML_ASSERT(input_file.is_open() && "Failed to open input file");
std::stringstream buffer;
buffer << input_file.rdbuf();
input_str = buffer.str();
} }
fseek(input_file, 0, SEEK_END);
size_t input_size = ftell(input_file);
fseek(input_file, 0, SEEK_SET);
std::string input_str(input_size, ' ');
fread(&input_str[0], 1, input_size, input_file);
fclose(input_file);
// Validate the input string against the grammar // Validate the input string against the grammar
size_t error_pos; size_t error_pos;
std::string error_msg; std::string error_msg;