[comp.sources.misc] v05i052: TC 2.0 source for TRAV text filter

amlovell@phoenix.princeton.edu (Anthony M Lovell) (11/15/88)

Posting-number: Volume 5, Issue 52
Submitted-by: "Anthony M Lovell" <amlovell@phoenix.princeton.edu>
Archive-name: trav.tc

This program turns ordinary text into infinite, amusing gibberish.
I think the original algorithm was called TRAVESTY.

This program is an implementation of an algorithm a friend told me about
that might have been published in an old BYTE magazine article.  If
coding up such a thing violates any copyright laws, I understand that I
will be fed drugs that slow my perception of time and then fed feet
first into a tree chipper.  I have not bothered to put it in a shell
script since I never really understood what they did or how they're
used.

The program reads the standard input for a textfile (which must be
<BUFSIZE in length).  It expects an <order> integer to be supplied on
the command line.  It will immediately output the first <order>
characters of the input.  Then a loop outputs characters one at a time
until a break is issued from the keyboard.

Loop goes thusly:
1. consider the last <order> characters output as a string S.
2. find all occurrences of S within the input file.
3.  Consider all characters C that immediately follow each instance of S.
4.  Select one char from C at random, output it, and loop to 1.

The results must be seen to be appreciated.. it is something like a
drunkard trying to recount a story you told him for orders ~7 and it is
like crack addicts for orders ~2-3.
Let me know what you think of this program.
Program source code follows:

/* cut here, and call the file trav.c and compile w/ TC 2.0 under MSDOS */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define BUFSIZE 32768
#define MAXORDER 16
#define MAXCANDS 256

char *buf;
unsigned int bs;
int order,linelen;
char lastout[MAXORDER];
char cands[MAXCANDS],outtahere;

unsigned int readbuf()
{   char temp[256];
    int i;

    buf = (char *) malloc(BUFSIZE);
    strcpy(buf,"");
    while (!feof(stdin)) {
       scanf("%[^\n]\n",temp);
       strcat(buf,strcat(temp," "));
       }
    return(strlen(buf));
}

findcands()
{ char *pp; int i;

  strcpy(cands,"");
  pp = buf;
  while ((pp = strstr(pp,lastout))!=NULL)
     { i = strlen(cands);
       cands[i] = *(pp+order); cands[++i] = '\0';
       pp++;
       }
}

restart()
{
  strncpy(lastout,buf,order);
  linelen = order;
  printf("\n%s",lastout);
}


main(int argc, char *argv[])
{
  randomize();

  if (argc<1 || (!(order = atoi(argv[1]))))
     { puts("USAGE:  TRAV <order>   where <order> is an integer 2-10.");
       exit(1); }

  bs = readbuf();

  restart();
  while (kbhit()) getch();
  while (!kbhit()) {
     findcands();
     if (cands[0] == '\0')
       { restart(); findcands(); }
     outtahere = cands[random(strlen(cands))];
     putchar(outtahere);
     lastout[order] = outtahere;
     lastout[order+1] = '\0';
     movmem(lastout+1,lastout,order+1);

     linelen++;
     if (linelen > 55 && isspace(outtahere))
       { puts(""); linelen = 0; }
     }
}  /* end of program file trav.c */
-- 
amlovell@phoenix.princeton.edu     ...since 1963.