r105: This commit was manufactured by cvs2svn to create tag
[cinelerra_cv/mob.git] / hvirtual / guicast / bootstrap.c
blob2f55967894d8a7aa7cbe27aaae680bf4b01898fe
1 #include <errno.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
6 // Bootstrap for themes.
8 // Concatenate the resources onto the end of the target. Then write a
9 // table of contents.
11 // Usage: bootstrap <target> <resource> * n
13 // The table of contents contains simply the filenames of the resources
14 // with offsets.
16 // Initial startup with static resources:
17 // F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND
18 // 040 0 14623 14602 12 0 107512 28632 - S pts/2 0:00 ci
19 //
20 // -rwxr-xr-x 1 root root 736712 Dec 8 15:59 defaulttheme.plugin
23 // Initial startup with concatenated resources:
24 // F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND
25 // 040 0 23653 23644 12 0 111512 27520 - S pts/0 0:00 ci
27 // -rwxr-xr-x 1 root root 860924 Dec 8 22:33 defaulttheme.plugin
29 // At least the compile time is less.
32 void append_contents(char *path,
33 int data_offset,
34 int dest_size,
35 char *buffer,
36 int *buffer_size)
38 char string[1024];
39 int i, j = 0;
41 for(i = strlen(path) - 1;
42 i > 0 && path[i] && path[i] != '/';
43 i--)
46 if(path[i] == '/') i++;
48 for(j = 0; path[i] != 0; i++, j++)
49 string[j] = path[i];
51 string[j] = 0;
53 strcpy(buffer + *buffer_size, string);
55 *buffer_size += strlen(string) + 1;
57 *(int*)(buffer + *buffer_size) = data_offset;
58 *buffer_size += sizeof(int);
61 int main(int argc, char *argv[])
63 FILE *dest;
64 FILE *src;
65 int total_resources;
66 int i;
67 char *contents_buffer;
68 int contents_size = 0;
69 char *data_buffer;
70 int data_size = 0;
71 int dest_size;
72 int contents_offset;
74 if(argc < 3)
76 fprintf(stderr, "Need 2 arguments you MOR-ON!\n");
77 exit(1);
82 if(!(dest = fopen(argv[1], "r")))
84 fprintf(stderr, "While opening %s: %s\n", argv[1], strerror(errno));
85 exit(1);
87 else
89 fseek(dest, 0, SEEK_END);
90 dest_size = ftell(dest);
91 fclose(dest);
94 dest = fopen(argv[1], "a+");
95 total_resources = argc - 2;
96 data_size = 0;
97 if(!(data_buffer = malloc(0x1000000)))
99 fprintf(stderr, "Not enough memory to allocate data buffer.\n");
100 exit(1);
103 if(!(contents_buffer = malloc(0x100000)))
105 fprintf(stderr, "Not enough memory to allocate contents buffer.\n");
106 exit(1);
110 for(i = 0; i < total_resources; i++)
112 char *path = argv[2 + i];
113 if(!(src = fopen(path, "r")))
115 fprintf(stderr, "%s while opening %s\n", strerror(errno), path);
117 else
119 // Copy data
120 int size, data_offset;
121 fseek(src, 0, SEEK_END);
122 size = ftell(src);
123 fseek(src, 0, SEEK_SET);
125 data_offset = data_size;
126 // Write size of image in data buffer
127 *(data_buffer + data_size) = (size & 0xff000000) >> 24;
128 data_size++;
129 *(data_buffer + data_size) = (size & 0xff0000) >> 16;
130 data_size++;
131 *(data_buffer + data_size) = (size & 0xff00) >> 8;
132 data_size++;
133 *(data_buffer + data_size) = size & 0xff;
134 data_size++;
136 fread(data_buffer + data_size, 1, size, src);
137 data_size += size;
138 fclose(src);
140 // Create contents
141 append_contents(path, data_offset, dest_size, contents_buffer, &contents_size);
145 fwrite(data_buffer, 1, data_size, dest);
146 contents_offset = ftell(dest);
147 *(int*)(contents_buffer + contents_size) = dest_size;
148 contents_size += sizeof(int);
149 *(int*)(contents_buffer + contents_size) = contents_offset;
150 contents_size += sizeof(int);
151 fwrite(contents_buffer, 1, contents_size, dest);
153 fclose(dest);