#include <stdio.h>
#include <string.h>
#include <exec/types.h>
#include <proto/dlg.h>

extern struct Library *DLGBase;

LONG TransBuf(char *inbuffer,
              char *outbuffer,
              int   maxsize,
              int (*callback)(char *,char *)
             )

{long incounter;
 long outcounter;
 long subcount;
 long width;
 long temp;
 char result[80];
 char padded[80];
 char cstr[21];


 outcounter = 0;

 for(incounter = 0; inbuffer[incounter] && (outcounter<maxsize); incounter++)
    {if (inbuffer[incounter] != '%')
        {outbuffer[outcounter] = inbuffer[incounter];
         outcounter++;
         continue;
        }

     temp  = incounter++;
     width =     0;
     if (!sscanf(inbuffer+incounter,"%20[_QWERTYUIOPLKJHGFDSAZXCVBNMqwertyuioplkjhgfdsazxcvbnm].%d",cstr,&width))
        {incounter--;
         outbuffer[outcounter] = inbuffer[incounter];
         outcounter++;
         continue;
        }

     incounter +=  strlen(cstr)-1;
     width      = (width>79) ? 79:width;
     if (width)    incounter += 2;
     if (width>9)  incounter++;

     if (!((*callback)(cstr,result)))
        {incounter = temp;
         outbuffer[outcounter++]='%';
        }

     ASPrintf(NULL, padded, "%-*s", width, result);
     for(subcount = 0; padded[subcount] && (outcounter<maxsize); subcount++)
        {outbuffer[outcounter] = padded[subcount];
         outcounter++;
        }
    }

 outbuffer[outcounter] = 0;
 return(outcounter);
}
