00001 #include <errno.h>
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <string.h>
00005
00006
00007 #include "bcdisplayinfo.h"
00008 #include "defaults.h"
00009 #include "guicast.h"
00010
00011
00012 class MWindow : public BC_Window
00013 {
00014 public:
00015 MWindow(int x, int y)
00016 : BC_Window("Replace", x, y, 320, 200, 0, 0)
00017 {
00018 }
00019
00020 int create_objects(char *input, char *output)
00021 {
00022 int x = 10, y = 10;
00023
00024 add_subwindow(title = new BC_Title(x, y, "String to replace:"));
00025 y += title->get_h() + 10;
00026 add_subwindow(input_text = new BC_TextBox(x, y, 200, 1, input, 1));
00027 y += input_text->get_h() + 10;
00028 add_subwindow(title = new BC_Title(x, y, "Replacement string:"));
00029 y += title->get_h() + 10;
00030 add_subwindow(output_text = new BC_TextBox(x, y, 200, 1, output, 1));
00031 y += output_text->get_h() + 10;
00032 add_subwindow(new BC_OKButton(this));
00033 x = get_w() - 100;
00034 add_subwindow(new BC_CancelButton(this));
00035 }
00036
00037 BC_Title *title;
00038 BC_TextBox *input_text, *output_text;
00039 };
00040
00041
00042 int main(int argc, char *argv[])
00043 {
00044 char input[1024], output[1024];
00045 int result;
00046 Defaults defaults("~/.replacerc");
00047
00048 input[0] = 0;
00049 output[0] = 0;
00050 defaults.load();
00051 defaults.get("INPUT", input);
00052 defaults.get("OUTPUT", output);
00053 BC_DisplayInfo info;
00054
00055
00056 MWindow window(info.get_abs_cursor_x(), info.get_abs_cursor_y());
00057 window.create_objects(input, output);
00058 result = window.run_window();
00059
00060 if(result == 0)
00061 {
00062 strcpy(input, window.input_text->get_text());
00063 strcpy(output, window.output_text->get_text());
00064 while(--argc > 0)
00065 {
00066 FILE *file;
00067 char *buffer, *ptr1, *ptr2;
00068 long len;
00069
00070 if(!(file = fopen(argv[argc], "r")))
00071 {
00072 printf("open %s for reading: %s", argv[argc], strerror(errno));
00073 continue;
00074 }
00075
00076 fseek(file, 0, SEEK_END);
00077 len = ftell(file);
00078 fseek(file, 0, SEEK_SET);
00079
00080 buffer = (char*)malloc(len);
00081 fread(buffer, 1, len, file);
00082 fclose(file);
00083
00084 if(!(file = fopen(argv[argc], "w")))
00085 {
00086 printf("open %s for writing: %s", argv[argc], strerror(errno));
00087 continue;
00088 }
00089
00090 ptr1 = buffer;
00091 do
00092 {
00093 ptr2 = strstr(ptr1, input);
00094
00095 if(ptr2)
00096 {
00097 while(ptr1 < ptr2)
00098 fputc(*ptr1++, file);
00099
00100 ptr1 += strlen(input);
00101 fprintf(file, "%s", output);
00102 }
00103 else
00104 while(ptr1 < buffer + len)
00105 fputc(*ptr1++, file);
00106 }while(ptr2);
00107 }
00108 printf("Replaced ""%s"" with ""%s"".\n", input, output);
00109 }
00110 else
00111 printf("Cancelled.\n");
00112
00113 defaults.update("INPUT", input);
00114 defaults.update("OUTPUT", output);
00115 defaults.save();
00116
00117 }