/****************************************************************************
**  SIONLIB     http://www.fz-juelich.de/jsc/sionlib                       **
*****************************************************************************
**  Copyright (c) 2008-2019                                                **
**  Forschungszentrum Juelich, Juelich Supercomputing Centre               **
**                                                                         **
**  See the file COPYRIGHT in the package base directory for details       **
****************************************************************************/

#define _XOPEN_SOURCE 700

#include <stdint.h>
#include <stdlib.h>

#include "sion_common.h"
#include "sion_debug.h"
#include "sion_enums.h"
#include "sion_internal.h"
#include "sion_serial.h"

sion_options *sion_options_new(void)
{
  sion_options *options = malloc(sizeof(sion_options));
  if (options) {
    *options = SION_OPTIONS_INIT;
  }
  return options;
}

void sion_options_delete(sion_options *options)
{
  free(options);
}

void sion_options_set_chunksizes(sion_options *options, int n, const int64_t chunksizes[])
{
  options->chunksizes = chunksizes;
  options->n_chunksizes = n;
}

void sion_options_set_fsblksize(sion_options *options, int32_t fsblksize)
{
  options->fsblksize = fsblksize;
}

void sion_options_set_keyval_mode(sion_options *options, sion_keyval_mode keyval_mode)
{
  options->keyval_mode = keyval_mode;
}

void sion_options_set_lowlevel_api(sion_options *options, sion_lowlevel_api lowlevel_api)
{
  options->lowlevel_api = lowlevel_api;
}

void sion_options_set_endianness(sion_options *options, sion_endianness endianness)
{
  options->endianness = endianness;
}

int sion_open(const char *fname, sion_open_mode mode, int n, const sion_options *options_)
{
  sion_options options = (options_) ? *options_ : SION_OPTIONS_INIT;

  // FIXME re-enable debug output of file_mode
  // DPRINTFP((1, __func__, 0, "enter open of file %s in %s mode\n", fname, file_mode));
  DPRINTFP((1, __func__, 0, "enter open of file %s\n", fname));
  int sid = (mode == SION_OPEN_WRITE) ? _sion_open_write(fname, n, &options)
                                      : _sion_open_read(fname, _SION_READ_ALL_OF_MULTI_FILES, &options);

  // FIXME re-enable debug output of file_mode
  // DPRINTFP((1, __func__, 0, "leave open of file %s in %s mode sid=%d\n", fname, file_mode,sid));
  DPRINTFP((1, __func__, 0, "leave open of file %s sid=%d\n", fname, sid));

  return sid;
}

sion_rank_options *sion_rank_options_new(void)
{
  sion_rank_options *options = malloc(sizeof(sion_rank_options));
  if (options) {
    *options = SION_RANK_OPTIONS_INIT;
  }
  return options;
}

void sion_rank_options_delete(sion_rank_options *options)
{
  free(options);
}

void sion_rank_options_set_lowlevel_api(sion_rank_options *options, sion_lowlevel_api lowlevel_api)
{
  options->lowlevel_api = lowlevel_api;
}

int sion_open_rank(const char *fname, sion_open_mode mode, int rank, const sion_rank_options *options)
{
  return _sion_open_rank(fname, mode, rank, (options) ? options : &SION_RANK_OPTIONS_INIT);
}

int sion_close(int sid)
{
  int rc = 0;
  DPRINTFP((1, "sion_close", -1, "enter close   sid=%d\n", sid));
  rc = _sion_close_sid(sid);
  DPRINTFP((1, "sion_close", -1, "leave close   sid=%d\n", sid));

  sion_dclose();

  return rc;
}
