Credit Andrew - update libaom from 3.4 to 3.8 requiring cmake of 3.7.2 or higher...
[goodguy/cinelerra.git] / cinelerra-5.1 / guicast / replace.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27
28 #include "bcdisplayinfo.h"
29 #include "bchash.h"
30 #include "guicast.h"
31
32
33 class MWindow : public BC_Window
34 {
35 public:
36         MWindow(int x, int y)
37          : BC_Window("Replace", x, y, 320, 200, 0, 0)
38         {
39         }
40
41         int create_objects(char *input, char *output)
42         {
43                 int x = 10, y = 10;
44
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));
54                 x = get_w() - 100;
55                 add_subwindow(new BC_CancelButton(this));
56         }
57
58         BC_Title *title;
59         BC_TextBox *input_text, *output_text;
60 };
61
62
63 int main(int argc, char *argv[])
64 {
65         char input[1024], output[1024];
66         int result;
67         BC_Hash defaults("~/.replacerc");
68
69         input[0] = 0;
70         output[0] = 0;
71         defaults.load();
72         defaults.get("INPUT", input);
73         defaults.get("OUTPUT", output);
74         BC_DisplayInfo info;
75
76
77         MWindow window(info.get_abs_cursor_x(), info.get_abs_cursor_y());
78         window.create_objects(input, output);
79         result = window.run_window();
80
81         if(result == 0)
82         {
83                 strcpy(input, window.input_text->get_text());
84                 strcpy(output, window.output_text->get_text());
85                 while(--argc > 0)
86                 {
87                         FILE *file;
88                         char *buffer, *ptr1, *ptr2;
89                         long len;
90
91                         if(!(file = fopen(argv[argc], "r")))
92                         {
93                                 printf("open %s for reading: %s", argv[argc], strerror(errno));
94                                 continue;
95                         }
96
97                         fseek(file, 0, SEEK_END);
98                         len = ftell(file);
99                         fseek(file, 0, SEEK_SET);
100
101                         buffer = (char*)malloc(len);
102                         fread(buffer, 1, len, file);
103                         fclose(file);
104
105                         if(!(file = fopen(argv[argc], "w")))
106                         {
107                                 printf("open %s for writing: %s", argv[argc], strerror(errno));
108                                 continue;
109                         }
110
111                         ptr1 = buffer;
112                         do
113                         {
114                                 ptr2 = strstr(ptr1, input);
115
116                                 if(ptr2)
117                                 {
118                                         while(ptr1 < ptr2)
119                                                 fputc(*ptr1++, file);
120
121                                         ptr1 += strlen(input);
122                                         fprintf(file, "%s", output);
123                                 }
124                                 else
125                                         while(ptr1 < buffer + len)
126                                                 fputc(*ptr1++, file);
127                         }while(ptr2);
128                 }
129                 printf("Replaced ""%s"" with ""%s"".\n", input, output);
130         }
131         else
132                 printf("Cancelled.\n");
133
134         defaults.update("INPUT", input);
135         defaults.update("OUTPUT", output);
136         defaults.save();
137
138 }