home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 195_01 / kwic.c < prev    next >
Text File  |  1987-10-05  |  2KB  |  100 lines

  1. /* [KWIC.C of JUGPDS Vol.18]
  2. *****************************************************************
  3. *                                *
  4. *    Written by  Hakuo Katayose (JUG-CP/M No.179)        *
  5. *            49-114 Kawauchi-Sanjuunin-machi        *
  6. *            Sendai, Miyagi 980                          *
  7. *            Phone: 0222-61-3219                *
  8. *                                *
  9. *    Edited & tested by Y. Monma (JUG-C/M Disk Editor)       * 
  10. *                                *
  11. *****************************************************************
  12. */
  13.  
  14. /* kwic - make keyword in context index */
  15.  
  16. #include <stdio.h>
  17. #include <dio.h>
  18.  
  19. #define    FOLD    36    /* FOLD = '$' */
  20.  
  21. int    cc;
  22.  
  23. main(argc, argv)
  24. int    argc;
  25. char     **argv;
  26.  
  27. {
  28.     char    buf[MAXLINE];
  29.  
  30.     dioinit(&argc, argv);
  31.     if (argc <2) {
  32.         error("Usage: kwic <infile >outfile ^Z");
  33.         exit();
  34.     }
  35.     cc = 'a';
  36.     while (getlin(buf, MAXLINE) > 0)
  37.         putrot(buf);
  38.     dioflush();
  39. }
  40.  
  41.  
  42. /* putrot - create lines with keyword at front */
  43. putrot(buf)
  44. char buf[];
  45. {
  46.     int    i, t;
  47.  
  48.     for (i = 0; buf[i] != NEWLINE; i++) {
  49.         t = type(buf[i]);
  50.         if (t == LETTER || t == DIGIT) {
  51.             rotate(buf, i);
  52.             t = type(buf[i+1]);
  53.             for (; t == LETTER || t == DIGIT; t = type(buf[i+1]))
  54.                 i++;
  55.         }
  56.     }
  57. }
  58.  
  59.  
  60. /* rotate - output rotated line */
  61. rotate(buf, n)
  62. char buf[];
  63. {
  64.     int    i;
  65.  
  66.     for (i = n; buf[i] != NEWLINE; i++)
  67.         putchr(buf[i]);
  68.     putchr(FOLD);
  69.     for (i = 0; i < n; i++)
  70.         putchr(buf[i]);
  71.     putchr(NEWLINE);
  72. }
  73.  
  74. putchr(c)
  75. int    c;
  76. {
  77.     if (c == '\t')
  78.         c = BLANK;
  79.     if (c != BLANK || cc != BLANK)
  80.         putchar(c);
  81.     cc = c;
  82. }
  83.  
  84.  
  85. getlin(s, lim)
  86. char    *s;
  87. int    lim;
  88. {
  89.     char    *p;
  90.     int    c;
  91.  
  92.     p = s;
  93.     while(--lim > 0 && (c = getchar()) != EOF && c != NEWLINE)
  94.         *s++ = c;
  95.     if(c == NEWLINE)
  96.         *s++ = c;
  97.     *s = EOS;
  98.     return(s-p);
  99. }
  100.