home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume05 / trav.tc < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  3.6 KB

  1. From decwrl!ucbvax!agate!pasteur!ames!mailrus!cwjcc!hal!ncoast!allbery Fri Nov 18 20:44:48 PST 1988
  2. Article 729 of comp.sources.misc:
  3. Path: granite!decwrl!ucbvax!agate!pasteur!ames!mailrus!cwjcc!hal!ncoast!allbery
  4. From: amlovell@phoenix.princeton.edu (Anthony M Lovell)
  5. Newsgroups: comp.sources.misc
  6. Subject: v05i052: TC 2.0 source for TRAV text filter
  7. Keywords: fun educational amaze your friends
  8. Message-ID: <4437@phoenix.Princeton.EDU>
  9. Date: 15 Nov 88 00:37:46 GMT
  10. Sender: allbery@ncoast.UUCP
  11. Reply-To: amlovell@phoenix.princeton.edu (Anthony M Lovell)
  12. Organization: Princeton University, NJ
  13. Lines: 110
  14. Approved: allbery@ncoast.UUCP
  15.  
  16. Posting-number: Volume 5, Issue 52
  17. Submitted-by: "Anthony M Lovell" <amlovell@phoenix.princeton.edu>
  18. Archive-name: trav.tc
  19.  
  20. This program turns ordinary text into infinite, amusing gibberish.
  21. I think the original algorithm was called TRAVESTY.
  22.  
  23. This program is an implementation of an algorithm a friend told me about
  24. that might have been published in an old BYTE magazine article.  If
  25. coding up such a thing violates any copyright laws, I understand that I
  26. will be fed drugs that slow my perception of time and then fed feet
  27. first into a tree chipper.  I have not bothered to put it in a shell
  28. script since I never really understood what they did or how they're
  29. used.
  30.  
  31. The program reads the standard input for a textfile (which must be
  32. <BUFSIZE in length).  It expects an <order> integer to be supplied on
  33. the command line.  It will immediately output the first <order>
  34. characters of the input.  Then a loop outputs characters one at a time
  35. until a break is issued from the keyboard.
  36.  
  37. Loop goes thusly:
  38. 1. consider the last <order> characters output as a string S.
  39. 2. find all occurrences of S within the input file.
  40. 3.  Consider all characters C that immediately follow each instance of S.
  41. 4.  Select one char from C at random, output it, and loop to 1.
  42.  
  43. The results must be seen to be appreciated.. it is something like a
  44. drunkard trying to recount a story you told him for orders ~7 and it is
  45. like crack addicts for orders ~2-3.
  46. Let me know what you think of this program.
  47. Program source code follows:
  48.  
  49. /* cut here, and call the file trav.c and compile w/ TC 2.0 under MSDOS */
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <ctype.h>
  54. #define BUFSIZE 32768
  55. #define MAXORDER 16
  56. #define MAXCANDS 256
  57.  
  58. char *buf;
  59. unsigned int bs;
  60. int order,linelen;
  61. char lastout[MAXORDER];
  62. char cands[MAXCANDS],outtahere;
  63.  
  64. unsigned int readbuf()
  65. {   char temp[256];
  66.     int i;
  67.  
  68.     buf = (char *) malloc(BUFSIZE);
  69.     strcpy(buf,"");
  70.     while (!feof(stdin)) {
  71.        scanf("%[^\n]\n",temp);
  72.        strcat(buf,strcat(temp," "));
  73.        }
  74.     return(strlen(buf));
  75. }
  76.  
  77. findcands()
  78. { char *pp; int i;
  79.  
  80.   strcpy(cands,"");
  81.   pp = buf;
  82.   while ((pp = strstr(pp,lastout))!=NULL)
  83.      { i = strlen(cands);
  84.        cands[i] = *(pp+order); cands[++i] = '\0';
  85.        pp++;
  86.        }
  87. }
  88.  
  89. restart()
  90. {
  91.   strncpy(lastout,buf,order);
  92.   linelen = order;
  93.   printf("\n%s",lastout);
  94. }
  95.  
  96.  
  97. main(int argc, char *argv[])
  98. {
  99.   randomize();
  100.  
  101.   if (argc<1 || (!(order = atoi(argv[1]))))
  102.      { puts("USAGE:  TRAV <order>   where <order> is an integer 2-10.");
  103.        exit(1); }
  104.  
  105.   bs = readbuf();
  106.  
  107.   restart();
  108.   while (kbhit()) getch();
  109.   while (!kbhit()) {
  110.      findcands();
  111.      if (cands[0] == '\0')
  112.        { restart(); findcands(); }
  113.      outtahere = cands[random(strlen(cands))];
  114.      putchar(outtahere);
  115.      lastout[order] = outtahere;
  116.      lastout[order+1] = '\0';
  117.      movmem(lastout+1,lastout,order+1);
  118.  
  119.      linelen++;
  120.      if (linelen > 55 && isspace(outtahere))
  121.        { puts(""); linelen = 0; }
  122.      }
  123. }  /* end of program file trav.c */
  124. -- 
  125. amlovell@phoenix.princeton.edu     ...since 1963.
  126.  
  127.  
  128.