/**** Xto8SVX *************************************************************
*
* Name    : Xto8SVX
*
* Usage   : Xto8SVX INFILE/A,OUTFILE/A
*
* Description : Convert a sound sample to 8SVX format, using datatypes
*
* Example : Xto8SVX UseTheForce.wav UseTheForce.8SVX
*
* Author  : Tak Tang (tst92@ecs.soton.ac.uk)
*
* Date    : 3rd June 1997
*
* History : 1.0  3/6/97  tst
*           - First version
*
* Legal   : This program is public domain
*
**************************************************************************/


/**** Definitions ****/

#define __USE_SYSBASE

#define THISPROC   ((struct Process *)(SysBase->ThisTask))
#define Result2(x) THISPROC->pr_Result2 = x

#define MSG_FAILED "Xto8SVX failed"

#define TEMPLATE  "INFILE/A,OUTFILE/A"
#define OPT_SOUR  0
#define OPT_DEST  1
#define OPT_COUNT 2


/**** Header files ****/

#include <exec/types.h>
#include <exec/execbase.h>
#include <dos/rdargs.h>

#include <datatypes/datatypes.h>
#include <datatypes/datatypesclass.h>
#include <datatypes/soundclass.h>

#include <clib/alib_protos.h>
#include <clib/dos_protos.h>
#include <clib/exec_protos.h>
#include <clib/datatypes_protos.h>

#include <pragmas/dos_pragmas.h>
#include <pragmas/exec_sysbase_pragmas.h>
#include <pragmas/datatypes_pragmas.h>

#include <string.h>


/**** Storage ****/

UBYTE VersTag[]="\0$VER: Xto8SVX 1.0 (3.6.97)";


/**** Main routine ****/

int cmd_Xto8SVX(void)
{
  struct ExecBase *SysBase = (*((struct ExecBase **) 4));
  struct Library *DOSBase;
  struct Library *DataTypesBase;
  struct RDArgs *rdargs;
  long rc;
  long opts[OPT_COUNT];
  Object *o;
  BPTR fh;

  rc = RETURN_FAIL;

  /* open DOS Library */
  DOSBase = OpenLibrary("dos.library", 36L);

  if (NULL != DOSBase)
  {
    /* open DataTypes Library */
    DataTypesBase = OpenLibrary ("datatypes.library", 39L);
    if (NULL != DataTypesBase)
    {
      /* read filenames */
      memset((char *)opts, 0, sizeof(opts));
      rdargs = ReadArgs(TEMPLATE, opts, NULL);
      if (NULL != rdargs)
      {
        /* load DT Object */
        o = NewDTObject ( (APTR)opts[OPT_SOUR],
            DTA_GroupID, GID_SOUND, TAG_DONE);
        if (NULL!=o)
        {
          /* open output file */
          fh = Open ((STRPTR)opts[OPT_DEST], MODE_NEWFILE);
          if (NULL != fh)
          {
            /* issue the write */
            if (DoDTMethod(o, NULL, NULL, DTM_WRITE, NULL, fh,
                           DTWM_IFF, NULL))
            {
              rc = RETURN_OK;
            } /* if DoDTMethod(DTM_WRITE) OK */
            else
            {
              PrintFault(IoErr(), MSG_FAILED);
            } /* if DoDTMethod(DTM_WRITE) FAIL */

            Close(fh);
          } /* if Open() OK */
          else
          {
            PrintFault(IoErr(), MSG_FAILED);
          } /* if Open() FAIL */

          DisposeDTObject (o);
        } /* if NewDTObject() OK */
        else
        {
          PrintFault(IoErr(), MSG_FAILED);
        } /* if NewDTObject() FAIL */

        FreeArgs(rdargs);
      } /* if readargs() OK */
      else
      {
        PrintFault(IoErr(), MSG_FAILED);
      } /* if readargs() FAIL */

      CloseLibrary(DataTypesBase);
    } /* if OpenLibrary(DataTypes.library) OK */
    else
    {
      PrintFault (IoErr(), MSG_FAILED);
    } /* if OpenLibrary(DataTypes.library) FAIL */

    CloseLibrary(DOSBase);
  } /* if OpenLibrary(DOS.library) OK */
  else
  {
    Result2(ERROR_INVALID_RESIDENT_LIBRARY);
  } /* if OpenLibrary(doslibrary) FAIL */

  return(rc);
} /* int cmd_Xto8SVX() */

/**** End Of File ****/

