home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / A / FIND / FINDUTIL.1 / FINDUTIL / findutils-4.1 / locate / bigram.c next >
Encoding:
C/C++ Source or Header  |  1994-10-07  |  3.2 KB  |  116 lines

  1. /* bigram -- list bigrams for locate
  2.    Copyright (C) 1994 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Usage: bigram < text > bigrams
  19.    Use `code' to encode a file using this output.
  20.  
  21.    Read a file from stdin and write out the bigrams (pairs of
  22.    adjacent characters), one bigram per line, to stdout.  To reduce
  23.    needless duplication in the output, it starts finding the
  24.    bigrams on each input line at the character where that line
  25.    first differs from the previous line (i.e., in the ASCII
  26.    remainder).  Therefore, the input should be sorted in order to
  27.    get the least redundant output.
  28.  
  29.    Written by James A. Woods <jwoods@adobe.com>.
  30.    Modified by David MacKenzie <djm@gnu.ai.mit.edu>.  */
  31.  
  32. #include <config.h>
  33. #include <stdio.h>
  34.  
  35. #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
  36. #include <string.h>
  37. #else
  38. #include <strings.h>
  39. #endif
  40.  
  41. #ifdef STDC_HEADERS
  42. #include <stdlib.h>
  43. #endif
  44. #include <sys/types.h>
  45.  
  46. char *xmalloc ();
  47.  
  48. /* The name this program was run with.  */
  49. char *program_name;
  50.  
  51. /* Return the length of the longest common prefix of strings S1 and S2. */
  52.  
  53. static int
  54. prefix_length (s1, s2)
  55.      char *s1, *s2;
  56. {
  57.   register char *start;
  58.  
  59.   for (start = s1; *s1 == *s2 && *s1 != '\0'; s1++, s2++)
  60.     ;
  61.   return s1 - start;
  62. }
  63.  
  64. void
  65. main (argc, argv)
  66.      int argc;
  67.      char **argv;
  68. {
  69.   char *path;            /* The current input entry.  */
  70.   char *oldpath;        /* The previous input entry.  */
  71.   size_t pathsize, oldpathsize;    /* Amounts allocated for them.  */
  72.   int line_len;            /* Length of input line.  */
  73.  
  74.   program_name = argv[0];
  75.  
  76.   pathsize = oldpathsize = 1026; /* Increased as necessary by getstr.  */
  77.   path = xmalloc (pathsize);
  78.   oldpath = xmalloc (oldpathsize);
  79.  
  80.   /* Set to anything not starting with a slash, to force the first
  81.      prefix count to 0.  */
  82.   strcpy (oldpath, " ");
  83.  
  84.   while ((line_len = getstr (&path, &pathsize, stdin, '\n', 0)) > 0)
  85.     {
  86.       register int count;    /* The prefix length.  */
  87.       register int j;        /* Index into input line.  */
  88.  
  89.       path[line_len - 1] = '\0'; /* Remove the newline. */
  90.  
  91.       /* Output bigrams in the remainder only. */
  92.       count = prefix_length (oldpath, path);
  93.       for (j = count; path[j] != '\0' && path[j + 1] != '\0'; j += 2)
  94.     {
  95.       putchar (path[j]);
  96.       putchar (path[j + 1]);
  97.       putchar ('\n');
  98.     }
  99.  
  100.       {
  101.     /* Swap path and oldpath and their sizes.  */
  102.     char *tmppath = oldpath;
  103.     size_t tmppathsize = oldpathsize;
  104.     oldpath = path;
  105.     oldpathsize = pathsize;
  106.     path = tmppath;
  107.     pathsize = tmppathsize;
  108.       }
  109.     }
  110.  
  111.   free (path);
  112.   free (oldpath);
  113.  
  114.   exit (0);
  115. }
  116.