# include "ppStatsInternal.h"

// calculate the stats for the non-constant entries (already calculated)
bool ppStatsFromMetadataPrint (psArray *entries, char *filename) {

    FILE *f = NULL;
    if (!strcmp (filename, "-")) {
	f = stdout;
    } else {
	f = fopen (filename, "w");
	if (f == NULL) {
	    psError(PS_ERR_UNKNOWN, false, "ppStatsFromMetadata cannot open output file %s\n", filename);
	    return false;
	}
    }

    // at this point, we have entries with values (of type STR or F32) or NULL
    for (int i = 0; i < entries->n; i++) {
	ppStatsEntry *entry = entries->data[i];

	if (!entry->value) continue;

	if (entry->value->type == PS_DATA_STRING) {
	    fprintf (f, "%s %s ", entry->flag, entry->value->data.str);
	}

	if (entry->value->type == PS_DATA_F32) {
	    fprintf (f, "%s %f ", entry->flag, entry->value->data.F32);
	}
    }
    fprintf (f, "\n");

    if (f != stdout) fclose (f);
    return true;
}
