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