Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

bootstrap.c

Go to the documentation of this file.
00001 #include <errno.h>
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <string.h>
00005 
00006 // Bootstrap for themes.
00007 
00008 // Concatenates all the resources and a table of contents 
00009 // into a data file that must be converted to an object with objcopy.
00010 // The user must then pass the name of the destination symbol
00011 // _binary_destname_start
00012 // to BC_Theme::set_data to set the image table.
00013 
00014 
00015 // Usage: bootstrap <destname> <resource>...
00016 
00017 
00018 
00019 
00020 void append_contents(char *path,
00021         int data_offset,
00022         char *buffer,
00023         int *buffer_size)
00024 {
00025         char string[1024];
00026         int i, j = 0;
00027 
00028         for(i = strlen(path) - 1; 
00029                 i > 0 && path[i] && path[i] != '/';
00030                 i--) 
00031                 ;
00032 
00033         if(path[i] == '/') i++;
00034         
00035         for(j = 0; path[i] != 0; i++, j++)
00036                 string[j] = path[i];
00037 
00038         string[j] = 0;
00039         
00040         strcpy(buffer + *buffer_size, string);
00041 
00042         *buffer_size += strlen(string) + 1;
00043         
00044         *(int*)(buffer + *buffer_size) = data_offset;
00045         *buffer_size += sizeof(int);
00046 }
00047 
00048 int main(int argc, char *argv[])
00049 {
00050         FILE *dest;
00051         FILE *src;
00052         int i;
00053         char *contents_buffer;
00054         int contents_size = 0;
00055         char *data_buffer;
00056         int data_size = 0;
00057         int data_offset = 0;
00058         char temp_path[1024];
00059         char system_command[1024];
00060 
00061         if(argc < 3)
00062         {
00063                 fprintf(stderr, "Need 2 arguments you MOR-ON!\n");
00064                 exit(1);
00065         }
00066 
00067 
00068 // Make object filename
00069         strcpy(temp_path, argv[1]);
00070 
00071         if(!(dest = fopen(temp_path, "w")))
00072         {
00073                 fprintf(stderr, "Couldn't open dest file %s. %s\n",
00074                         temp_path,
00075                         strerror(errno));
00076                 exit(1);
00077         }
00078 
00079         if(!(data_buffer = malloc(0x1000000)))
00080         {
00081                 fprintf(stderr, "Not enough memory to allocate data buffer.\n");
00082                 exit(1);
00083         }
00084 
00085         if(!(contents_buffer = malloc(0x100000)))
00086         {
00087                 fprintf(stderr, "Not enough memory to allocate contents buffer.\n");
00088                 exit(1);
00089         }
00090 
00091 // Leave space for offset to data
00092         contents_size = sizeof(int);
00093 
00094 // Read through all the resources, concatenate to dest file, 
00095 // and record the contents.
00096         for(i = 2; i < argc; i++)
00097         {
00098                 char *path = argv[i];
00099                 if(!(src = fopen(path, "r")))
00100                 {
00101                         fprintf(stderr, "%s while opening %s\n", strerror(errno), path);
00102                         exit(1);
00103                 }
00104                 else
00105                 {
00106                         int size;
00107                         fseek(src, 0, SEEK_END);
00108                         size = ftell(src);
00109                         fseek(src, 0, SEEK_SET);
00110 
00111                         data_offset = data_size;
00112 
00113 // Write size of image in data buffer
00114                         *(data_buffer + data_size) = (size & 0xff000000) >> 24;
00115                         data_size++;
00116                         *(data_buffer + data_size) = (size & 0xff0000) >> 16;
00117                         data_size++;
00118                         *(data_buffer + data_size) = (size & 0xff00) >> 8;
00119                         data_size++;
00120                         *(data_buffer + data_size) = size & 0xff;
00121                         data_size++;
00122 
00123                         fread(data_buffer + data_size, 1, size, src);
00124                         data_size += size;
00125                         fclose(src);
00126 
00127 // Create contents
00128                         append_contents(path, 
00129                                 data_offset, 
00130                                 contents_buffer, 
00131                                 &contents_size);
00132                 }
00133         }
00134 
00135 // Finish off size of contents
00136         *(int*)(contents_buffer) = contents_size;
00137 // Write contents
00138         fwrite(contents_buffer, 1, contents_size, dest);
00139 // Write data
00140         fwrite(data_buffer, 1, data_size, dest);
00141         fclose(dest);
00142 
00143         return 0;
00144 }
00145 
00146 

Generated on Sun Jan 8 13:26:34 2006 for Guicast-svn by  doxygen 1.4.4