Add function to print to file.

This commit is contained in:
Danila Fedorin 2018-02-26 18:14:31 -08:00
parent 2823b0b66b
commit 08513a2315
2 changed files with 17 additions and 11 deletions

View File

@ -2,7 +2,9 @@
#define LIBABACUS_DEBUG_H
#include "tree.h"
#include <stdio.h>
void libab_debug_fprint_tree(libab_tree* print, FILE* file);
void libab_debug_print_tree(libab_tree* print);
#endif

View File

@ -1,5 +1,4 @@
#include "debug.h"
#include <stdio.h>
int _debug_foreach_print_tree(void* data, va_list args);
@ -19,30 +18,35 @@ const char* _debug_node_name(libab_tree_variant var) {
return names[var];
}
void _debug_print_tree_node(libab_tree* tree) {
printf("%s", _debug_node_name(tree->variant));
void _debug_print_tree_node(libab_tree* tree, FILE* file) {
fprintf(file, "%s", _debug_node_name(tree->variant));
if(libab_tree_has_string(tree->variant)) {
printf(": %s", tree->string_value);
fprintf(file, ": %s", tree->string_value);
}
printf("\n");
fprintf(file, "\n");
}
void _debug_print_tree(libab_tree* tree, int depth) {
void _debug_print_tree(libab_tree* tree, FILE* file, int depth) {
int i = depth;
while(i--) printf(" ");
_debug_print_tree_node(tree);
_debug_print_tree_node(tree, file);
if(libab_tree_has_vector(tree->variant)) {
vec_foreach(&tree->children, NULL, compare_always,
_debug_foreach_print_tree, depth + 1);
_debug_foreach_print_tree, file, depth + 1);
}
}
int _debug_foreach_print_tree(void* data, va_list args) {
FILE* file = va_arg(args, FILE*);
int depth = va_arg(args, int);
_debug_print_tree(data, depth);
_debug_print_tree(data, file, depth);
return 0;
}
void libab_debug_print_tree(libab_tree* print) {
_debug_print_tree(print, 0);
void libab_debug_fprint_tree(libab_tree* print, FILE* file) {
_debug_print_tree(print, file, 0);
}
void libab_debug_print_tree(libab_tree* print) {
_debug_print_tree(print, stdout, 0);
}