locate.bigram.c (8874) | locate.bigram.c (17592) |
---|---|
1/* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * James A. Woods. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 39 unchanged lines hidden (view full) --- 48 * bigram < text > bigrams 49 * 50 * List bigrams for 'updatedb' script. 51 * Use 'code' to encode a file using this output. 52 */ 53 54#include <stdio.h> 55#include <sys/param.h> /* for MAXPATHLEN */ | 1/* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * James A. Woods. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 39 unchanged lines hidden (view full) --- 48 * bigram < text > bigrams 49 * 50 * List bigrams for 'updatedb' script. 51 * Use 'code' to encode a file using this output. 52 */ 53 54#include <stdio.h> 55#include <sys/param.h> /* for MAXPATHLEN */ |
56#include <string.h> /* memchr */ 57#include "locate.h" |
|
56 | 58 |
57char buf1[MAXPATHLEN] = " "; 58char buf2[MAXPATHLEN]; | 59u_char buf1[MAXPATHLEN] = " "; 60u_char buf2[MAXPATHLEN]; 61unsigned int bigram[UCHAR_MAX][UCHAR_MAX]; |
59 | 62 |
60main ( ) | 63 64void main ( ) |
61{ | 65{ |
62 register char *cp; 63 register char *oldpath = buf1, *path = buf2; | 66 register u_char *cp; 67 register u_char *oldpath = buf1, *path = buf2; 68 register int i, j; |
64 | 69 |
70 /* init bigram buffer */ 71 for (i = 0; i < UCHAR_MAX; i++) 72 for (j = 0; j < UCHAR_MAX; j++) 73 bigram[i][j] = 0; 74 |
|
65 while ( fgets ( path, sizeof(buf2), stdin ) != NULL ) { 66 | 75 while ( fgets ( path, sizeof(buf2), stdin ) != NULL ) { 76 |
77 /* skip empty lines */ 78 if (*path == '\n') 79 continue; 80 81 /* Squelch characters that would botch the decoding. */ 82 for (cp = path; *cp != NULL; cp++) { 83 /* chop newline */ 84 if (*cp == '\n') 85 *cp = NULL; 86 /* range */ 87 else if (*cp < ASCII_MIN || *cp > ASCII_MAX) 88 *cp = '?'; 89 } 90 91 |
|
67 /* skip longest common prefix */ | 92 /* skip longest common prefix */ |
68 for ( cp = path; *cp == *oldpath; cp++, oldpath++ ) 69 if ( *oldpath == NULL ) 70 break; | 93 for (cp = path; *cp == *oldpath && *cp; cp++, oldpath++); 94 |
71 /* 72 * output post-residue bigrams only 73 */ | 95 /* 96 * output post-residue bigrams only 97 */ |
98 99 /* check later for boundary */ |
|
74 while ( *cp != NULL && *(cp + 1) != NULL ) { | 100 while ( *cp != NULL && *(cp + 1) != NULL ) { |
75 putchar ( *cp++ ); 76 putchar ( *cp++ ); 77 putchar ( '\n' ); | 101 bigram[*cp][*(cp+1)]++; 102 cp += 2; |
78 } | 103 } |
104 |
|
79 if ( path == buf1 ) /* swap pointers */ 80 path = buf2, oldpath = buf1; 81 else 82 path = buf1, oldpath = buf2; 83 } | 105 if ( path == buf1 ) /* swap pointers */ 106 path = buf2, oldpath = buf1; 107 else 108 path = buf1, oldpath = buf2; 109 } |
110 111 /* output, boundary check */ 112 for (i = ASCII_MIN; i <= ASCII_MAX; i++) 113 for (j = ASCII_MIN; j <= ASCII_MAX; j++) 114 if (bigram[i][j] != 0) 115 fprintf(stdout, "%4d %c%c\n", 116 bigram[i][j], i, j); |
|
84} | 117} |