/******************************************************************************

    MODUL
	strings.c

    DESCRIPTION
	This file contains some functions to work on strings that have
	arbitrary length, i.e. no SPECIFIY length. You can add infinite
	amount of text to them. Well actually not INFINITE, but 2^32-1 which
	is pretty close to "infinite" :-).

    NOTES

    BUGS

    TODO

    EXAMPLES

    SEE ALSO

    INDEX
	CreateVString
	AppendString
	AppendVString
	InsertString
	InsertVString
	FreeVString
	CopyVString
	DuplicateVString
	VStringLength

    HISTORY
	14. Nov 1992	ada created

******************************************************************************/

/**************************************
		Includes
**************************************/
#include <exec/types.h>
#include <exec/memory.h>
#define STRING_C
#include "vstring.h"


/**************************************
	    Globale Variable
**************************************/


/**************************************
      Interne Defines & Strukturen
**************************************/
/* All sizes are WITHOUT the Null-byte */
typedef struct {
    ULONG length;	    /* This is the actual length of the VString */
    ULONG size; 	    /* This is the amount of memory we reserved */
    UBYTE text[1];	    /* Here is the contents */
} VString;


/**************************************
	    Interne Variable
**************************************/


/**************************************
	   Interne Prototypes
**************************************/


/*****************************************************************************

    NAME
	CreateVString

    PARAMETER
	UBYTE * initial_contents;

    RESULT

    RETURN
	VString * vstring;

    DESCRIPTION
	Creates a new VString with a initial value. If initial_contents is
	zero, it creates an empty VString.

    NOTES

    BUGS

    EXAMPLES

    SEE ALSO

    INTERNALS

    HISTORY
	14. Nov 1992	ada created

******************************************************************************/

VString * CreateVString (UBYTE * initial_contents)
{
    VString * vstring;

    if (vstring = AllocMem (sizeof(VString) + 7, 0)) {
	vstring->length  = 0;
	vstring->size	 = 7;
	vstring->text[0] = 0;

	if (initial_contents)
	    AppendString2VS (vstring, initial_contents);
    }

    return (vstring);
} /* CreateVString */


/*****************************************************************************

    NAME
	AppendString2VS

    PARAMETER
	VString * vstring;
	UBYTE	* string;

    RESULT

    RETURN
	BOOL success;

    DESCRIPTION
	Appends a simple string to a VString.

    NOTES

    BUGS

    EXAMPLES

    SEE ALSO

    INTERNALS

    HISTORY
	22. Nov 1992	ada created

******************************************************************************/

void AppendString2VS (VString * vstring, UBYTE * string)
{
    ULONG   len;
    ULONG   size;
    UBYTE * text;

    len = strlen (string);

    if (len+vstring->length <= vstring->size) {
	movmem (string, vstring->text + vstring->length, len+1);
    } else {
	VString * new;
	size = (len + 7) & -7;

	if (!(new = AllocMem (sizeof(VString) + size, 0)) )
	    return (FALSE);

	new->length = len;
	new->size   = size;
	movmem (new->text, vstring->
} /* AppendString2VS */


/******************************************************************************
*****  ENDE strings.c
******************************************************************************/
