7 #include "bcdisplayinfo.h"
12 class MWindow : public BC_Window
16 : BC_Window("Replace", x, y, 320, 200, 0, 0)
20 int create_objects(char *input, char *output)
24 add_subwindow(title = new BC_Title(x, y, "String to replace:"));
25 y += title->get_h() + 10;
26 add_subwindow(input_text = new BC_TextBox(x, y, 200, 1, input, 1));
27 y += input_text->get_h() + 10;
28 add_subwindow(title = new BC_Title(x, y, "Replacement string:"));
29 y += title->get_h() + 10;
30 add_subwindow(output_text = new BC_TextBox(x, y, 200, 1, output, 1));
31 y += output_text->get_h() + 10;
32 add_subwindow(new BC_OKButton(this));
34 add_subwindow(new BC_CancelButton(this));
38 BC_TextBox *input_text, *output_text;
42 int main(int argc, char *argv[])
44 char input[1024], output[1024];
46 BC_Hash defaults("~/.replacerc");
51 defaults.get("INPUT", input);
52 defaults.get("OUTPUT", output);
56 MWindow window(info.get_abs_cursor_x(), info.get_abs_cursor_y());
57 window.create_objects(input, output);
58 result = window.run_window();
62 strcpy(input, window.input_text->get_text());
63 strcpy(output, window.output_text->get_text());
67 char *buffer, *ptr1, *ptr2;
70 if(!(file = fopen(argv[argc], "r")))
72 printf("open %s for reading: %s", argv[argc], strerror(errno));
76 fseek(file, 0, SEEK_END);
78 fseek(file, 0, SEEK_SET);
80 buffer = (char*)malloc(len);
81 fread(buffer, 1, len, file);
84 if(!(file = fopen(argv[argc], "w")))
86 printf("open %s for writing: %s", argv[argc], strerror(errno));
93 ptr2 = strstr(ptr1, input);
100 ptr1 += strlen(input);
101 fprintf(file, "%s", output);
104 while(ptr1 < buffer + len)
105 fputc(*ptr1++, file);
108 printf("Replaced ""%s"" with ""%s"".\n", input, output);
111 printf("Cancelled.\n");
113 defaults.update("INPUT", input);
114 defaults.update("OUTPUT", output);