<commit_before>#include <stdio.h>
#include <stdlib.h>
#include "NHparser.h"

static void
write_tree_r (NHNode *n, FILE *out);

static void
write_tree (NHNode *root, char *file);

extern int NH_12123_debug;

int
main (int argc, char *args[]) {
    NHNode *root;
    FILE *in, *err;

    if (argc != 2) {
        fprintf(stderr, "Usage: %s tree-file\n", args[0]);
        exit(1);
    } 
    
    in = fopen(args[1], "r");
    err = stderr;

    NH_12123_debug = 1;
    root = NH_read_tree(in, err, args[1]);
    write_tree(root, NULL);
    printf("\n");
    //    printf("%d, %d\n", NH_get_lineno(), NH_get_colno());
    return 0;
}

void
write_tree_r (NHNode *n, FILE *out) {
    NHNodeList *l;
    
    if (n == NULL) {
        return;
    }

    if (n->children == NULL) {
        fprintf(out, "%s", n->label);
        if (n->branchLength >= 0) {
            fprintf(out, ":%g", n->branchLength);
        }
        if (n->annotation) {
            fprintf(out, "%s", n->annotation->str);
        }
    } else {
        fprintf(out, "(");
        for (l = n->children; l; l = l->next) {
            write_tree_r(l->node, out);
            if (l->next)
                fprintf(out, ",");
        }
        fprintf(out, ")");
        if (n->label) 
            fprintf(out, "%s", n->label);
        if (n->branchLength >= 0) {
            fprintf(out, ":%g", n->branchLength);
        }
        if (n->annotation) {
            fprintf(out, "%s", n->annotation->str);
        }
    }
}

void
write_tree (NHNode *root, char *file) {
    FILE *out;

    out = file ? fopen(file, "w") : stdout;
    if (out == NULL) {
        fprintf(stderr, "The file %s could not be opened for writing\n", file);
    }
    
    if (root == NULL) {
        if (file) 
            fclose(out);
        return;
    }
    
    if (file)
        fclose(out);
    write_tree_r(root, out);
    fprintf(out, ";");
}<commit_msg>Minor bugfix: in function write_tree, the output stream is now closed _after_ output has been written to it.<commit_after>#include <stdio.h>
#include <stdlib.h>
#include "NHparser.h"

static void
write_tree_r (NHNode *n, FILE *out);

static void
write_tree (NHNode *root, char *file);

extern int NH_12123_debug;

int
main (int argc, char *args[]) {
    NHNode *root;
    FILE *in, *err;

    if (argc != 2) {
        fprintf(stderr, "Usage: %s tree-file\n", args[0]);
        exit(1);
    } 
    
    in = fopen(args[1], "r");
    err = stderr;

    NH_12123_debug = 1;
    root = NH_read_tree(in, err, args[1]);
    write_tree(root, NULL);
    printf("\n");
    //    printf("%d, %d\n", NH_get_lineno(), NH_get_colno());
    return 0;
}

void
write_tree_r (NHNode *n, FILE *out) {
    NHNodeList *l;
    
    if (n == NULL) {
        return;
    }

    if (n->children == NULL) {
        fprintf(out, "%s", n->label);
        if (n->branchLength >= 0) {
            fprintf(out, ":%g", n->branchLength);
        }
        if (n->annotation) {
            fprintf(out, "%s", n->annotation->str);
        }
    } else {
        fprintf(out, "(");
        for (l = n->children; l; l = l->next) {
            write_tree_r(l->node, out);
            if (l->next)
                fprintf(out, ",");
        }
        fprintf(out, ")");
        if (n->label) 
            fprintf(out, "%s", n->label);
        if (n->branchLength >= 0) {
            fprintf(out, ":%g", n->branchLength);
        }
        if (n->annotation) {
            fprintf(out, "%s", n->annotation->str);
        }
    }
}

void
write_tree (NHNode *root, char *file) {
    FILE *out;

    out = file ? fopen(file, "w") : stdout;
    if (out == NULL) {
        fprintf(stderr, "The file %s could not be opened for writing\n", file);
    }
    
    if (root == NULL) {
        if (file) 
            fclose(out);
        return;
    }
    
    write_tree_r(root, out);
    fprintf(out, ";");
    if (file)
        fclose(out);
}
{'fraction_non_alphanumeric': 0.17586649550706032, 'fraction_numerical': 0.00975609756097561, 'mean_word_length': 1.9009679821295606, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '161757', 'n_tokens_mistral': 1454, 'n_tokens_neox': 1410, 'n_words': 341}