/*
 *	$VER: loaddttext.c 0.1 (01.05.2000)
 *
 *	Shows how to load text files using datatypes.library, using the
 *	text.datatype class and its sub classes
 *
 *	Written by Amarpreet Singh Munde.
 *
 */

/* amiga includes */
#include <exec/types.h>
#include <exec/memory.h>
#include <exec/libraries.h>
#include <dos/dos.h>
#include <datatypes/datatypes.h>
#include <datatypes/datatypesclass.h>
#include <datatypes/textclass.h>

/* amiga prototypes */
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/datatypes_protos.h>

/* ansi includes */
#include <string.h>

/*
 * Library Structures
 * Some are extern since they are automatically opened by the compiler
 */
extern struct Library	*SysBase,
						*DOSBase;
struct Library			*DataTypesBase;

/* Main Prog */
int main( void )
{
	if( (SysBase -> lib_Version) < 39UL )
	{
		Printf( "requires 3.x or beyond to run\n" );
	}
	else
	{
		/* Open the libraries */
		if( DataTypesBase = OpenLibrary("datatypes.library", 39))
		{
			/*
			 * filename is the name of the file you want to load
			 * size should be what you want including path
			 */
			char	filename[128] = "loaddttext.c";

			/* dto is a pointer to the datatype object */
			Object	*dto;

			/*
			 * Here we specify a GroupID of Text so it will only return if
			 * it is a text object
			 */
			if(dto = NewDTObject(filename, DTA_GroupID, GID_TEXT, TAG_DONE))
			{
				/* len is the size of the text buffer */
				ULONG len;
				/* buffer is the pointer to the actual text */
				STRPTR buffer;

				/*
				 * Get the attributes that we need to determine
				 * memory needed and where text buffer is
				 * You can also get any other attribute you may need
				 */
				GetDTAttrs (dto,
					TDTA_Buffer,	(ULONG)&buffer,
					TDTA_BufferLen,	(ULONG)&len,
					TAG_DONE);

				/* Make sure we have a text buffer */
				if (buffer && len)
				{
					/*
					 * here you copy the datatypes buffer to your own buffer
					 * because we will free it via DisposeDTObject below
					 */
				}
				else
				{
					Printf("Text Object info could not be determined\n");
				}

				/* Dispose of the DataType object */
				DisposeDTObject( dto );
			}
			else
			{
				Printf("Couldn't open object \n");
			}

			CloseLibrary(DataTypesBase);
		}
		else
		{
			/*
			 * Couldn't open datatypes.library, Version 39 or higher
			 * You should use version 39 since it has all the needed functions
			 */
			Printf("Couldn't open datatypes.library V39\n");
		}
	}
}
