/************************************************************************/
/* $Header: /usr/tiburon/unix/gui/proto/RCS/convert.c,v 1.1 1998/01/28 16:17:12 oreilly Exp $		*/
/* Summary  : Conversion convenience Routine				*/
/* Filename : convert.c							*/
/* Author   : Integrated Computer Solutions (ICS)			*/
/* Project  : New ROV							*/
/* $Revision: 1.1 $							*/
/* Comments : Copied and modified from $BX/functions.ansi/convert.c	*/
/************************************************************************/
/* Modification History:						*/
/* $Log: convert.c,v $
 * Revision 1.1  1998/01/28 16:17:12  oreilly
 * Initial revision
 *
 * Revision 1.0  92/11/20  12:03:30  12:03:30  hebo (Bob Herlien)
 * Initial revision
 * 
*/
/************************************************************************/

#include <Xm/Xm.h>

     XtPointer
CONVERT(Widget w, char *from_string, char *to_type, int to_size, 
	Boolean *success)
{
    XrmValue		fromVal, toVal;	/* resource holders		*/
    Boolean		convResult;	/* return value			*/
    unsigned char	oByte;		/* one byte result		*/
    unsigned short	tByte;		/* two byte result		*/
    XtPointer		fByte;		/* four byte result		*/
    XtPointer		aByte;		/* allocated result		*/
    
    /*
     * Zero it.
     */
    *success = False;
    fByte = aByte = NULL;
    
    /*
     * String to string.
     */
    if( strcmp(XmRString, to_type) == 0 )
    {
	*success = True;
	return( from_string );
    }

    /*
     * Sometimes we do not know this at code output.
     */
    if(to_size == 0) to_size = strlen(from_string);
        
    /*
     * Motif String to StringTable converter
     * does not cache correctly and so
     * we need to special case this.
     */ 
    if( strcmp(to_type, "XmStringTable") == 0)
    {
	register int lcount, loop;
	register char tmp, *ptr, *e_ptr;
	char *tmp_from;
	XmStringTable table;
	
	tmp_from = XtNewString((char *)from_string);
	ptr = tmp_from;
	if ((ptr == NULL) || (*ptr == '\0'))
	{
	    XtFree((XtPointer)tmp_from);
	    return( NULL );
	}
	else 
	{	
	    lcount = 1;
	    while (*ptr != '\0') 
	    {
		if (*ptr++ == '\n')
		    lcount++;
	    }
	    table = (XmStringTable)
		XtMalloc(sizeof(XmString) * lcount);
	    
	    ptr = tmp_from;
	    
	    for (loop = 0; loop < lcount; loop++) 
	    {
		e_ptr = ptr;
		while((*e_ptr!='\0')&&(*e_ptr != '\n'))
		    e_ptr++;	     
		tmp = *e_ptr;
		*e_ptr = '\0';
		table[loop] = XmStringCreateSimple(ptr);
		*e_ptr = tmp;
		ptr = ++e_ptr;
	    }
	    
	    fByte = (XtPointer)table;
	}
	*success = True;
	XtFree((XtPointer)tmp_from);
	/*SUPPRESS 80*/
	return( fByte );
    }

    /*
     * Set up the list.
     */
    fromVal.size = strlen(from_string) + 1;
    fromVal.addr = from_string;

    switch( to_size )
    {
    case 1:
	toVal.size = sizeof(unsigned char);
	toVal.addr = (XtPointer)&oByte;
	break;
    case 2:
	toVal.size = sizeof(unsigned short);
	toVal.addr = (XtPointer)&tByte;
	break;
    default:
	toVal.size = sizeof(XtPointer);
	toVal.addr = (XtPointer)&fByte;
	break;
    }
    
    convResult = XtConvertAndStore(w, 
				   XmRString, 
				   &fromVal,
				   to_type,
				   &toVal);
    
    if( convResult )
    {
	switch( to_size )
	{
	case 1:
	    fByte = (XtPointer)((int)oByte);
	    break;
	case 2:
	    fByte = (XtPointer)((int)tByte);
	    break;
	default:
	    break;
	}
    }
    

    /*
     * Conversion will fail if we need more than 4 bytes.
     */
    if( !convResult && toVal.size != to_size )
    {
	/*
	 * Need to allocate more space for this one.
	 */
	toVal.addr = (XtPointer)XtMalloc(toVal.size);
	fByte = aByte = toVal.addr;
	convResult = XtConvertAndStore(w, 
				       XmRString, 
				       &fromVal,
				       to_type,
				       &toVal);
    }
    
    /*
     * Free any thing useless we may have allocated.
     */
    if( !convResult )
    {
	XtFree(aByte);
	aByte = NULL;
    }
    
    /*
     * Return the result.
     */
    *success = convResult;
    /*SUPPRESS 80*/
    return( fByte );
}
