#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <pslib.h>

#include "ppMerge.h"
#include "ppMergeData.h"

// Free function for ppMergeData
static void mergeDataFree(ppMergeData *data // Data to free
    )
{
    if (data->files) {
        for (long i = 0; i < data->files->n; i++) {
            psFitsClose(data->files->data[i]);
            data->files->data[i] = NULL;
        }
        psFree(data->files);
    }
    psFree(data->in);
    psFree(data->out);
    if (data->outFile) {
        psFitsClose(data->outFile);
        data->outFile = NULL;
    }
    if (data->statsFile) {
        fclose(data->statsFile);
        data->statsFile = NULL;
    }
    psFree(data->stats);
}

// Allocator for ppMergeData
ppMergeData *ppMergeDataAlloc(void)
{
    ppMergeData *data = psAlloc(sizeof(ppMergeData)); // The data, to return
    psMemSetDeallocator(data, (psFreeFunc)mergeDataFree);

    data->numCells = 0;
    data->files = NULL;
    data->in = NULL;
    data->out = NULL;
    data->outFile = NULL;
    data->stats = NULL;
    data->statsFile = NULL;

    return data;
}
