data2c.c

Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2005-2006 The Pentagram team
00003 
00004 This program is free software; you can redistribute it and/or
00005 modify it under the terms of the GNU General Public License
00006 as published by the Free Software Foundation; either version 2
00007 of the License, or (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 */
00018 
00019 #include <stdio.h>
00020 #include <string.h>
00021 #include <stdlib.h>
00022 
00023 const char* basename(const char* filename)
00024 {
00025         const char* base;
00026         const char* x;
00027 
00028         base = filename;
00029 
00030         for (x = filename; *x != 0; ++x) {
00031                 if (*x == '/' || *x == '\\')
00032                         base = x+1;
00033         }
00034 
00035         return base;
00036 }
00037 
00038 int scanname(const char* name)
00039 {
00040         const char* x;
00041         char c;
00042 
00043         for (x = name; *x != 0; ++x) {
00044                 c = *x;
00045                 if (c < 'A' && c > 'Z' && c < 'a' && c > 'z' &&
00046                         c < '0' && c > '9' && c != '_' && c != '.' && c != '-')
00047                         return 0;
00048         }
00049 
00050         return 1;
00051 }
00052 
00053 int main(int argc, char* argv[])
00054 {
00055         int i;
00056         const char* outfilename = 0;
00057         FILE* outfile;
00058         unsigned int* filesizes;
00059 
00060         if (argc < 2) {
00061                 fprintf(stderr, "Not enough arguments: %s output.h input_files \n",
00062                                 argv[0]);
00063                 return 1;
00064         }
00065 
00066         outfilename = argv[1];
00067 /*      printf("Output file: %s\n", outfilename);*/
00068         outfile = fopen(outfilename, "w");
00069         if (!outfile) {
00070                 fprintf(stderr, "Error opening output file: %s\n", outfilename);
00071                 return 1;
00072         }
00073 
00074         fprintf(outfile, "/* This file was generated automatically. DO NOT EDIT! */\n\n");
00075 
00076         fprintf(outfile, "namespace PentagramData {\n");
00077 
00078         filesizes = (unsigned int*)malloc((argc-2)*sizeof(filesizes[0]));
00079 
00080         for (i = 2; i < argc; ++i) {
00081                 const char* filename;
00082                 const char* base;
00083                 FILE* infile;
00084                 int index;
00085                 unsigned char buf[1024];
00086                 unsigned int read;
00087                 unsigned int j;
00088                 int first;
00089 
00090                 filename = argv[i];
00091                 base = basename(filename);
00092                 index = i-2;
00093 
00094 /*
00095                 printf("Input file: %s\n", filename);
00096                 printf("base: %s\n", base);
00097 */
00098 
00099                 if (strlen(filename) == 0 || strlen(base) == 0 || !scanname(base)) {
00100                         fprintf(stderr, "Invalid filename.");
00101                         fclose(outfile);
00102                         unlink(outfilename);
00103                         return 1;
00104                 }
00105 
00106                 infile = fopen(filename, "rb");
00107                 if (!infile) {
00108                         fprintf(stderr, "Error opening input file: %s\n", filename);
00109                         fclose(outfile);
00110                         unlink(outfilename);
00111                         return 1;
00112                 }
00113 
00114                 fprintf(outfile, "static const uint8 data%d[] = {\n", index);
00115                 first = 1;
00116                 filesizes[index] = 0;
00117 
00118                 do {
00119                         unsigned int s;
00120 
00121                         read = fread(buf, 1, 1024, infile);
00122                         filesizes[index] += read;
00123                         for (j = 0; j < read/8; j++) {
00124                                 fprintf(outfile, "%s0x%02X,0x%02X,0x%02X,0x%02X,0x%02X,0x%02X,0x%02X,0x%02X", (first?"":",\n"),buf[j*8],buf[j*8+1],buf[j*8+2],buf[j*8+3],buf[j*8+4],buf[j*8+5],buf[j*8+6],buf[j*8+7]);
00125                                 first = 0;
00126                         }
00127                         s = j*8;
00128                         for (j = 0; j < read%8; j++) {
00129                                 fprintf(outfile, "%s0x%02X", j?",":(first?"":",\n"), buf[s+j]);
00130                         }
00131 
00132                 } while (read == 1024);
00133 
00134                 fprintf(outfile, "\n};\n");
00135 
00136                 fclose(infile);
00137         }
00138 
00139         fprintf(outfile, "struct DataFile { const char* name; const uint8* data; unsigned int size; };\n");
00140 
00141         fprintf(outfile, "DataFile files[] = {\n");
00142 
00143         for (i = 2; i < argc; ++i) {
00144                 const char* filename;
00145                 const char* base;
00146                 int index;
00147 
00148                 filename = argv[i];
00149                 base = basename(filename);
00150                 index = i-2;
00151 
00152                 fprintf(outfile, "{ \"%s\", data%d, %d },\n", base, index, filesizes[index]);
00153         }
00154 
00155         fprintf(outfile, "{ 0, 0, 0 }\n};\n");
00156 
00157         fprintf(outfile, "}\n");
00158 
00159         fclose(outfile);
00160 
00161         return 0;
00162 }

Generated on Fri Jul 27 22:27:13 2007 for pentagram by  doxygen 1.4.7