/****************************************************************************
**  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       **
****************************************************************************/

/*!
 * \cond sion_internal
 * \file
 */

#ifndef SION_SION_SEEK_H
#define SION_SION_SEEK_H

#include <stdint.h>

#include "sion_enums.h"
#include "sion_filedesc.h"

/*!
 * @brief Function to set the file pointer to a new position, updates internal data structure.
 *
 * This function works on a SINGLE SION file
 *
 * @param[in,out]  sion_filedesc  sion data structure
 * @param[in]      rank           rank number of the process (SION_CURRENT_RANK to select the current rank)
 *
 * @retval      1 if file pointer can be moved to new position
 *              0 otherwise
 */
int _sion_switch_logical_file_read(_sion_filedesc *sion_filedesc, int rank);

/*!
 * @brief Function to set the file pointer to a new position, updates internal data structure.
 *
 * This function works on Multi-files
 *
 * @param[in,out]  sion_filedesc  sion data structure
 * @param[in]      rank           rank number of the process (SION_CURRENT_RANK to select the current rank)
 *
 * @retval      1 if file pointer can be moved to new position
 *              0 otherwise
 */
int _sion_switch_logical_file_read_master(_sion_filedesc *sion_filedesc, int rank);

/*!
 * @brief Function to set the file pointer to a new position in the same rank, updates internal data structure.
 *
 * \param[in,out]  filedesc descriptor of the file to seek in
 * \param[in] offset
 * \param[in] whence
 *
 * @retval      1 if file pointer can be moved to new position
 *              0 otherwise
 */
int _sion_seek_on_current_rank_read(_sion_filedesc *filedesc, int64_t offset, sion_seek_mode whence);

/*!
 * @brief Function to set the file pointer to a new position, updates internal data structure.
 *
 * This function works on a SINGLE SION file, Multi-files have to be handle in upper layer
 *
 * @param[in,out]  sion_filedesc sion data structure
 * @param[in]      rank                  rank number of the process (SION_CURRENT_RANK to select the current rank)
 *
 * @retval      1 if file pointer can be moved to new position
 *              0 otherwise
 */
int _sion_switch_logical_file_read_mapped(_sion_filedesc *sion_filedesc, int rank);

/*!
 * @brief Function to set the file pointer to a new position, updates internal data structure.
 *
 * This function works on a SINGLE SION file, Multi-files have to be handle in upper layer
 *
 * @param[in,out]  sion_filedesc  sion data structure
 * @param[in]      rank           rank number of the process (SION_CURRENT_RANK to select the current rank)
 *
 * @retval      1 if file pointer can be moved to new position
 *              0 otherwise
 */
int _sion_switch_logical_file_write(_sion_filedesc *sion_filedesc, int rank);

/*!
 * @brief Function to set the file pointer to a new position in the same rank, updates internal data structure.
 *
 * \param[in,out] filedesc descriptor of the file to seek in
 * \param[in] offset
 * \param[in] whence
 *
 * @retval      1 if file pointer can be moved to new position
 *              0 otherwise
 */
int _sion_seek_on_current_rank_write(_sion_filedesc *filedesc, int64_t offset, sion_seek_mode whence);

/*!
 * @brief Function to set the file pointer to a new position, updates internal data structure.
 *
 * This function works on a SINGLE SION file, Multi-files have to be handle in upper layer
 *
 * @param[in,out]  sion_filedesc  sion data structure
 * @param[in]      rank                  rank number of the process (SION_CURRENT_RANK to select the current rank)
 *
 * @retval      1 if file pointer can be moved to new position
 *              0 otherwise
 */
int _sion_switch_logical_file_write_mapped(_sion_filedesc *sion_filedesc, int rank);

/*!
 * @brief Find block and position inside block for absolute position `abspos`.
 *
 *
 * @retval      1 if abspos lays inside available data
 *              0 otherwise
 */
int _sion_seek_search_forward_read(
  _sion_filedesc *filedesc, int64_t offset, int from_block, int *newblocknr, int64_t *newposinblk);

/*!
 * @brief Find block and position inside block position relative to end.
 *
 *
 * @retval      1 if posend lays inside available data
 *              0 otherwise
 */
int _sion_seek_search_backward_read(
  _sion_filedesc *filedesc, int64_t offset, int from_block, int *newblocknr, int64_t *newposinblk);
#endif

/// \endcond
