4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "bcdisplayinfo.h"
33 class MWindow : public BC_Window
37 : BC_Window("Replace", x, y, 320, 200, 0, 0)
41 int create_objects(char *input, char *output)
45 add_subwindow(title = new BC_Title(x, y, "String to replace:"));
46 y += title->get_h() + 10;
47 add_subwindow(input_text = new BC_TextBox(x, y, 200, 1, input, 1));
48 y += input_text->get_h() + 10;
49 add_subwindow(title = new BC_Title(x, y, "Replacement string:"));
50 y += title->get_h() + 10;
51 add_subwindow(output_text = new BC_TextBox(x, y, 200, 1, output, 1));
52 y += output_text->get_h() + 10;
53 add_subwindow(new BC_OKButton(this));
55 add_subwindow(new BC_CancelButton(this));
59 BC_TextBox *input_text, *output_text;
63 int main(int argc, char *argv[])
65 char input[1024], output[1024];
67 BC_Hash defaults("~/.replacerc");
72 defaults.get("INPUT", input);
73 defaults.get("OUTPUT", output);
77 MWindow window(info.get_abs_cursor_x(), info.get_abs_cursor_y());
78 window.create_objects(input, output);
79 result = window.run_window();
83 strcpy(input, window.input_text->get_text());
84 strcpy(output, window.output_text->get_text());
88 char *buffer, *ptr1, *ptr2;
91 if(!(file = fopen(argv[argc], "r")))
93 printf("open %s for reading: %s", argv[argc], strerror(errno));
97 fseek(file, 0, SEEK_END);
99 fseek(file, 0, SEEK_SET);
101 buffer = (char*)malloc(len);
102 fread(buffer, 1, len, file);
105 if(!(file = fopen(argv[argc], "w")))
107 printf("open %s for writing: %s", argv[argc], strerror(errno));
114 ptr2 = strstr(ptr1, input);
119 fputc(*ptr1++, file);
121 ptr1 += strlen(input);
122 fprintf(file, "%s", output);
125 while(ptr1 < buffer + len)
126 fputc(*ptr1++, file);
129 printf("Replaced ""%s"" with ""%s"".\n", input, output);
132 printf("Cancelled.\n");
134 defaults.update("INPUT", input);
135 defaults.update("OUTPUT", output);