| | @@ -54,10 +54,49 @@ |
| 54 | 54 | got = fread(z, 1, nByte, in); |
| 55 | 55 | fclose(in); |
| 56 | 56 | z[got] = 0; |
| 57 | 57 | return z; |
| 58 | 58 | } |
| 59 | + |
| 60 | +/* |
| 61 | +** Try to compress a javascript file by removing unnecessary whitespace. |
| 62 | +** |
| 63 | +** Warning: This compression routine does not necessarily work for any |
| 64 | +** arbitrary Javascript source file. But it should work ok for the |
| 65 | +** well-behaved source files in this project. |
| 66 | +*/ |
| 67 | +static void compressJavascript(unsigned char *z, int *pn){ |
| 68 | + int n = *pn; |
| 69 | + int i, j, k; |
| 70 | + for(i=j=0; i<n; i++){ |
| 71 | + unsigned char c = z[i]; |
| 72 | + if( c=='/' ){ |
| 73 | + if( z[i+1]=='*' ){ |
| 74 | + for(k=i+3; k<n && (z[k]!='/' || z[k-1]!='*'); k++){} |
| 75 | + if( k<n ){ |
| 76 | + i = k; |
| 77 | + while( i+1<n && isspace(z[i+1]) ) i++; |
| 78 | + continue; |
| 79 | + } |
| 80 | + }else if( z[i+1]=='/' ){ |
| 81 | + for(k=i+2; k<n && z[k]!='\n'; k++){} |
| 82 | + i = k; |
| 83 | + while( i+1<n && isspace(z[i+1]) ) i++; |
| 84 | + continue; |
| 85 | + } |
| 86 | + } |
| 87 | + if( c=='\n' ){ |
| 88 | + while( j>0 && isspace(z[j-1]) ) j--; |
| 89 | + z[j++] = '\n'; |
| 90 | + while( i+1<n && isspace(z[i+1]) ) i++; |
| 91 | + continue; |
| 92 | + } |
| 93 | + z[j++] = c; |
| 94 | + } |
| 95 | + z[j] = 0; |
| 96 | + *pn = j; |
| 97 | +} |
| 59 | 98 | |
| 60 | 99 | /* |
| 61 | 100 | ** There is an instance of the following for each file translated. |
| 62 | 101 | */ |
| 63 | 102 | typedef struct Resource Resource; |
| | @@ -85,10 +124,11 @@ |
| 85 | 124 | int nRes; |
| 86 | 125 | unsigned char *pData; |
| 87 | 126 | int nErr = 0; |
| 88 | 127 | int nSkip; |
| 89 | 128 | int nPrefix = 0; |
| 129 | + int nName; |
| 90 | 130 | |
| 91 | 131 | if( argc>3 && strcmp(argv[1],"--prefix")==0 ){ |
| 92 | 132 | nPrefix = (int)strlen(argv[2]); |
| 93 | 133 | argc -= 2; |
| 94 | 134 | argv += 2; |
| | @@ -119,10 +159,18 @@ |
| 119 | 159 | nSkip = 0; |
| 120 | 160 | while( pData[nSkip]=='#' ){ |
| 121 | 161 | while( pData[nSkip]!=0 && pData[nSkip]!='\n' ){ nSkip++; } |
| 122 | 162 | if( pData[nSkip]=='\n' ) nSkip++; |
| 123 | 163 | } |
| 164 | + |
| 165 | + /* Compress javascript source files */ |
| 166 | + nName = (int)strlen(aRes[i].zName); |
| 167 | + if( nName>3 && strcmp(&aRes[i].zName[nName-3],".js")==0 ){ |
| 168 | + int x = sz-nSkip; |
| 169 | + compressJavascript(pData+nSkip, &x); |
| 170 | + sz = x + nSkip; |
| 171 | + } |
| 124 | 172 | |
| 125 | 173 | aRes[i].nByte = sz - nSkip; |
| 126 | 174 | aRes[i].idx = i; |
| 127 | 175 | printf("/* Content of file %s */\n", aRes[i].zName); |
| 128 | 176 | printf("static const unsigned char bidata%d[%d] = {\n ", |
| 129 | 177 | |