/* ** Copyright (c) 2014 D. Richard Hipp ** ** This program is free software; you can redistribute it and/or ** modify it under the terms of the Simplified BSD License (also ** known as the "2-Clause License" or "FreeBSD License".) ** ** This program is distributed in the hope that it will be useful, ** but without any warranty; without even the implied warranty of ** merchantability or fitness for a particular purpose. ** ** Author contact information: ** drh@hwaci.com ** http://www.hwaci.com/drh/ ** ******************************************************************************* ** ** This is a stand-alone utility program that is part of the Fossil build ** process. This program reads files named on the command line and converts ** them into ANSI-C static char array variables. Output is written onto ** standard output. ** ** The makefiles use this utility to package various resources (large scripts, ** GIF images, etc) that are separate files in the source code as byte ** arrays in the resulting executable. */ #include #include #include #include /* ** Read the entire content of the file named zFilename into memory obtained ** from malloc() and return a pointer to that memory. Write the size of the ** file into *pnByte. */ static unsigned char *read_file(const char *zFilename, int *pnByte){ FILE *in; unsigned char *z; int nByte; int got; in = fopen(zFilename, "rb"); if( in==0 ){ return 0; } fseek(in, 0, SEEK_END); *pnByte = nByte = ftell(in); fseek(in, 0, SEEK_SET); z = malloc( nByte+1 ); if( z==0 ){ fprintf(stderr, "failed to allocate %d bytes\n", nByte+1); exit(1); } got = fread(z, 1, nByte, in); fclose(in); z[got] = 0; return z; } /* ** Try to compress a javascript file by removing unnecessary whitespace. ** ** Warning: This compression routine does not necessarily work for any ** arbitrary Javascript source file. But it should work ok for the ** well-behaved source files in this project. */ static void compressJavascript(unsigned char *z, int *pn){ int n = *pn; int i, j, k; for(i=j=0; i0 && (z[j-1]==' ' || z[j-1]=='\t') ){ j--; } for(k=i+3; k0 && (z[j-1]==' ' || z[j-1]=='\t') ){ j--; } for(k=i+2; k0 && isspace(z[j-1]) ) j--; z[j++] = '\n'; while( i+1zName, pB->zName); } int main(int argc, char **argv){ int i, sz; int j, n; Resource *aRes; int nRes; unsigned char *pData; int nErr = 0; int nSkip; int nPrefix = 0; int nName; if( argc>3 && strcmp(argv[1],"--prefix")==0 ){ nPrefix = (int)strlen(argv[2]); argc -= 2; argv += 2; } nRes = argc - 1; aRes = malloc( nRes*sizeof(aRes[0]) ); if( aRes==0 ){ fprintf(stderr, "malloc failed\n"); return 1; } for(i=0; i3 && strcmp(&aRes[i].zName[nName-3],".js")==0) || (nName>7 && strcmp(&aRes[i].zName[nName-7], "/js.txt")==0) ){ int x = sz-nSkip; compressJavascript(pData+nSkip, &x); sz = x + nSkip; } aRes[i].nByte = sz - nSkip; aRes[i].idx = i; printf("/* Content of file %s */\n", aRes[i].zName); printf("static const unsigned char bidata%d[%d] = {\n ", i, sz+1-nSkip); for(j=nSkip, n=0; j<=sz; j++){ printf("%3d", pData[j]); if( j==sz ){ printf(" };\n"); }else if( n==14 ){ printf(",\n "); n = 0; }else{ printf(", "); n++; } } free(pData); } printf("typedef struct BuiltinFileTable BuiltinFileTable;\n"); printf("struct BuiltinFileTable {\n"); printf(" const char *zName;\n"); printf(" const unsigned char *pData;\n"); printf(" int nByte;\n"); printf("};\n"); printf("static const BuiltinFileTable aBuiltinFiles[] = {\n"); for(i=0; i=nPrefix ) z += nPrefix; while( z[0]=='.' || z[0]=='/' || z[0]=='\\' ){ z++; } aRes[i].zName = z; while( z[0] ){ if( z[0]=='\\' ) z[0] = '/'; z++; } } qsort(aRes, nRes, sizeof(aRes[0]), compareResource); for(i=0; i