#include "ka_input.h"

/**
 * Releases the input stream and all its data.
 *
 * @param  f         Input stream handle to delete.
 */
void ka_delete_input(ka_input_t** pf)
{
  if (!*pf)
    return;

  (*pf)->vptr->FNdelete(pf);
}

/**
 * Returns the length in bytes of the input stream.
 *
 * @param  f         Input stream handle.
 *
 * @returns Length in bytes of the input stream.
 */
uint64_t ka_input_getLen(ka_input_t* f)
{
  return f->vptr->FNgetLen(f);
}

/**
 * Closes the input stream.
 *
 * @param  f         Input stream handle.
 *
 * @returns negative value in case of error.
 */
int ka_input_close(ka_input_t* f)
{
  return f->vptr->FNclose(f);
}

/**
 * Returns the input stream position.
 *
 * @param  f         Input stream handle.
 *
 * @returns Position within the input stream
 *          or negative value in case of error.
 */
uint64_t ka_input_tell(ka_input_t* f)
{
  return f->vptr->FNtell(f);
}

/**
 * Changes from position within the input stream.
 *
 * @param  f         Input stream handle.
 * @param  pos       New relative position.
 *
 * @returns negative value in case of error.
 */
int ka_input_seek(ka_input_t* f, uint64_t pos)
{
  return f->vptr->FNseek(f, pos);
}

/**
 * Has enough data from current position in stream.
 *
 * @param  f         Input stream handle.
 * @param  nbr       Number of bytes expected.
 *
 * @returns 1 Ok, 0 less than nbr bytes remaining, -1 waiting.
 */
int ka_input_hasBytes(ka_input_t* f, int nbr)
{
  return f->vptr->FNhasBytes(f, nbr);
}

/**
 * Skip data from current position in stream.
 *
 * @param  f         Input stream handle.
 * @param  nbr       Number of bytes to skip.
 *
 * @returns Number of bytes skipped, -1 if error.
 */
int ka_input_skip(ka_input_t* f, int nbr)
{
  return f->vptr->FNskip(f, nbr);
}

/**
 * Read data from current position in stream.
 *
 * @param  f         Input stream handle.
 * @param  ptr       Destination pointer where to store the data.
 * @param  nbr       Number of bytes to read.
 *
 * @returns Number of bytes read, -1 if error.
 */
int ka_input_read(ka_input_t* f, void* ptr, int nbr)
{
  return f->vptr->FNread(f, ptr, nbr);
}

/**
 * Read data from current position in stream and add it to buffer.
 *
 * @param  f         Input stream handle.
 * @param  buffer    Buffer handle.
 * @param  pdone     EOF indecator to fill on exit.
 *
 * @returns Number of bytes read.
 */
int ka_input_buffer(ka_input_t* f, ka_buffer_t* pbuffer, int* pdone)
{
  return f->vptr->FNbuffer(f, pbuffer, pdone);
}
