#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/times.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>
#include <iconv.h>

#include "riscos.h"
#include "module-iconv.h"

bool Iconv::SWI (unsigned int swinum, bool xbit)
{
  switch (swinum)
    {
    case Iconv_Open: Open (); break;
    case Iconv_Iconv: iconv (); break;
    case Iconv_Close: Close (); break;
    default:
      return false;
      break;
    }
  return true;
}

void Iconv::Open (void)
{
  const char *tocode = (const char *) READ_REG (0);
  const char *fromcode = (const char *) READ_REG (1);
  iconv_t ret = iconv_open (tocode, fromcode);
  WRITE_REG (0, (uint32_t) ret);
  CLEAR_V ();
}

void Iconv::iconv (void)
{
  iconv_t cd = (iconv_t) READ_REG (0);
  char **inbuf = (char **) READ_REG (1);
  size_t *inbytesleft = (size_t *) READ_REG (2);
  char **outbuf = (char **) READ_REG (3);
  size_t *outbytesleft = (size_t *) READ_REG (4);
  size_t ret = ::iconv (cd, inbuf, inbytesleft, outbuf, outbytesleft);
  WRITE_REG (0, (uint32_t) ret);
  CLEAR_V ();
}

void Iconv::Close (void)
{
  iconv_t cd = (iconv_t) READ_REG (0);
  int ret = iconv_close (cd);
  WRITE_REG (0, ret);
  CLEAR_V ();
}
