/* module to extract string segments & splice them into a template */
/* Pete Goodeve 87:8:10 */

#include "exec/types.h"
#include "splicer.h"

Slice(source, slice, pieces, sliceset)
  char *source, *slice, *pieces;
struct CutsRef sliceset[];
{
    char *pp = pieces;
    struct CutsRef *markp = sliceset; /* pointers to pieces */
    char index = '0';
    int smark = 0, endmark, len, i;
    len = strlen(source);
    for( i=5; i--; markp++) {
#ifdef DEBUG
        printf("slicept %d  |%s|  smark=%d[s:%d d:%d]\n",
               *slice,source, smark, source, pp);
#endif
        markp->Segment = pp;
        markp->ID = index++;
        markp->Mode = 0;
        if (*slice) {
            endmark = *++slice; /* move to odd byte */
            slice++; /* on to next even byte */
        } else endmark = len;
        for (;smark < endmark; smark++) *pp++ = *source++;
        *pp++ = '\0';
    }
#ifdef DEBUG
    for (markp = sliceset; markp->ID; markp++)
        printf("-->%s<--\n", markp->Segment);
#endif
}


char *Splice(sliceset, template, comp)
  struct CutsRef sliceset[];
  char *template, *comp;
{
    struct CutsRef *sp;
    char cch, *tp, *pp;
    char *cp = comp;
    for (tp = template; *tp; tp++) {
        if (*tp == '^') {
            cch = *++tp;
#ifdef DEBUG
            printf("selector = %c\n", cch);
#endif
            for (sp = sliceset; sp->ID; sp++)
                if (sp->ID == cch) {;
                    /*** note ->Mode MUST be 0 for now !! ****/
#ifdef DEBUG
            printf("segment: %s\n",pp);
#endif
                    for (pp = sp->Segment; *pp; *cp++ = *pp++);
                          /* no null byte */
                    break;
                }
        }
        else *cp++ = *tp;
    }
    *cp = '\0';
    return comp;
}

