equal
deleted
inserted
replaced
6 */ |
6 */ |
7 #include <stdio.h> |
7 #include <stdio.h> |
8 #include <assert.h> |
8 #include <assert.h> |
9 #include <string.h> |
9 #include <string.h> |
10 |
10 |
|
11 #include "sreplace.h" |
|
12 |
11 enum { |
13 enum { |
12 BUFFER_SIZE = 2048 |
14 BUFFER_SIZE = 2048 |
13 }; |
15 }; |
14 |
16 |
15 static FILE * input, * output; |
17 static FILE * input, * output; |
16 |
|
17 struct String |
|
18 { |
|
19 char *ptr; |
|
20 int length; |
|
21 }; |
|
22 |
18 |
23 struct CmpState |
19 struct CmpState |
24 { |
20 { |
25 struct String *str; |
21 struct String *str; |
26 int matched; |
22 int matched; |
239 static void show_usage(const char *pname) |
235 static void show_usage(const char *pname) |
240 { |
236 { |
241 printf("usage: %s OLD_STR NEW_STR\n", pname); |
237 printf("usage: %s OLD_STR NEW_STR\n", pname); |
242 } |
238 } |
243 |
239 |
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) |
240 static void process_parameters(int argc, char **argv) |
284 { |
241 { |
285 if (argc != 3) |
242 if (argc != 3) |
286 { |
243 { |
287 show_usage(argv[0]); |
244 show_usage(argv[0]); |
289 } |
246 } |
290 |
247 |
291 old_str.ptr = argv[1]; |
248 old_str.ptr = argv[1]; |
292 new_str.ptr = argv[2]; |
249 new_str.ptr = argv[2]; |
293 |
250 |
294 parse_backslashes(old_str.ptr); |
251 old_str.length = parse_backslashes(old_str.ptr); |
295 parse_backslashes(new_str.ptr); |
252 fprintf(stderr, "OLD: "); |
296 |
253 print_hex(stderr, &old_str); |
297 old_str.length = strlen(old_str.ptr); |
254 fprintf(stderr, "\n"); |
298 new_str.length = strlen(new_str.ptr); |
255 |
|
256 new_str.length = parse_backslashes(new_str.ptr); |
|
257 fprintf(stderr, "NEW: "); |
|
258 print_hex(stderr, &new_str); |
|
259 fprintf(stderr, "\n"); |
299 } |
260 } |
300 |
261 |
301 int main(int argc, char ** argv) |
262 int main(int argc, char ** argv) |
302 { |
263 { |
303 process_parameters(argc, argv); |
264 process_parameters(argc, argv); |