Skip to content

Commit f9cfd04

Browse files
author
Olivier Chafik
committed
address gbnf-validator unused fread warning (switched to C++ / ifstream)
1 parent b843639 commit f9cfd04

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ BUILD_TARGETS = \
1212
llama-eval-callback \
1313
llama-export-lora \
1414
llama-finetune \
15+
llama-gbnf-validator \
1516
llama-gguf \
1617
llama-gguf-split \
1718
llama-gritlm \

examples/gbnf-validator/gbnf-validator.cpp

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#include <cstdio>
99
#include <cstdlib>
10+
#include <sstream>
11+
#include <fstream>
1012
#include <string>
1113
#include <vector>
1214

@@ -69,13 +71,14 @@ int main(int argc, char** argv) {
6971
return 1;
7072
}
7173

72-
fseek(grammar_file, 0, SEEK_END);
73-
size_t grammar_size = ftell(grammar_file);
74-
fseek(grammar_file, 0, SEEK_SET);
75-
76-
std::string grammar_str(grammar_size, ' ');
77-
fread(&grammar_str[0], 1, grammar_size, grammar_file);
78-
fclose(grammar_file);
74+
std::string grammar_str;
75+
{
76+
std::ifstream grammar_file(grammar_filename);
77+
GGML_ASSERT(grammar_file.is_open() && "Failed to open grammar file");
78+
std::stringstream buffer;
79+
buffer << grammar_file.rdbuf();
80+
grammar_str = buffer.str();
81+
}
7982

8083
// Parse the GBNF grammar
8184
auto parsed_grammar = grammar_parser::parse(grammar_str.c_str());
@@ -100,20 +103,15 @@ int main(int argc, char** argv) {
100103
grammar_rules.size(), parsed_grammar.symbol_ids.at("root"));
101104

102105
// Read the input file
103-
FILE* input_file = fopen(input_filename.c_str(), "r");
104-
if (!input_file) {
105-
fprintf(stdout, "Failed to open input file: %s\n", input_filename.c_str());
106-
return 1;
106+
std::string input_str;
107+
{
108+
std::ifstream input_file(input_filename);
109+
GGML_ASSERT(input_file.is_open() && "Failed to open input file");
110+
std::stringstream buffer;
111+
buffer << input_file.rdbuf();
112+
input_str = buffer.str();
107113
}
108114

109-
fseek(input_file, 0, SEEK_END);
110-
size_t input_size = ftell(input_file);
111-
fseek(input_file, 0, SEEK_SET);
112-
113-
std::string input_str(input_size, ' ');
114-
fread(&input_str[0], 1, input_size, input_file);
115-
fclose(input_file);
116-
117115
// Validate the input string against the grammar
118116
size_t error_pos;
119117
std::string error_msg;

0 commit comments

Comments
 (0)