File: istext.c

package info (click to toggle)
bow 19991122-4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,544 kB
  • ctags: 2,987
  • sloc: ansic: 38,660; lisp: 1,072; makefile: 594; perl: 492; yacc: 149; sh: 91
file content (197 lines) | stat: -rw-r--r-- 5,925 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* istext.c - test if a file contains text or not. */

/* Copyright (C) 1997, 1998, 1999 Andrew McCallum

   Written by:  Andrew Kachites McCallum <mccallum@cs.cmu.edu>

   This file is part of the Bag-Of-Words Library, `libbow'.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License
   as published by the Free Software Foundation, version 2.
   
   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA */

#include <bow/libbow.h>
#include <ctype.h>		/* for isprint(), etc. */

/* The percentage of characters that must be text-like in order for
   us to say this is a text file. */
#define TEXT_PRINTABLE_PERCENT 95

#define NUM_TEST_CHARS 4096

/* bow_*_is_text() always returns `yes'.  This is useful for Japanese
   byte codes. */
int bow_is_text_always_yes = 0;

/* Examine the first NUM_TEST_CHARS characters of `fp', and return a 
   non-zero value iff TEXT_PRINTABLE_PERCENT of them are printable. */
int
bow_fp_is_text (FILE *fp)
{
  char buf[NUM_TEST_CHARS];
  int num_read;
  int num_printable = 0;
  int num_spaces = 0;
  int fpos;
  int i;

  if (bow_is_text_always_yes)
    return 1;

  fpos = ftell (fp);
  num_read = fread (buf, sizeof (char), NUM_TEST_CHARS, fp);
  fseek (fp, fpos, SEEK_SET);

  for (i = 0; i < num_read; i++)
    {
      if (isprint (buf[i]) || isspace (buf[i]))
	num_printable++;
      if (isspace (buf[i]))
	num_spaces++;
    }

  if (!(num_read > 0 
	&& (((100 * num_printable) / num_read) > TEXT_PRINTABLE_PERCENT)))
    return 0;

  if (bow_istext_avoid_uuencode)
    {
      int num_newlines;
      static int NUM_LINE_LENGTHS = NUM_TEST_CHARS;
      int line_lengths[NUM_LINE_LENGTHS];
      int line_length_histogram[NUM_LINE_LENGTHS];
      int max_line_length_histogram_height;
      int max_line_length_histogram_length;

      /* Test for uuencoded blocks by seeing if over 1/3 of the lines have
	 identical length. */
      for (i = 0, num_newlines = 0, line_lengths[num_newlines] = 0;
	   i < num_read;
	   i++)
	{
	  if (buf[i] == '\n')
	    {
	      num_newlines++;
	      assert (num_newlines < NUM_LINE_LENGTHS);
	      line_lengths[num_newlines] = 0;
	    }
	  else
	    {
	      line_lengths[num_newlines]++;
	    }
	}
      for (i = 0; i < NUM_LINE_LENGTHS; i++)
	line_length_histogram[i] = 0;
      for (i = 0; i < num_newlines; i++)
	line_length_histogram[line_lengths[i]]++;
      max_line_length_histogram_height = line_length_histogram[0];
      max_line_length_histogram_length = 0;
      for (i = 1; i < NUM_LINE_LENGTHS; i++)
	if (max_line_length_histogram_height < line_length_histogram[i])
	  {
	    max_line_length_histogram_height = line_length_histogram[i];
	    max_line_length_histogram_length = i;
	  }
      /* If over a 1/2 of the lines have the same height, and there
	 aren't many spaces in the text, and the line length with the
	 most lines is greater than or equal to 50, then this file
	 probably contains a uuencoded block. */
      if (max_line_length_histogram_height > num_newlines / 2
	  && num_spaces < num_read / 10
	  && max_line_length_histogram_length >= 50
	  && max_line_length_histogram_length <= 80)
	return 0;
    }

  return 1;
}


/* Examine the first NUM_TEST_CHARS characters of STR, and return a 
   non-zero value iff TEXT_PRINTABLE_PERCENT of them are printable. */
int
bow_str_is_text (char *buf)
{
  int num_read;
  int num_printable = 0;
  int num_spaces = 0;
  int i;

  if (bow_is_text_always_yes)
    return 1;

  /* Loop until we found the end or until we hit our limit */
  for (i = 0; buf[i] && i < NUM_TEST_CHARS; i++)
    {
      if (isprint (buf[i]) || isspace (buf[i]))
	num_printable++;
      if (isspace (buf[i]))
	num_spaces++;
    }

  num_read = i;

  if (!(num_read > 0 
	&& (((100 * num_printable) / num_read) > TEXT_PRINTABLE_PERCENT)))
    return 0;

  if (bow_istext_avoid_uuencode)
    {
      int num_newlines;
      static int NUM_LINE_LENGTHS = NUM_TEST_CHARS;
      int line_lengths[NUM_LINE_LENGTHS];
      int line_length_histogram[NUM_LINE_LENGTHS];
      int max_line_length_histogram_height;
      int max_line_length_histogram_length;

      /* Test for uuencoded blocks by seeing if over 1/3 of the lines have
	 identical length. */
      for (i = 0, num_newlines = 0, line_lengths[num_newlines] = 0;
	   i < num_read;
	   i++)
	{
	  if (buf[i] == '\n')
	    {
	      num_newlines++;
	      assert (num_newlines < NUM_LINE_LENGTHS);
	      line_lengths[num_newlines] = 0;
	    }
	  else
	    {
	      line_lengths[num_newlines]++;
	    }
	}
      for (i = 0; i < NUM_LINE_LENGTHS; i++)
	line_length_histogram[i] = 0;
      for (i = 0; i < num_newlines; i++)
	line_length_histogram[line_lengths[i]]++;
      max_line_length_histogram_height = line_length_histogram[0];
      max_line_length_histogram_length = 0;
      for (i = 1; i < NUM_LINE_LENGTHS; i++)
	if (max_line_length_histogram_height < line_length_histogram[i])
	  {
	    max_line_length_histogram_height = line_length_histogram[i];
	    max_line_length_histogram_length = i;
	  }
      /* If over a 1/2 of the lines have the same height, and there
	 aren't many spaces in the text, and the line length with the
	 most lines is greater than or equal to 50, then this file
	 probably contains a uuencoded block. */
      if (max_line_length_histogram_height > num_newlines / 2
	  && num_spaces < num_read / 10
	  && max_line_length_histogram_length >= 50
	  && max_line_length_histogram_length <= 80)
	return 0;
    }

  return 1;
}