//
//
//	XMLLibStub.cpp
//	
//	Copyright © 2003 Nemetschek North America 
//	All Rights Reserved.
//
//	This file contains stub functions for the "XMLLib" SDK Plug-in Library.
//	These stub functions do necessary allocation of argument tables and overhead of calling into the SDK Plug-in Library.
//	The XMLLibStub.h header file contains prototypes that enable other SDK Plug-ins to conveniently use the XMLLib
//	features without worrying about the details of calling mechanism.  
//

#if _WINDOWS
#include <Windows.h>
#include "GSWinSDK.h"	// Graphsoft Required.
#else
#include "StdHeaders.h"
#endif

#include <MiniCadCallBacks.h>
#include <MiniCadExternals.h>
//#include "CustomObjects.h"

#include <GSString.h>
#include <GSString.X.h>
#include <RecordHandler.h>

#include "XMLLibStub.h"



//===========================================
// this is a snippet from another source file of the Plug-in Object....
#include <MiniCadHookIntf.h>
#include <MCCoordTypes.h>
#include <MCVector.h>
#include <MCMatrix.h>



//---------------------------------------------------------------------
long InitXML()
{
	TXString functionName("InitXML");
	PluginLibraryArgTable argTable;
	argTable.functionResult.argType = kLongArgType;
	long libStatus = -1;
	if (GS_VerifyPluginLibrary(gCBP, (const unsigned char*)functionName) == true) {
		if (GS_CallPluginLibrary(gCBP, (const unsigned char*)functionName, &argTable, libStatus) == true)
			return argTable.functionResult.longValue;
	}

	return -1;
}


//---------------------------------------------------------------------
short CreateNewXMLDocument(long XMLHandle, Str255 root)
{
	TXString functionName("CreateNewXMLDocument");
	PluginLibraryArgTable argTable;
	// setup arguments
	argTable.args[0].argType = kLongArgType;
	argTable.args[0].longValue = XMLHandle;										// in
	argTable.args[1].argType = kStringArgType;
	StringCopy(root, argTable.args[1].strValue, root[0]+1);	// StringCopy(src,dst,len);
	argTable.functionResult.argType = kIntegerArgType;
	
	// call function
	long libStatus = -1;
	if (GS_CallPluginLibrary(gCBP, (const unsigned char*)functionName, &argTable, libStatus) == true) {
		return argTable.functionResult.intValue;
	}
	return -1;
}


//---------------------------------------------------------------------
short ReadXMLFile(long XMLHandle, short whichPath, ConstStr255Param filename)
{
	TXString functionName("ReadXMLFile");
	PluginLibraryArgTable argTable;
	argTable.args[0].argType = kLongArgType;
	argTable.args[0].longValue = XMLHandle;
	argTable.args[1].argType = kIntegerArgType;
	argTable.args[1].intValue = whichPath;
	argTable.args[2].argType = kStringArgType;
	StringCopy(filename, argTable.args[2].strValue, filename[0]+1);	// StringCopy(src,dst,len);
	argTable.functionResult.argType = kIntegerArgType;
	long libStatus = -1;
	if (GS_CallPluginLibrary(gCBP, (const unsigned char*)functionName, &argTable, libStatus) == true)
		return argTable.functionResult.intValue;
	
	return -1;
}



//---------------------------------------------------------------------
short WriteXMLFile(long XMLHandle, short whichPath, ConstStr255Param filename)
{
	TXString functionName("WriteXMLFile");
	PluginLibraryArgTable argTable;
	argTable.args[0].argType = kLongArgType;
	argTable.args[0].longValue = XMLHandle;
	argTable.args[1].argType = kIntegerArgType;
	argTable.args[1].intValue = whichPath;
	argTable.args[2].argType = kStringArgType;
	StringCopy(filename, argTable.args[2].strValue, filename[0]+1);	// StringCopy(src,dst,len);
	argTable.functionResult.argType = kIntegerArgType;
	long libStatus = -1;
	if (GS_CallPluginLibrary(gCBP, (const unsigned char*)functionName, &argTable, libStatus) == true) {
		return argTable.functionResult.intValue;
	}
			
	return -1;
}	


//---------------------------------------------------------------------
short GetElementValue(long XMLHandle, Str255 elementPath, Str255& value)
{
	TXString functionName("GetElementValue");
	PluginLibraryArgTable argTable;
	// setup arguments
	argTable.args[0].argType = kLongArgType;
	argTable.args[0].longValue = XMLHandle;										// in
	argTable.args[1].argType = kStringArgType;
	StringCopy(elementPath, argTable.args[1].strValue, elementPath[0]+1);			// in
	argTable.args[2].argType = kStringVarArgType;
	argTable.functionResult.argType = kIntegerArgType;
	// call function
	long libStatus = -1;
	if (GS_CallPluginLibrary(gCBP, (const unsigned char*)functionName, &argTable, libStatus) == true) {
		StringCopy(argTable.args[2].strValue, value, argTable.args[2].strValue[0]+1);	// out
		return argTable.functionResult.intValue;
	}
		
	return -1;	
}


//---------------------------------------------------------------------
short SetElementValue(long XMLHandle, Str255 elementPath, Str255 value)
{
	TXString functionName("SetElementValue");
	PluginLibraryArgTable argTable;
	// setup arguments
	argTable.args[0].argType = kLongArgType;
	argTable.args[0].longValue = XMLHandle;											// in
	argTable.args[1].argType = kStringArgType;
	StringCopy(elementPath, argTable.args[1].strValue, elementPath[0]+1);			// in
	argTable.args[2].argType = kStringArgType;
	StringCopy(value, argTable.args[2].strValue, value[0]+1);						// in
	argTable.functionResult.argType = kIntegerArgType;
	// call function
	long libStatus = -1;
	if (GS_CallPluginLibrary(gCBP, (const unsigned char*)functionName, &argTable, libStatus) == true) {
		return argTable.functionResult.intValue;
	}	
	return -1;
}


//---------------------------------------------------------------------
short DeleteElement(long XMLHandle, Str255 elementPath)
{
	TXString functionName("DeleteElement");
	PluginLibraryArgTable argTable;
	// setup arguments
	argTable.args[0].argType = kLongArgType;
	argTable.args[0].longValue = XMLHandle;										// in
	argTable.args[1].argType = kStringArgType;
	StringCopy(elementPath, argTable.args[1].strValue, elementPath[0]+1);
	argTable.functionResult.argType = kIntegerArgType;
	// call function
	long libStatus = -1;
	if (GS_CallPluginLibrary(gCBP, (const unsigned char*)functionName, &argTable, libStatus) == true) {
		return argTable.functionResult.intValue;
	}
	return -1;
}


//---------------------------------------------------------------------
short GetAttributeValue(long XMLHandle, Str255 elementPath, Str255 attribute, Str255& value)
{
	TXString functionName("GetAttributeValue");
	PluginLibraryArgTable argTable;
	// setup arguments
	argTable.args[0].argType = kLongArgType;
	argTable.args[0].longValue = XMLHandle;										// in
	argTable.args[1].argType = kStringArgType;
	StringCopy(elementPath, argTable.args[1].strValue, elementPath[0]+1);
	argTable.args[2].argType = kStringArgType;
	StringCopy(attribute, argTable.args[2].strValue, attribute[0]+1);
	argTable.args[3].argType = kStringVarArgType;// out only
	StringCopy(value, argTable.args[3].strValue, value[0]+1);

	argTable.functionResult.argType = kIntegerArgType;
	// call function
	long libStatus = -1;
	if (GS_CallPluginLibrary(gCBP, (const unsigned char*)functionName, &argTable, libStatus) == true) {
		StringCopy(argTable.args[3].strValue, value, argTable.args[3].strValue[0]+1);
		return argTable.functionResult.intValue;
	}
	
	return -1;
}


//---------------------------------------------------------------------
short SetAttributeValue(long XMLHandle, Str255 elementPath, Str255 attribute, Str255 value)
{
	TXString functionName("SetAttributeValue");
	PluginLibraryArgTable argTable;
	// setup arguments
	argTable.args[0].argType = kLongArgType;
	argTable.args[0].longValue = XMLHandle;										// in
	argTable.args[1].argType = kStringArgType;
	StringCopy(elementPath, argTable.args[1].strValue, elementPath[0]+1);
	argTable.args[2].argType = kStringArgType;
	StringCopy(attribute, argTable.args[2].strValue, attribute[0]+1);
	argTable.args[3].argType = kStringArgType;
	StringCopy(value, argTable.args[3].strValue, value[0]+1);
	argTable.functionResult.argType = kIntegerArgType;
	// call function
	long libStatus = -1;
	if (GS_CallPluginLibrary(gCBP, (const unsigned char*)functionName, &argTable, libStatus) == true) {
		return argTable.functionResult.intValue;
	}
	return -1;
}


//---------------------------------------------------------------------
short DeleteAttribute(long XMLHandle, Str255 elementPath, Str255 attribute)
{
	TXString functionName("DeleteAttribute");
	PluginLibraryArgTable argTable;
	// setup arguments
	argTable.args[0].argType = kLongArgType;
	argTable.args[0].longValue = XMLHandle;										// in
	argTable.args[1].argType = kStringArgType;
	StringCopy(elementPath, argTable.args[1].strValue, elementPath[0]+1);
	argTable.args[2].argType = kStringArgType;
	StringCopy(attribute, argTable.args[2].strValue, attribute[0]+1);

	// call function
	long libStatus = -1;
	if (GS_CallPluginLibrary(gCBP, (const unsigned char*)functionName, &argTable, libStatus) == true) {
		return argTable.functionResult.intValue;
	}
	return -1;
}

//---------------------------------------------------------------------
short GetCDATA(long XMLHandle, Str255 elementPath, GSHandle& theData)
{
	TXString functionName("GetCDATA");
	PluginLibraryArgTable argTable;
	// setup arguments
	argTable.args[0].argType = kLongArgType;
	argTable.args[0].longValue = XMLHandle;										// in
	argTable.args[1].argType = kStringArgType;
	StringCopy(elementPath, argTable.args[1].strValue, elementPath[0]+1);
	argTable.args[2].argType = kCharDynarrayArgType;	// out
	argTable.functionResult.argType = kIntegerArgType;
	
	// call function
	long libStatus = -1;
	if (GS_CallPluginLibrary(gCBP, (const unsigned char*)functionName, &argTable, libStatus) == true) {
		short theLen = argTable.args[2].dynVar.textLen;
		theData = argTable.args[2].dynVar.textHdl;
		::HandToHand(&theData);		// allocates storage, copies data, resets handle to point to new data. 

		return argTable.functionResult.intValue;
	}

	return nil;
}


//---------------------------------------------------------------------
short SetCDATA(long XMLHandle, Str255 elementPath, void* data)
{
	TXString functionName("SetCDATA");
	PluginLibraryArgTable argTable;
	// setup arguments
	argTable.args[0].argType = kLongArgType;
	argTable.args[0].longValue = XMLHandle;										// in
	argTable.args[1].argType = kStringArgType;
	StringCopy(elementPath, argTable.args[1].strValue, elementPath[0]+1);
	argTable.args[2].argType = kCharDynarrayArgType;
	argTable.args[2].dynVar.textPtr = (unsigned char*)data;	// *********
	argTable.args[2].dynVar.textLen = sizeof(data);
	argTable.functionResult.argType = kIntegerArgType;

	// call function
	long libStatus = -1;
	if (GS_CallPluginLibrary(gCBP, (const unsigned char*)functionName, &argTable, libStatus) == true) {
		return argTable.functionResult.intValue;	
	}
	return -1;
}


//---------------------------------------------------------------------
short DeleteCDATA(long XMLHandle, Str255 elementPath)
{
	TXString functionName("DeleteCDATA");
	PluginLibraryArgTable argTable;
	// setup arguments
	argTable.args[0].argType = kLongArgType;
	argTable.args[0].longValue = XMLHandle;										// in
	argTable.args[1].argType = kStringArgType;
	StringCopy(elementPath, argTable.args[1].strValue, elementPath[0]+1);
	argTable.functionResult.argType = kIntegerArgType;

	// call function
	long libStatus = -1;
	if (GS_CallPluginLibrary(gCBP, (const unsigned char*)functionName, &argTable, libStatus) == true) {
		return argTable.functionResult.intValue;	
	}
	return -1;
}

//---------------------------------------------------------------------
short ReleaseXML(long XMLHandle)
{
	TXString functionName("ReleaseXML");
	PluginLibraryArgTable argTable;
	// setup arguments
	argTable.args[0].argType = kLongArgType;
	argTable.args[0].longValue = XMLHandle;										// in
	argTable.functionResult.argType = kIntegerArgType;

	// call function
	long libStatus = -1;
	if (GS_CallPluginLibrary(gCBP, (const unsigned char*)functionName, &argTable, libStatus) == true) {
		return argTable.functionResult.intValue;	
	}
	return -1;
}


//---------------------------------------------------------------------
short ReadXMLMemory(long XMLHandle, void* data)
{
	TXString functionName("ReadXMLMemory");
	PluginLibraryArgTable argTable;
	// setup arguments
	argTable.args[0].argType = kLongArgType;
	argTable.args[0].longValue = XMLHandle;										// in

	argTable.args[1].argType = kCharDynarrayArgType;
	argTable.args[1].dynVar.textPtr = (unsigned char *)data;
	argTable.args[1].dynVar.textLen = strlen((char*)data);

	// call function
	long libStatus = -1;
	if (GS_CallPluginLibrary(gCBP, (const unsigned char*)functionName, &argTable, libStatus) == true) {
		return argTable.functionResult.intValue;	
	}
	return -1;	// *** unkown error const
}


//---------------------------------------------------------------------
short WriteXMLMemory(long XMLHandle, char*& data)
{
	TXString functionName("WriteXMLMemory");
	PluginLibraryArgTable argTable;
	// setup arguments
	argTable.args[0].argType = kLongArgType;
	argTable.args[0].longValue = XMLHandle;										// in

	argTable.args[1].argType = kCharDynarrayVarArgType;
	argTable.args[1].dynVar.textHdl = NewHandle(0);	// need real master pointer		//GSHandle(&data);
	argTable.args[1].dynVar.textLen = 0;

	// call function
	long libStatus = -1;
	if (GS_CallPluginLibrary(gCBP, (const unsigned char*)functionName, &argTable, libStatus) == true) {
		data = *(argTable.args[1].dynVar.textHdl);		
		return argTable.functionResult.intValue;	
	}

	return kXMLUnknownError;	// *** unkown error const
}


//---------------------------------------------------------------------
// ****
// GetFirstChild
// GetNextElement
// GetPreviousElement
// FindElement
// FindAttribute



//---------------------------------------------------------------------
/*
	TXString functionName("");
	PluginLibraryArgTable argTable;
	// setup arguments
	argTable.args[0].argType = kLongArgType;
	argTable.args[0].longValue = XMLHandle;										// in

	// call function
	long libStatus = -1;
	if (GS_CallPluginLibrary(gCBP, (const unsigned char*)functionName, &argTable, libStatus) == true) {
		return argTable.functionResult.intValue;	
	}

*/