/* template.c -- routines to manipulate templates */
#include "he.h"

#define TEMPLATE_CHAR1 '#'
#define TEMPLATE_CHAR2 '@'
#define TEMPLATE_CHAR3 '%'

void convertTemplate(template, n1, n2, n3, pname)
    char *template;
    int n1, n2, n3;
    char **pname;
{
    char s1[20], s2[20], s3[20];
    char *t;

    sprintf(s1, "%1d", n1);
    sprintf(s2, "%1d", n2);
    sprintf(s3, "%1d", n3);

    *pname = t = (char *) malloc(strlen(template) + 61);

    while (*template)
	switch(*template)
	{
	case TEMPLATE_CHAR1:
	    fillTemplate(&template, &t, s1, TEMPLATE_CHAR1);
	    break;
	case TEMPLATE_CHAR2:
	    fillTemplate(&template, &t, s2, TEMPLATE_CHAR2);
	    break;
	case TEMPLATE_CHAR3:
	    fillTemplate(&template, &t, s3, TEMPLATE_CHAR3);
	    break;
	default:
	    *t++ = *template++;
	}
    *t = '\0';
}

void fillTemplate(template, pout, s, templateChar)
    char **template;
    char **pout;
    char *s;
    char templateChar;
{
    int templateLen, sLen;

    /* count length of template to replace */
    for (templateLen = 0; **template == templateChar;
	 (*template)++, templateLen++);
    sLen = strlen(s);

    /* fill with zero's if the space reserved in template is
       longer than the length of s */
    for (; templateLen > sLen; templateLen--)
	*(*pout)++ = '0';

    while (*s) *(*pout)++ = *s++;
}

/* end of template.c */
