/*static char *SCCSID = "@(#)parmmsg.c	6.2 91/01/14";*/
#define SCCSID "@(#)parmmsg.c	6.2 91/01/14"
/*
 *	IBM/Microsoft Confidential
 *
 *	Copyright (c) IBM Corporation 1987, 1989
 *	Copyright (c) Microsoft Corporation 1987, 1989
 *
 *	All Rights Reserved
 *
 */

/*
 *  PARMMSG.C
 *
 *    DESCRIPTION
 *    
 *      This file contains the function which inserts parameters into
 *      a message string.
 *      
 *
 *    FUNCTIONS DEFINED
 *
 *      ParmMsg ()          --  Copies parameters into a string.
 *
 *
 *  REVISION HISTORY (in reverse chronological order)
 *  -----------------------------------------------------------------------
 *  3/12/90     Brian Andrew    Initial version of ParmMsg.
 *
 *  TO DO
 *  -------------------------------------------------------------------------
 */

#define INCL_DOSERRORS
#define INCL_NOPMAPI

#include <os2.h>

#include "cdfs.h"
#include "sem.h"
#include "path.h"
#include "vol.h"
#include "file.h"
#include "buffer.h"
#include "mem.h"
#include "heap.h"
#include "message.h"

#ifdef STATS_H
#include "stats.h"
#endif

#include "fsdata.h"
#include "memory.h"
#include "pr_misc.h"
#include "pr_strng.h"

/***    ParmMsg ()  -- Insert parameters into a string.
 *      
 *  DESCRIPTION
 *
 *      This function inserts parameters into a string.  The string 
 *      is passed in a MESSAGE structure.  The parameters are passed in
 *      in an array of pointers to character strings.  The resulting
 *      string is stored in a buffer pointed to by the 'pszDst' parameter.
 *
 *      The arguement string is walked through character by character.
 *      When a '%' character is encountered, it and the single digit
 *      which follows it are replaced by a string out of the array of
 *      parameter strings.
 *
 *      This function only supports parameter digits '1' through '9' with
 *      parameter digit '1' indicating the first parameter string in
 *      the array of parameters.
 *      
 *      
 *  ENTRY
 *
 *      pmMsg       -   Pointer to MESSAGE for basic string.
 *      pszDst      -   Pointer to buffer to store resulting string.
 *      apcParms    -   Array of pointers to characters.  These
 *                      are the parameter strings.
 *
 *
 *  LOCAL
 *
 *      usTotalLen  -   Length of resulting string.
 *      cbCount     -   Number of bytes remaining in source string.
 *      cbParmLen   -   Length of parameter string.
 *      usParmIndex -   Index into parameter array.
 *      pSrc        -   Current position in source string.
 *
 *
 *  EXIT
 *
 *      pszDst contains the modified string.
 *
 *
 *  RETURN
 *      Length of the result string.
 */

USHORT ParmMsg (pmMsg, pszDst, apcParms)
PMESSAGE        pmMsg;
PCHAR           pszDst;
PCHAR       FAR apcParms[];
{
	PCHAR       pSrc;      
	USHORT      cbCount;    
	USHORT      cbParmLen;  
	USHORT      usParmIndex;
	USHORT      usTotalLen;      

	/* Initialize usTotalLen */
	usTotalLen = 0;

	/* Get the starting values from 'pmMsg' */
	cbCount = pmMsg->cchMessage;
	pSrc = pmMsg->pszMessage;

	while (cbCount--)
	{
		/* Is it the start of a parameter */

		if (*pSrc == '%')
		{
			/* Swallow the '%' character */
			pSrc++;
			cbCount--;

			/* Substitute the parameter string */
			usParmIndex = '1' - *pSrc;
			cbParmLen = istrlen (apcParms[usParmIndex]);
			memcpy (pszDst, apcParms[usParmIndex], cbParmLen);

			/* Add to the length of the result string */
			usTotalLen += cbParmLen;

			/* Swallow the parameter digit and update destination pointer */
			pSrc++;
			pszDst += cbParmLen;
		}

		/* Else copy a single byte */
		else
		{
			*(pszDst++) = *(pSrc++);
			usTotalLen++;
		}
	}

	/* Null terminate the result string */
	*pszDst = '\0';

	return usTotalLen;

}
