author | lbatlle@npdl268.bpo.hp.com |
Tue, 29 May 2007 18:50:35 +0200 | |
changeset 5 | 18c4f565e6d8 |
parent 4 | 576167dd9de2 |
child 7 | fcde17ef6af6 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5 | 2 |
Stream Replace - replaces sequences of bytes in FILE streams |
2 | 3 |
Copyright (C) 2007 LluĂs Batlle i Rossell |
4 |
||
5 |
Please find the license in the provided COPYING file. |
|
6 |
*/ |
|
0 | 7 |
#include <stdio.h> |
8 |
#include <assert.h> |
|
9 |
#include <string.h> |
|
10 |
||
3
edae5c16989e
Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents:
0
diff
changeset
|
11 |
enum { |
edae5c16989e
Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents:
0
diff
changeset
|
12 |
BUFFER_SIZE = 2048 |
edae5c16989e
Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents:
0
diff
changeset
|
13 |
}; |
0 | 14 |
|
15 |
static FILE * input, * output; |
|
16 |
||
17 |
struct String |
|
18 |
{ |
|
19 |
char *ptr; |
|
20 |
int length; |
|
21 |
}; |
|
22 |
||
23 |
struct CmpState |
|
24 |
{ |
|
25 |
struct String *str; |
|
26 |
int matched; |
|
27 |
}; |
|
28 |
||
29 |
struct String old_str; |
|
30 |
struct String new_str; |
|
31 |
struct CmpState cmp_state; |
|
32 |
||
33 |
/* Buffer: |
|
34 |
[F] - read |
|
35 |
[F] - read_cmp |
|
36 |
[F] |
|
37 |
[F] - filled |
|
38 |
[ ] |
|
39 |
[ ] |
|
40 |
||
41 |
In the circular sense: |
|
42 |
read <= read_cmp < filled |
|
43 |
*/ |
|
44 |
struct Buffer |
|
45 |
{ |
|
3
edae5c16989e
Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents:
0
diff
changeset
|
46 |
unsigned char *base; |
0 | 47 |
int filled; |
48 |
int read; |
|
49 |
int read_cmp; |
|
50 |
int size; |
|
51 |
}; |
|
52 |
||
53 |
static int get_buffer_space(struct Buffer *b, int start, int end) |
|
54 |
{ |
|
55 |
int space; |
|
56 |
space = end - start + b->size; |
|
57 |
if (space > b->size) |
|
58 |
space -= b->size; |
|
59 |
||
60 |
return space; |
|
61 |
} |
|
62 |
||
63 |
static int min(int a, int b) |
|
64 |
{ |
|
65 |
if (a < b) |
|
66 |
return a; |
|
67 |
return b; |
|
68 |
} |
|
69 |
||
70 |
static int one_before(struct Buffer *b, int pos) |
|
71 |
{ |
|
72 |
if (pos == 0) |
|
73 |
return b->size - 1; |
|
74 |
||
75 |
return pos - 1; |
|
76 |
} |
|
77 |
||
78 |
/* This should be called without any exceeding size */ |
|
79 |
static int file_to_buffer(struct Buffer *b, int size) |
|
80 |
{ |
|
81 |
int blocksize; |
|
82 |
int res; |
|
83 |
||
84 |
blocksize = min(b->size - b->filled, size); |
|
85 |
if (blocksize > 0) |
|
86 |
{ |
|
87 |
res = fread(b->base + b->filled, 1, blocksize, input); |
|
3
edae5c16989e
Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents:
0
diff
changeset
|
88 |
/*res = read(0, b->base + b->filled, blocksize);*/ |
0 | 89 |
/*fprintf(stderr, "fread1 %i bytes (should %i).\n", res, blocksize);*/ |
90 |
size -= res; |
|
91 |
b->filled = (b->filled + res) % b->size; |
|
92 |
||
93 |
/* Check EOF */ |
|
94 |
if (res == 0) |
|
95 |
return 0; |
|
96 |
} |
|
97 |
||
98 |
if (size > 0 && res == blocksize) |
|
99 |
{ |
|
100 |
blocksize = min(b->read - 1, size); |
|
101 |
res = fread(b->base, 1, blocksize, input); |
|
3
edae5c16989e
Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents:
0
diff
changeset
|
102 |
/*res = read(0, b->base, blocksize);*/ |
0 | 103 |
/*fprintf(stderr, "fread2 %i bytes.\n", res);*/ |
104 |
size -= res; |
|
105 |
b->filled = (b->filled + res) % b->size; |
|
106 |
} |
|
107 |
return 1; |
|
108 |
} |
|
109 |
||
110 |
static int file_read_more(struct Buffer *b) |
|
111 |
{ |
|
112 |
int can_read; |
|
113 |
int res; |
|
114 |
||
115 |
can_read = get_buffer_space(b, b->filled, b->read - 1); |
|
116 |
||
117 |
/*fprintf(stderr, "file_to_buffer %i\n", can_read);*/ |
|
118 |
||
119 |
return file_to_buffer(b, can_read); |
|
120 |
} |
|
121 |
||
122 |
/* pos will be read or read_cmp, depending on |
|
123 |
what to read */ |
|
124 |
static int can_read_from_buffer(struct Buffer *b, int pos) |
|
125 |
{ |
|
126 |
/* |
|
127 |
In the circular sense: |
|
128 |
read <= read_cmp < filled |
|
129 |
*/ |
|
130 |
if (pos != b->filled) |
|
131 |
return 1; |
|
132 |
else |
|
133 |
return 0; |
|
134 |
} |
|
135 |
||
136 |
static int getc_real(struct Buffer *b) |
|
137 |
{ |
|
138 |
int a; |
|
139 |
||
140 |
if (!can_read_from_buffer(b, b->read)) |
|
141 |
a = EOF; |
|
142 |
else |
|
143 |
{ |
|
144 |
a = *(b->base + b->read); |
|
145 |
b->read += 1; |
|
146 |
if (b->read == b->size) |
|
147 |
b->read = 0; |
|
148 |
} |
|
149 |
/*fprintf(stderr, "getc_real %x\n", a);*/ |
|
150 |
||
151 |
return a; |
|
152 |
} |
|
153 |
||
154 |
static int getc_cmp(struct Buffer *b) |
|
155 |
{ |
|
156 |
int a; |
|
157 |
if (!can_read_from_buffer(b, b->read_cmp)) |
|
158 |
a = EOF; |
|
159 |
else |
|
160 |
{ |
|
161 |
a = *(b->base + b->read_cmp); |
|
162 |
b->read_cmp += 1; |
|
163 |
if (b->read_cmp == b->size) |
|
164 |
b->read_cmp = 0; |
|
165 |
} |
|
166 |
/*fprintf(stderr, "getc_cmp %x\n", a);*/ |
|
167 |
||
168 |
return a; |
|
169 |
} |
|
170 |
||
171 |
/* This will not be moved farer than read_cmp */ |
|
172 |
static void read_advance(struct Buffer *b, int adv) |
|
173 |
{ |
|
174 |
int i; |
|
175 |
for (i=0; i < adv; ++i) |
|
176 |
getc_real(b); |
|
177 |
}; |
|
178 |
||
179 |
static void process_buffer(struct Buffer *b) |
|
180 |
{ |
|
181 |
int c; |
|
182 |
||
183 |
while (1) |
|
184 |
{ |
|
185 |
c = getc_cmp(b); |
|
186 |
||
187 |
if (c == EOF) |
|
188 |
break; |
|
189 |
||
190 |
if (cmp_state.str->ptr[cmp_state.matched] == (char) c) |
|
191 |
{ |
|
192 |
cmp_state.matched++; |
|
193 |
if (cmp_state.matched == cmp_state.str->length) |
|
194 |
{ |
|
195 |
fwrite(new_str.ptr, 1, new_str.length, output); |
|
3
edae5c16989e
Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents:
0
diff
changeset
|
196 |
/*write(1, new_str.ptr, new_str.length);*/ |
0 | 197 |
read_advance(b, cmp_state.matched); |
198 |
cmp_state.matched = 0; |
|
199 |
} |
|
200 |
} else |
|
201 |
{ |
|
202 |
int c; |
|
203 |
c = getc_real(b); /* Will not be EOF. read_cmp is forwarder. */ |
|
204 |
fputc(c, output); |
|
3
edae5c16989e
Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents:
0
diff
changeset
|
205 |
/*write(1, &c, 1);*/ |
0 | 206 |
cmp_state.matched = 0; |
207 |
b->read_cmp = b->read; |
|
208 |
} |
|
209 |
} |
|
210 |
} |
|
211 |
||
212 |
static void end_buffer(struct Buffer *b) |
|
213 |
{ |
|
214 |
int c; |
|
215 |
while((c = getc_real(b)) != EOF) |
|
216 |
fputc(c, output); |
|
3
edae5c16989e
Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents:
0
diff
changeset
|
217 |
/*write(1, &c, 1);*/ |
0 | 218 |
} |
219 |
||
220 |
static void loop() |
|
221 |
{ |
|
222 |
struct Buffer b; |
|
223 |
||
3
edae5c16989e
Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents:
0
diff
changeset
|
224 |
b.base = (unsigned char *) malloc(BUFFER_SIZE); |
0 | 225 |
b.read = 0; |
226 |
b.read_cmp = 0; |
|
227 |
b.filled = 0; |
|
3
edae5c16989e
Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents:
0
diff
changeset
|
228 |
b.size = BUFFER_SIZE; |
0 | 229 |
|
230 |
while(file_read_more(&b) > 0) |
|
231 |
{ |
|
232 |
int c; |
|
233 |
process_buffer(&b); |
|
234 |
} |
|
235 |
||
236 |
end_buffer(&b);; |
|
237 |
} |
|
238 |
||
239 |
static void show_usage(const char *pname) |
|
240 |
{ |
|
241 |
printf("usage: %s OLD_STR NEW_STR\n", pname); |
|
242 |
} |
|
243 |
||
244 |
static void parse_backslashes(char *str) |
|
245 |
{ |
|
246 |
int was_backslash = 0; |
|
247 |
char *write_str = str; |
|
248 |
||
249 |
while (*str != 0) |
|
250 |
{ |
|
251 |
if (*str == '\\') |
|
252 |
was_backslash = 1; |
|
253 |
else |
|
254 |
{ |
|
255 |
if (was_backslash) |
|
256 |
{ |
|
257 |
switch(*str) |
|
258 |
{ |
|
259 |
case '\\': |
|
260 |
*(write_str++) = '\\'; |
|
261 |
break; |
|
262 |
case 'n': |
|
263 |
*(write_str++) = '\n'; |
|
264 |
break; |
|
265 |
case 't': |
|
266 |
*(write_str++) = '\t'; |
|
267 |
break; |
|
268 |
default: |
|
269 |
*(write_str++) = *str; |
|
270 |
} |
|
271 |
} |
|
272 |
else |
|
273 |
{ |
|
274 |
*(write_str++) = *str; |
|
275 |
} |
|
276 |
was_backslash = 0; |
|
277 |
} |
|
278 |
++str; |
|
279 |
} |
|
280 |
*(write_str++) = '\0'; |
|
281 |
} |
|
282 |
||
283 |
static void process_parameters(int argc, char **argv) |
|
284 |
{ |
|
285 |
if (argc != 3) |
|
286 |
{ |
|
287 |
show_usage(argv[0]); |
|
288 |
exit(1); |
|
289 |
} |
|
290 |
||
291 |
old_str.ptr = argv[1]; |
|
292 |
new_str.ptr = argv[2]; |
|
293 |
||
294 |
parse_backslashes(old_str.ptr); |
|
295 |
parse_backslashes(new_str.ptr); |
|
296 |
||
297 |
old_str.length = strlen(old_str.ptr); |
|
298 |
new_str.length = strlen(new_str.ptr); |
|
299 |
} |
|
300 |
||
301 |
int main(int argc, char ** argv) |
|
302 |
{ |
|
303 |
process_parameters(argc, argv); |
|
304 |
||
305 |
cmp_state.str = &old_str; |
|
306 |
cmp_state.matched = 0; |
|
307 |
||
308 |
input = stdin; |
|
309 |
output = stdout; |
|
310 |
||
311 |
loop(); |
|
3
edae5c16989e
Fixed a EOF problem (char -> unsigned char).
lbatlle@npdl268.bpo.hp.com
parents:
0
diff
changeset
|
312 |
return 0; |
0 | 313 |
} |