/*****************************************************************/
/**                                                             **/
/**             (C) Copyright 1992 the Regents of the           **/
/**        University of California.  All Rights Reserved.      **/
/**                                                             **/
/**        This work was produced under the sponsorship of      **/
/**         the U.S. Department of Energy.  The Government      **/
/**                retains certain rights therein.              **/
/**                                                             **/
/**                         Written by                          **/
/**         Distributed Programming Environments Project        **/
/**           Computer Software and Networking Division,        **/
/**                   Livermore Computer Center,                **/
/**            Lawrence Livermore National Laboratory           **/
/**                                                             **/
/*****************************************************************/

static char sccsid_form_genstr[] = "@(#)form_genstr.c	1.1 10/20/92 05:43:01  LLNL ";

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		James A. Guse
			Network Systems Division

	DATE WRITTEN:	11/6/90

	ROUTINE:	__ROr_format_GeneralString


PURPOSE:
	This routine converts a character string into printable format

IN:
	value - value to print
	valuelength - length in bytes of value variable
	type_info - parameters vector for lengths of this type

OUT:
RETURNS malloc'ed character string

CALLED ROUTINES:

	
MODIFICATION HISTORY:
	920818  Renamed/modified for char string changes (ls)


*/

#include <elros/exceptions.h>

#include <malloc.h>
#include <memory.h>

char *__ROr_format_GeneralString(value,valuelength,maxlength,maxlength2)

	unsigned char *value; /* val field of char string */
/* NOTE: use unsigned here so that we can test for non-ASCII later; but
   the ELROS type translates to plain char */
	unsigned long valuelength; /* len field of char string */
	unsigned long maxlength;
	unsigned long maxlength2;

{
   char *buffer,*bufptr;
   int i,count;

   /* Check for valid string value */
   if (maxlength && valuelength > maxlength)
      return (char *)0;

   for (count = 3,i=0;i<valuelength;i++)
      if (value[i]<32 || value[i] > 127)
         count += 4;
      else if (value[i] == '\\' || value[i] == '\"')
         count += 2;
      else
         count += 1;
   bufptr = buffer = malloc(count);

   *bufptr++ = '"';
   for (i=0; i<valuelength; i++)
      if (value[i]<32 || value[i] > 127)
      {
         sprintf(bufptr,"\\%3.3o",(int)value[i]);
         bufptr += 4;
      }
      else if (value[i] == '\\')
      {
         *bufptr++ = '\\';
         *bufptr++ = '\\';
      }
      else if (value[i] == '\"')
      {
         *bufptr++ = '\\';
         *bufptr++ = '\"';
      }
      else
         *bufptr++ = value[i];
   *bufptr++ = '"';
   *bufptr = 0;
   return(buffer);
}


