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