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

#include <pslib.h>
#include "ppStackLoop.h"

static void stackOptionsFree(ppStackOptions *options)
{
    psFree(options->stats);
    if (options->statsFile) {
        fclose(options->statsFile);
    }
    psFree(options->imageNames);
    psFree(options->maskNames);
    psFree(options->varianceNames);
    psFree(options->psf);
    psFree(options->inputMask);
    psFree(options->sourceLists);
    psFree(options->cells);
    psFree(options->subKernels);
    psFree(options->subRegions);
    psFree(options->matchChi2);
    psFree(options->weightings);
    psFree(options->covariances);
    psFree(options->outRO);
    psFree(options->inspect);
    psFree(options->rejected);
    return;
}

ppStackOptions *ppStackOptionsAlloc(void)
{
    ppStackOptions *options = psAlloc(sizeof(ppStackOptions)); // Options, to return
    psMemSetDeallocator(options, (psFreeFunc)stackOptionsFree);

    options->stats = NULL;
    options->statsFile = NULL;
    options->imageNames = NULL;
    options->maskNames = NULL;
    options->varianceNames = NULL;
    options->num = 0;
    options->psf = NULL;
    options->inputMask = NULL;
    options->sourceLists = NULL;
    options->cells = NULL;
    options->subKernels = NULL;
    options->subRegions = NULL;
    options->numCols = 0;
    options->numRows = 0;
    options->matchChi2 = NULL;
    options->weightings = NULL;
    options->covariances = NULL;
    options->outRO = NULL;
    options->inspect = NULL;
    options->rejected = NULL;

    return options;
}
