ppcondition *conditions;
ppmacro **macros;
- bool output_string;
+ bool to_string;
char *output;
+ FILE *output_file;
} ftepp_t;
#define ftepp_tokval(f) ((f)->lex->tok.value)
ppmacro_delete(self->macros[i]);
vec_free(self->macros);
vec_free(self->conditions);
- lex_close(self->lex);
+ if (self->lex)
+ lex_close(self->lex);
+ if (self->output_file)
+ fclose(self->output_file);
mem_d(self);
}
{
size_t len;
char *data;
- if (!ftepp->output_string) {
- printf("%s", str);
+ if (!ftepp->to_string) {
+ fprintf((ftepp->output_file ? ftepp->output_file : stdout), "%s", str);
return;
}
len = strlen(str);
static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params)
{
char *old_string = ftepp->output;
- bool old_string_flag = ftepp->output_string;
+ bool old_string_flag = ftepp->to_string;
lex_file *old_lexer = ftepp->lex;
bool retval = true;
if (!vec_size(macro->output))
return true;
- ftepp->output = NULL;
- ftepp->output_string = true;
+ ftepp->output = NULL;
+ ftepp->to_string = true;
for (o = 0; o < vec_size(macro->output); ++o) {
pptoken *out = macro->output[o];
switch (out->token) {
retval = false;
goto cleanup;
}
- ftepp->output = old_string;
- ftepp->output_string = old_string_flag;
+ ftepp->output = old_string;
+ ftepp->to_string = old_string_flag;
ftepp->lex = inlex;
if (!ftepp_preprocess(ftepp)) {
lex_close(ftepp->lex);
}
cleanup:
- ftepp->lex = old_lexer;
- ftepp->output = old_string;
- ftepp->output_string = old_string_flag;
+ ftepp->lex = old_lexer;
+ ftepp->output = old_string;
+ ftepp->to_string = old_string_flag;
return retval;
}
return ftepp_preprocess_done();
}
-bool ftepp_init()
+bool ftepp_init(FILE *out)
{
ftepp = ftepp_new();
+ ftepp->output_file = out;
return !!ftepp;
}
void ftepp_finish()
{
+ if (!ftepp)
+ return;
ftepp_delete(ftepp);
+ ftepp = NULL;
}