/* Agent Tcl
   Bob Gray
   6 August 1998

   tclBlob.cc

   This file implements the C routines that provide the interface between Tcl 
   and the BinaryData classes.

   Copyright (c) 1995-1998, Bob Gray, Dartmouth College

   See the file "agent.terms" for information on usage and redistribution
   of this file and for a DISCLAIMER OF ALL WARRANTIES.
*/

#ifndef NO_PRAGMAS
#pragma implementation "blobTcl.h"
#endif

#include "platPorting.h"
#include "suppBinaryTypes.h"
#include "suppBinary.h"
#include "suppBinaryTable.h"
#include "blobTcl.h"
#include "tcl.h"

/*
 * blob table 
 */

static BinaryTable *s_binaryTable = NULL;

/*
 * create and destroy blobs
 */

int Blob_Create (Tcl_Interp */*interp*/, BinaryDataTypes blobType, UINT_32 *handle)
{
	// create the blob table if necessary

    if (s_binaryTable == NULL) {
	s_binaryTable = new BinaryTable();
    }

	// create the blob

    switch (blobType) {

	case e_BinaryStructure:

	    BinaryDataStructure binaryStructure;
	    *handle = s_binaryTable -> add (binaryStructure);
	    break;
    };

    return (TCL_OK);
}

int Blob_Destroy (Tcl_Interp *interp, UINT_32 handle)
{
    int rc;
    BinaryTable::BinaryTableErrors blobRc;

	// create the blob table if necessary

    if (s_binaryTable == NULL) {
	s_binaryTable = new BinaryTable();
    }

	// remove the blob

    if ((blobRc = s_binaryTable -> remove (handle)) == BinaryTable::e_Ok) {

	rc = TCL_OK;

    } else {

	char temp[16];
	sprintf (temp, "%u", handle);
	Tcl_AppendResult (interp, "no blob with handle ", temp, (char *) NULL);
	rc = TCL_ERROR;
    }

    return (rc);
}
