;/*
lc -L -cfist -y -v -j73 clipit
quit
 * clipit.c
 */

#include <exec/types.h>
#include <exec/memory.h>
#include <exec/execbase.h>
#include <exec/libraries.h>
#include <datatypes/datatypes.h>
#include <datatypes/datatypesclass.h>
#include <datatypes/textclass.h>
#include <dos/dos.h>
#include <string.h>
#include <stdio.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_pragmas.h>
#include <pragmas/datatypes_pragmas.h>

extern struct ExecBase *SysBase;
extern struct Library *DOSBase;
struct Library *DataTypesBase;

void main (int argc, char **argv)
{
    struct dtGeneral dtg;
    struct gpLayout gpl;
    BYTE *buffer;
    ULONG length;
    Object *o;
    BPTR sfh;

    if (argc < 2)
    {
	printf ("requires a file name\n");
    }
    else if (DataTypesBase = OpenLibrary ("datatypes.library", 39))
    {
	/* Open and read the source raw sound file */
	if (sfh = Open (argv[1], MODE_OLDFILE))
	{
	    Seek (sfh, 0, OFFSET_END);
	    if ((length = Seek (sfh, 0, OFFSET_BEGINNING)) > 0)
	    {
		printf ("length = %ld bytes\n", length);

		if (buffer = AllocVec (length + 1, MEMF_CLEAR))
		{
		    if (Read (sfh, buffer, length) == length)
		    {
			    /* Create the text object */
			    if (o = NewDTObject ((APTR) "ascii.test",
						 DTA_SourceType, DTST_RAM,
						 DTA_GroupID, GID_TEXT,
						 TDTA_Buffer, buffer,
						 TDTA_BufferLen, length,
						 TAG_DONE))
			    {
				/* Tell the object to layout */
				gpl.MethodID = DTM_PROCLAYOUT;
				gpl.gpl_GInfo = NULL;
				gpl.gpl_Initial = 1;
				if (DoMethodA (o, &gpl))
				{
				    /* Copy it to the clipboard */
				    dtg.MethodID = DTM_COPY;
				    DoDTMethodA (o, NULL, NULL, &dtg);
				}

				/* Get rid of the object */
				DisposeDTObject (o);

				/* Once you send the buffer to the object, it belongs to it. */
				buffer = NULL;
			    }
			    else
			    {
				printf ("couldn't create text object\n");
			    }
		    }
		    FreeVec (buffer);
		}
	    }
	    Close (sfh);
	}

	CloseLibrary (DataTypesBase);
    }
    else
	printf ("couldn't open datatypes.library V39\n");
}
