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

/*!
 * \cond sion_internal
 * \file
 *
 * Flags handling
 */

#ifndef SION_SION_FLAGS_H
#define SION_SION_FLAGS_H

#include <stdint.h>

struct _sion_flags_store_struct;
struct _sion_flags_entry_struct;

typedef struct _sion_flags_store_struct _sion_flags_store;
typedef struct _sion_flags_entry_struct _sion_flags_entry;

struct _sion_flags_store_struct {
  _sion_flags_entry *root;
  _sion_flags_entry *current;
  int64_t mask;
};

struct _sion_flags_entry_struct {
  char *key;
  char *val;
  _sion_flags_entry *next;
};

void _lsion_flags_init_entry(_sion_flags_entry *entry);

/*! \brief Create a flags entry
 *
 * Use _lsion_flags_destroy_entry to free the memory when not needed any longer.
 *
 * @return     flag entry
 */
_sion_flags_entry *_lsion_flags_create_entry(void);
void _lsion_flags_destroy_entry(_sion_flags_entry **entry);

_sion_flags_entry *_lsion_flags_iter(const _sion_flags_store *store);

void _lsion_flags_init_store(_sion_flags_store *store);

/*! \brief Create a flags entry
 *
 * Use _lsion_flags_destroy_store to free the memory when not needed any longer.
 *
 * @return     flag store with flag entries (key value pairs)
 */
_sion_flags_store *_lsion_flags_create_store(void);
void _lsion_flags_destroy_store(_sion_flags_store **store);

/*! \brief Create a flags entry
 *
 * Memory is deallocated in _lsion_flags_destroy_entry.
 *
 * @param[in,out]   entry   current flag entry to add key and value to
 * @param[in]       key     key
 * @param[in]       val     value
 *
 * @return     flag store with key value pairs
 */
_sion_flags_entry *_lsion_flags_add_entry(_sion_flags_entry *entry, const char *key, const char *val);
void _lsion_flags_add(_sion_flags_store *store, const char *key, const char *val);
_sion_flags_entry *_lsion_flags_get(const _sion_flags_store *store, const char *key);
int64_t _lsion_flags_update_mask(_sion_flags_store *store);

/*! \brief Parse flags and return a flags store with key value pairs
 *
 * @param[in]  flags   string containing the open flags
 *
 * @return     flag store with key value pairs
 */
_sion_flags_store *_lsion_parse_flags(const char *flags);

#endif

/// \endcond
