clip fixes, ladspa build fixes
[goodguy/history.git] / cinelerra-5.1 / guicast / bootstrap.c
1 #include <errno.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 // Bootstrap for themes.
7
8 // By default, concatenates all the resources and a table of contents
9 // into an object file with objcopy.
10
11 // Run nm <object> to get the symbols created by bootstrap.
12
13 // The user must make extern variable declarations like
14 // extern unsigned char _binary_myobject_data_start[];
15 // and link the object to get at the symbols.
16
17 // Pass the symbol like
18 // _binary_destname_o_start
19 // to BC_Theme::set_data to set an image table for a theme.
20
21
22 // Because of the need for runtime compilation of source files for OpenGL,
23 // it now supports loading a single resource and omitting the table of contents.
24
25
26
27
28 // Usage: bootstrap <dest object> <resource> * n
29
30
31
32
33 void append_contents(char *path,
34         int data_offset,
35         char *buffer,
36         int *buffer_size)
37 {
38         char string[1024];
39         int i, j = 0;
40
41         for(i = strlen(path) - 1;
42                 i > 0 && path[i] && path[i] != '/';
43                 i--)
44                 ;
45
46         if(path[i] == '/') i++;
47
48         for(j = 0; path[i] != 0; i++, j++)
49                 string[j] = path[i];
50
51         string[j] = 0;
52
53         strcpy(buffer + *buffer_size, string);
54
55         *buffer_size += strlen(string) + 1;
56
57         *(int*)(buffer + *buffer_size) = data_offset;
58         *buffer_size += sizeof(int);
59 }
60
61 int main(int argc, char *argv[])
62 {
63         FILE *dest;
64         FILE *src;
65         int i;
66         char *contents_buffer;
67         int contents_size = 0;
68         char *data_buffer;
69         int data_size = 0;
70         int data_offset = 0;
71         char temp_path[1024];
72         char out_path[1024];
73         char *ptr;
74         char system_command[1024];
75         int current_arg = 1;
76         int binary_mode = 0;
77         int string_mode = 0;
78
79         if(argc < 3)
80         {
81                 fprintf(stderr, "Usage: bootstrap [-b] <dest object> <resource> * n\n");
82                 fprintf(stderr, "-b - omit table of contents just store resource.\n");
83                 fprintf(stderr, "-s - omit table of contents but append 0 to resource.\n");
84                 exit(1);
85         }
86
87         if(!strcmp(argv[current_arg], "-b"))
88         {
89                 binary_mode = 1;
90                 current_arg++;
91         }
92
93         if(!strcmp(argv[current_arg], "-s"))
94         {
95                 string_mode = 1;
96                 current_arg++;
97         }
98
99 // Make object filename
100         strcpy(temp_path, argv[current_arg]);
101         strcpy(out_path, argv[current_arg++]);
102         ptr = strchr(temp_path, '.');
103         if(ptr)
104         {
105                 *ptr = 0;
106         }
107         else
108         {
109                 fprintf(stderr, "Dest path must end in .o\n");
110                 exit(1);
111         }
112
113         if(!(dest = fopen(temp_path, "w")))
114         {
115                 fprintf(stderr, "Couldn't open dest file %s. %s\n",
116                         temp_path,
117                         strerror(errno));
118                 exit(1);
119         }
120
121         if(!(data_buffer = malloc(0x1000000)))
122         {
123                 fprintf(stderr, "Not enough memory to allocate data buffer.\n");
124                 exit(1);
125         }
126
127         if(!(contents_buffer = malloc(0x100000)))
128         {
129                 fprintf(stderr, "Not enough memory to allocate contents buffer.\n");
130                 exit(1);
131         }
132
133 // Leave space for offset to data
134         contents_size = sizeof(int);
135
136 // Read through all the resources, concatenate to dest file,
137 // and record the contents.
138         while(current_arg < argc)
139         {
140                 char *path = argv[current_arg++];
141                 if(!(src = fopen(path, "r")))
142                 {
143                         fprintf(stderr, "%s while opening %s\n", strerror(errno), path);
144                 }
145                 else
146                 {
147                         int size;
148                         fseek(src, 0, SEEK_END);
149                         size = ftell(src);
150                         fseek(src, 0, SEEK_SET);
151
152                         data_offset = data_size;
153
154                         if(!binary_mode && !string_mode)
155                         {
156 // Write size of image in data buffer
157                                 *(data_buffer + data_size) = (size & 0xff000000) >> 24;
158                                 data_size++;
159                                 *(data_buffer + data_size) = (size & 0xff0000) >> 16;
160                                 data_size++;
161                                 *(data_buffer + data_size) = (size & 0xff00) >> 8;
162                                 data_size++;
163                                 *(data_buffer + data_size) = size & 0xff;
164                                 data_size++;
165                         }
166
167                         int temp = fread(data_buffer + data_size, 1, size, src);
168                         data_size += size;
169 // Terminate string
170                         if(string_mode) data_buffer[data_size++] = 0;
171                         fclose(src);
172
173                         if(!binary_mode && !string_mode)
174                         {
175 // Create contents
176                                 append_contents(path,
177                                         data_offset,
178                                         contents_buffer,
179                                         &contents_size);
180                         }
181                 }
182         }
183
184         if(!binary_mode && !string_mode)
185         {
186 // Finish off size of contents
187                 *(int*)(contents_buffer) = contents_size;
188 // Write contents
189                 fwrite(contents_buffer, 1, contents_size, dest);
190         }
191
192 // Write data
193         fwrite(data_buffer, 1, data_size, dest);
194         fclose(dest);
195
196 // Run system command on it
197         sprintf(system_command, "%s %s %s", BOOTSTRAP, temp_path, out_path);
198         int temp = system(system_command);
199         return 0;
200 }
201
202