/* Agent Tcl
   Bob Gray
   21 January 1997

   genAgentConv.cc

   This file implements various conversion routines that are useful only in
   the agent interpreters.

   Copyright (c) 1995-1997, 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
#endif

#include "platPorting.h"
#include "platDelete.h"		// DELETE_OBJECT and DELETE_ARRAY_OBJECT
#include "mesgTcpip.h"
#include "genAgentConv.h"
#include "genConversion.h"
#include "genMasks.h"
#include "genUtility.h"

const char * const SPACE = " ";

/* Agent_AgentMaskToEntryToString

   Purpose: Create a string representation for a mask entry

     Input: entry  = the mask entry
		     (struct AgentMaskEntry *)

    Output: The procedure returns a pointer to a DYNAMICALLY allocated string
	    that contains the string representation of the mask entry.
*/

char *Agent_AgentMaskEntryToString (const struct AgentMaskEntry *entry)
{
    int idLength = 0;
    int ipLength = 0;
    int tagLength = 0;
    int length = 0;
    int nameLength = 0;
    int typeLength = 0;
    int serverLength= 0;
    int handlerLength = 0;
    char id_string[16];
    char *string = NULL;
    char *ip_string = NULL;
    char *type_string = NULL;
    DynamicString handler_string;

	/* calculate the length of the string representation; */
        /* first default or actual representation?            */

    if (entry -> isDefault()) {

	length += 8;        // "default "

    } else {

	    /* server name and IP address */

	DynamicString serverName = entry -> getId().getServerName();

	if ((serverLength = serverName.length()) == 0) {
	    length += 8;        // "ANY ANY "
	} else {
	    ip_string = tcpip_IPToString (entry -> getId().getServerIp());
	    ipLength = strlen (ip_string);
	    length += serverLength + ipLength + 2;
	}

	    /* agent name */

	DynamicString name = entry -> getId().getName();

	if ((nameLength = name.length()) == 0) {
	    length += 4;     // "ANY "
	} else {
	    length += nameLength + 3;
	}

	    /* agent id */

	UINT_32 id = entry -> getId().getId();

	if (id == ANY_ID) {
	    length += 4;     // "ANY "
	} else {
	    fastIntegerToAscii (id, id_string);
	    idLength = strlen(id_string);
	    length += idLength + 1;
	}
    }

	    /* event tag */

    if (entry -> hasTag()) {
	tagLength = strlen (entry -> getTag());
	length += tagLength + 8;        // "-tag {<TAG>} "
    } 

	/* handler */

    if (entry -> hasHandler()) {
	handler_string = entry -> getHandler().getStringRepresentation ();
        type_string = (char *) ((entry -> getHandler().getType() == AgentMaskHandler::EVENT_HANDLER) ? "-event" : "-interrupt");
	typeLength = (entry -> getHandler().getType() == AgentMaskHandler::EVENT_HANDLER) ? 6 : 10;
	handlerLength = handler_string.length();
	length += typeLength + handlerLength + 4;
    } 
	
	/* construct the string representation */
        /* default or actual identification?   */

    string   = new char[length];
    char *dp = string;
 
    if (entry -> isDefault()) {

	dp = fast_memcpy (dp, "default ", 8);

    } else {

	    /* server name and IP address */

	if (serverLength  == 0) {
	    dp = fast_memcpy (dp, "ANY ANY ", 8);
	} else {
	    DynamicString serverName = entry -> getId().getServerName();
	    dp = fast_memcpy (dp, serverName.value(), serverLength);
	    dp = fast_memcpy (dp, SPACE, 1);
	    dp = fast_memcpy (dp, ip_string, ipLength);
	    dp = fast_memcpy (dp, SPACE, 1);
	}

	    /* agent name */

	if (nameLength == 0) {
	    dp = fast_memcpy (dp, "ANY ", 4);
	} else {
	    DynamicString name = entry -> getId().getName();
	    dp = fast_memcpy (dp, "{", 1);
	    dp = fast_memcpy (dp, name.value(), nameLength);
	    dp = fast_memcpy (dp, "}", 1);
	    dp = fast_memcpy (dp, SPACE, 1);
	}

	    /* agent id */

	UINT_32 id = entry -> getId().getId();

	if (id == ANY_ID) {
	    dp = fast_memcpy (dp, "ANY ", 4);
	} else {
	    dp = fast_memcpy (dp, id_string, idLength);
	    dp = fast_memcpy (dp, SPACE, 1);
	}
    }

	/* event tag */

    if (entry -> hasTag()) {
	dp = fast_memcpy (dp, "-tag ", 5); 
	dp = fast_memcpy (dp, "{", 1);
	dp = fast_memcpy (dp, entry -> getTag(), tagLength);
	dp = fast_memcpy (dp, "}", 1);
	dp = fast_memcpy (dp, SPACE, 1);
    } 

	/* handler */

    if (entry -> hasHandler()) {
	dp = fast_memcpy (dp, type_string, typeLength);
	dp = fast_memcpy (dp, SPACE, 1);
	dp = fast_memcpy (dp, "{", 1);
	dp = fast_memcpy (dp, handler_string.value(), handlerLength);  
	dp = fast_memcpy (dp, "}", 1);
	dp = fast_memcpy (dp, SPACE, 1);
    }

	/* terminating byte ('\0') */

    *(dp - 1) = '\0';

	/* return the string */

    DELETE_ARRAY_OBJECT(ip_string);
    return (string);
}
