r956: README.BUILD - add more library recommendations
[cinelerra_cv/ct.git] / guicast / bootstrap.c
blob917056d99e38d93556d3de4298c87e377c25fc93
1 #include <errno.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
6 // Bootstrap for themes.
8 // Concatenates all the resources and a table of contents
9 // into a data file that must be converted to an object with objcopy.
10 // The user must then pass the name of the destination symbol
11 // _binary_destname_start
12 // to BC_Theme::set_data to set the image table.
15 // Usage: bootstrap <destname> <resource>...
20 void append_contents(char *path,
21 int data_offset,
22 char *buffer,
23 int *buffer_size)
25 char string[1024];
26 int i, j = 0;
28 for(i = strlen(path) - 1;
29 i > 0 && path[i] && path[i] != '/';
30 i--)
33 if(path[i] == '/') i++;
35 for(j = 0; path[i] != 0; i++, j++)
36 string[j] = path[i];
38 string[j] = 0;
40 strcpy(buffer + *buffer_size, string);
42 *buffer_size += strlen(string) + 1;
44 *(int*)(buffer + *buffer_size) = data_offset;
45 *buffer_size += sizeof(int);
48 int main(int argc, char *argv[])
50 FILE *dest;
51 FILE *src;
52 int i;
53 char *contents_buffer;
54 int contents_size = 0;
55 char *data_buffer;
56 int data_size = 0;
57 int data_offset = 0;
58 char temp_path[1024];
59 char system_command[1024];
61 if(argc < 3)
63 fprintf(stderr, "Need 2 arguments you MOR-ON!\n");
64 exit(1);
68 // Make object filename
69 strcpy(temp_path, argv[1]);
71 if(!(dest = fopen(temp_path, "w")))
73 fprintf(stderr, "Couldn't open dest file %s. %s\n",
74 temp_path,
75 strerror(errno));
76 exit(1);
79 if(!(data_buffer = malloc(0x1000000)))
81 fprintf(stderr, "Not enough memory to allocate data buffer.\n");
82 exit(1);
85 if(!(contents_buffer = malloc(0x100000)))
87 fprintf(stderr, "Not enough memory to allocate contents buffer.\n");
88 exit(1);
91 // Leave space for offset to data
92 contents_size = sizeof(int);
94 // Read through all the resources, concatenate to dest file,
95 // and record the contents.
96 for(i = 2; i < argc; i++)
98 char *path = argv[i];
99 if(!(src = fopen(path, "r")))
101 fprintf(stderr, "%s while opening %s\n", strerror(errno), path);
102 exit(1);
104 else
106 int size;
107 fseek(src, 0, SEEK_END);
108 size = ftell(src);
109 fseek(src, 0, SEEK_SET);
111 data_offset = data_size;
113 // Write size of image in data buffer
114 *(data_buffer + data_size) = (size & 0xff000000) >> 24;
115 data_size++;
116 *(data_buffer + data_size) = (size & 0xff0000) >> 16;
117 data_size++;
118 *(data_buffer + data_size) = (size & 0xff00) >> 8;
119 data_size++;
120 *(data_buffer + data_size) = size & 0xff;
121 data_size++;
123 fread(data_buffer + data_size, 1, size, src);
124 data_size += size;
125 fclose(src);
127 // Create contents
128 append_contents(path,
129 data_offset,
130 contents_buffer,
131 &contents_size);
135 // Finish off size of contents
136 *(int*)(contents_buffer) = contents_size;
137 // Write contents
138 fwrite(contents_buffer, 1, contents_size, dest);
139 // Write data
140 fwrite(data_buffer, 1, data_size, dest);
141 fclose(dest);
143 return 0;