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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
/* Functions for reading FILE*'s up to certain strings or characters. */
/* Copyright (C) 1997 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 <ifile.h>
#include <unistd.h> /* for SunOS 4.1.3_U1 - no SEEK_CUR in stdio.h */
#include <stdio.h>
#include <ctype.h> /* for tolower() */
/* Read characters from the file pointer FP until the string STRING is
found or EOF if reached. Return 0 if EOF was reached, 1 otherwise.
The search is case-insensitive. If 1 is returned, the file pointer
will be at the character after the last character in STRING. If
ONELINE is non-zero, insist that the string appear before a newline
character. If STRING is NULL or of zero length, scan until EOF. */
int
ifile_scan_fp_for_string (FILE *fp, const char *string, int oneline)
{
int byte; /* character read from the FP */
const char *string_ptr; /* a placeholder into STRING */
/* If STRING is NULL, scan forward to the end of the file. */
if (!string)
{
while (fgetc (fp) != EOF)
;
return 1;
}
/* If STRING is the empty string, return without scanning forward at all */
if (!string[0])
return 0;
/* Read forward until we find the first character of STRING. */
/* Make an initial newline in STRING match the beginning of the file. */
if (!(ftell (fp) == 0 && string[0] == '\n'))
{
again:
do
{
byte = fgetc (fp);
if (byte == EOF || (string[0] != '\n' && oneline && byte == '\n'))
return 0;
}
while (tolower (byte) != tolower (string[0]));
}
/* Step through the characters in STRING, starting all over again
if we encounter a mismatch. */
string_ptr = string+1;
while (*string_ptr)
{
byte = fgetc (fp);
if (byte == EOF || (oneline && byte == '\n'))
return 0;
/* Ignore Carriage-Return characters, so we can match MIME headers
like "\r\n\r\n" with a search STRING of "\n\n" */
if (byte == '\r')
continue;
if (tolower (byte) != tolower (*string_ptr))
/* A mismatch; start the search again. */
goto again;
/* Move on to next character in the pattern. */
string_ptr++;
}
/* Success! We found the string. */
return 1;
}
/* Read characters from FP into BUF until the character STOPCHAR is
reached. On success, returns the number of characters read. If
EOF is reached before reading the STOPCHAR, return the negative of
the number of characters read. If BUFLEN is reached before reading
the STOPCHAR, return 0. If NEGFLAG is 1, the sense of the test is
reversed. */
int
ifile_scan_fp_into_buffer_until_char (FILE *fp, char *buf, int buflen,
char stopchar, int negflag)
{
int byte;
int count = 0;
assert (buflen > 0 && buf);
while (buflen--)
{
byte = fgetc (fp);
if (byte == EOF)
{
buf[count] = '\0';
return -count;
}
if (negflag ? (byte != stopchar) : (byte == stopchar))
{
fseek (fp, -1, SEEK_CUR);
buf[count] = '\0';
return count;
}
buf[count++] = byte;
}
buf[count-1] = '\0';
return 0;
}
/* Read characters from FP into BUF until any of the characters in the
string STOPCHARS is reached. On success, returns the number of
characters read. If EOF is reached before reading any of the
STOPCHARS, return the negative of the number of characters read.
If BUFLEN is reached before reading the STOPCHAR, return 0.
If NEGFLAG is 1, the sense of the test is reversed. */
int
ifile_scan_fp_into_buffer_until_chars (FILE *fp, char *buf, int buflen,
const char *stopchars, int negflag)
{
int byte;
int count = 0;
assert (buflen > 0 && buf);
while (buflen--)
{
byte = fgetc (fp);
if (byte == EOF)
{
buf[count] = '\0';
return -count;
}
if (negflag
? (strchr (stopchars, byte) == 0)
: (strchr (stopchars, byte) != 0))
{
fseek (fp, -1, SEEK_CUR);
buf[count] = '\0';
return count;
}
buf[count++] = byte;
}
buf[count-1] = '\0';
return 0;
}
/* Read characters from FP into BUF until the string STOPSTR is
reached. On success, returns the number of characters read. If
EOF is reached before reading the STOPSTR, return the negative of
the number of characters read. If BUFLEN is reached before reading
the STOPCHAR, return 0. */
int
ifile_scan_fp_into_buffer_until_string (FILE *fp, char *buf, int buflen,
char* stopstr)
{
int byte; /* character read from the FP */
const char *stopstr_ptr; /* a placeholder into STOPSTR */
char *buf_ptr; /* a placeholder into BUF */
int count; /* the number of characters added to BUF */
if (!stopstr || !stopstr[0])
return 0;
count = 0;
buf_ptr = buf;
again:
/* Read forward until we find the first character of STRING. */
do
{
byte = fgetc (fp);
if (byte == EOF)
{
if (buf)
buf[count] = '\0';
return -count;
}
count++;
if (buf)
{
*buf_ptr++ = byte;
if (count >= buflen)
{
buf[buflen-1] = '\0';
return 0;
}
}
}
while (tolower (byte) != tolower (stopstr[0]));
/* Step through the characters in STRING, starting all over again
if we encounter a mismatch. */
for (stopstr_ptr = stopstr+1; *stopstr_ptr; stopstr_ptr++)
{
byte = fgetc (fp);
if (byte == EOF)
{
if (buf)
buf[count] = '\0';
return -count;
}
if (buf)
*buf_ptr++ = byte;
if (++count >= buflen)
{
if (buf)
buf[buflen-1] = '\0';
return 0;
}
if (tolower (byte) != tolower (*stopstr_ptr))
/* A mismatch; start the search again. */
goto again;
}
/* Success! We found the stopstr. */
count =- strlen (stopstr);
if (buf)
buf[count] = '\0';
return count;
}
|