7
7
8
8
#include < cstdio>
9
9
#include < cstdlib>
10
+ #include < sstream>
11
+ #include < fstream>
10
12
#include < string>
11
13
#include < vector>
12
14
@@ -69,13 +71,14 @@ int main(int argc, char** argv) {
69
71
return 1 ;
70
72
}
71
73
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
+ }
79
82
80
83
// Parse the GBNF grammar
81
84
auto parsed_grammar = grammar_parser::parse (grammar_str.c_str ());
@@ -100,20 +103,15 @@ int main(int argc, char** argv) {
100
103
grammar_rules.size (), parsed_grammar.symbol_ids .at (" root" ));
101
104
102
105
// 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 ();
107
113
}
108
114
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
-
117
115
// Validate the input string against the grammar
118
116
size_t error_pos;
119
117
std::string error_msg;
0 commit comments