r878: This is Cinelerra 2.1.
[cinelerra_cv/ct.git] / guicast / replace.C
blob917e7bf409be7cdccce8e9ffc7f320c1f1185721
1 #include <errno.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
7 #include "bcdisplayinfo.h"
8 #include "bchash.h"
9 #include "guicast.h"
12 class MWindow : public BC_Window
14 public:
15         MWindow(int x, int y)
16          : BC_Window("Replace", x, y, 320, 200, 0, 0)
17         {
18         }
20         int create_objects(char *input, char *output)
21         {
22                 int x = 10, y = 10;
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));
33                 x = get_w() - 100;
34                 add_subwindow(new BC_CancelButton(this));
35         }
37         BC_Title *title;
38         BC_TextBox *input_text, *output_text;
42 int main(int argc, char *argv[])
44         char input[1024], output[1024];
45         int result;
46         BC_Hash defaults("~/.replacerc");
48         input[0] = 0;
49         output[0] = 0;
50         defaults.load();
51         defaults.get("INPUT", input);
52         defaults.get("OUTPUT", output);
53         BC_DisplayInfo info;
54         
55         
56         MWindow window(info.get_abs_cursor_x(), info.get_abs_cursor_y());
57         window.create_objects(input, output);
58         result = window.run_window();
60         if(result == 0)
61         {
62                 strcpy(input, window.input_text->get_text());
63                 strcpy(output, window.output_text->get_text());
64                 while(--argc > 0)
65                 {
66                         FILE *file;
67                         char *buffer, *ptr1, *ptr2;
68                         long len;
70                         if(!(file = fopen(argv[argc], "r")))
71                         {
72                                 printf("open %s for reading: %s", argv[argc], strerror(errno));
73                                 continue;
74                         }
76                         fseek(file, 0, SEEK_END);
77                         len = ftell(file);
78                         fseek(file, 0, SEEK_SET);
80                         buffer = (char*)malloc(len);
81                         fread(buffer, 1, len, file);
82                         fclose(file);
84                         if(!(file = fopen(argv[argc], "w")))
85                         {
86                                 printf("open %s for writing: %s", argv[argc], strerror(errno));
87                                 continue;
88                         }
90                         ptr1 = buffer;
91                         do
92                         {
93                                 ptr2 = strstr(ptr1, input);
95                                 if(ptr2)
96                                 {
97                                         while(ptr1 < ptr2)
98                                                 fputc(*ptr1++, file);
100                                         ptr1 += strlen(input);
101                                         fprintf(file, "%s", output);
102                                 }
103                                 else
104                                         while(ptr1 < buffer + len)
105                                                 fputc(*ptr1++, file);
106                         }while(ptr2);
107                 }
108                 printf("Replaced ""%s"" with ""%s"".\n", input, output);
109         }
110         else
111                 printf("Cancelled.\n");
113         defaults.update("INPUT", input);
114         defaults.update("OUTPUT", output);
115         defaults.save();