This is trivial enough that we can just do it in C..  If we need to do 
something fancier at a later date, perl would be fine.

Here's a quick hack in C:  (using C makes me appreciate perl ;)

#include <stdio.h>
#include <string.h>

/* filter.c
 * Go through a file and replace all occurrances of "!!!" with "\n".
 * Equivalent to "perl -pe 's/!!!/\n/g'"
 */

#define BLOCKSIZE 2048

int main (int argc, char* argv[]) {
    char* filename;
    FILE* fp;
    char buf[BLOCKSIZE+1];
    char* lastwritten, *nextmatch, *end;
    int rc, sawbangs=0;

    if (argc == 2) {
        fp = fopen(argv[1], "r");
        if (fp == NULL) {
            perror("fopen");
            exit(1);
        }
    } else {
        fp = stdin;  
    }

    while (! feof(fp)) {
        rc = fread(&buf, sizeof(char), BLOCKSIZE, fp);
        if (rc < 0) {
            perror("fread");
            exit(1);
        }

        if (rc > 0)
            buf[rc] = '\0';
        else { 
            buf[0] = '\0';  
            break; 
        }

        lastwritten=buf;

        /* special case- if we saw !'s at the end last time, handle that */
        if (sawbangs == 1 && (*buf == '!') && (*(buf+1) == '!')) {
            printf("\n"); lastwritten+=2;
        }

        if (sawbangs == 2 && (*buf == '!')) {
            printf("\n"); lastwritten+=1;
        }
                    
        /* go through the buffer replacing !!!'s with \n's and writing it out 
           as we go. */
        while (nextmatch = strstr(lastwritten, "!!!")) {
            *nextmatch = '\0';
            
            printf("%s\n", lastwritten);
            lastwritten = nextmatch+3;            
        }

        /* special case- if the last one or two characters of the buffer are
           "!", we make a note of this for next time. */
        end = buf + strlen(buf) - 1;
        if (end < buf) end = buf;

        sawbangs=0;        
        if (*end == '!') { sawbangs=1; *end = '\0'; }
        if (sawbangs && (*(end-1) == '!')) { sawbangs++; *(end-1) = '\0'; }

        /* write out anything which is left */
        printf("%s", lastwritten);
    }
}




> We could use perl... you can download the cygnus compiler for free for
> Windows... which has a perl interpreter as well as a bash shell.
> 
> Nick
> 
> ----- Original Message -----
> From: "Kevin P. Lawton" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Friday, April 07, 2000 4:36 PM
> Subject: Re: Parallel Printers
> 
> 
> > Josh Wilmes wrote:
> > >
> > > > I think the only feature I used tools86 for was to preprocess
> > > > the bios file and separate lines after like...
> > > >
> > > > #define JMPL(label) db 0xe9!!!dw (label-(*+2)) ; jmp near label
> > > >
> > > > ...after gcc -E preprocessed macros.  I could be forgetting
> > > > something else.  But if now, this would only take a small
> > > > C program to do, and could replace using tools86.
> > > >
> > > > When you see '!!!', generate a newline.
> > > >
> > >
> > > You mean something like "perl -pe 's/!!!/\n/g' file"?
> >
> > Yeah, something like that.  Sed, perl, or other stream
> > editor will do as well as C.  I suggested C 'cause
> > I'm always in lowest common denominator mode (Windows).
> >
> > -Kevin
> >
> >
> 
> 



Reply via email to