home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume5 / trav.tc < prev    next >
Encoding:
Text File  |  1989-02-03  |  3.5 KB  |  124 lines

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