File: reswrap.cpp

package info (click to toggle)
fox1.6 1.6.57-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 17,592 kB
  • sloc: cpp: 148,083; sh: 4,294; ansic: 2,345; makefile: 1,422; perl: 119
file content (471 lines) | stat: -rw-r--r-- 14,556 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/********************************************************************************
*                                                                               *
*                R e s o u r c e   W r a p p i n g   U t i l i t y              *
*                                                                               *
*********************************************************************************
* Copyright (C) 1997,2006 by Jeroen van der Zijp.   All Rights Reserved.        *
*********************************************************************************
* This program is free software; you can redistribute it and/or modify          *
* it under the terms of the GNU General Public License as published by          *
* the Free Software Foundation; either version 2 of the License, or             *
* (at your option) any later version.                                           *
*                                                                               *
* 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.  See the                 *
* GNU General Public License for more details.                                  *
*                                                                               *
* You should have received a copy of the GNU General Public License             *
* along with this program; if not, write to the Free Software                   *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.    *
*********************************************************************************
* $Id: reswrap.cpp,v 1.17 2006/01/22 17:59:02 fox Exp $                         *
********************************************************************************/
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "ctype.h"



/*

  Notes:
  - Updated to version 4.0.
  - Can now also generate output as a text string.
  - When ASCII option is used with text string option, it prints human
    readable C-style string, with non-ASCII characters escaped as appropriate.
  - Removed PPM feature as that was never really used in FOX.
  - License changed to GPL from LGPL because this is a standalone program
    that does not need to be linked to anything.
  - Added MSDOS mode, useful for wrapping ASCII text.
  - Added option to keep extension (separated by '_').
  - Need option to place #include "icons.h" or something into icons.cpp
  - Always prepend prefix in front of resource name, even if name was overrided
    because prefix may be namespace name.
*/

/*******************************************************************************/

#define MODE_DECIMAL  0
#define MODE_HEX      1
#define MODE_TEXT     2


const char version[]="4.0.0";


/* Print some help */
void printusage(){
  fprintf(stderr,"Usage: reswrap [options] [-o[a] outfile] files...\n");
  fprintf(stderr,"Convert files containing images, text, or binary data into C/C++ data arrays.\n");
  fprintf(stderr,"\n");
  fprintf(stderr,"Options:\n");
  fprintf(stderr,"  -o[a] outfile  Output [append] to outfile instead of stdout\n");
  fprintf(stderr,"  -h             Print help\n");
  fprintf(stderr,"  -v             Print version number\n");
  fprintf(stderr,"  -d             Output as decimal\n");
  fprintf(stderr,"  -m             Read files with MS-DOS mode (default is binary)\n");
  fprintf(stderr,"  -x             Output as hex (default)\n");
  fprintf(stderr,"  -t[a]          Output as [ascii] text string\n");
  fprintf(stderr,"  -e             Generate external reference declaration\n");
  fprintf(stderr,"  -i             Build an include file\n");
  fprintf(stderr,"  -k             Keep extension, separated by underscore\n");
  fprintf(stderr,"  -s             Suppress header in output file\n");
  fprintf(stderr,"  -p prefix      Place prefix in front of names of declarations and definitions\n");
  fprintf(stderr,"  -n namespace   Place declarations and definitions inside given namespace\n");
  fprintf(stderr,"  -c cols        Change number of columns in output to cols\n");
  fprintf(stderr,"  -u             Force unsigned char even for text mode\n");
  fprintf(stderr,"  -z             Output size in declarations\n");
  fprintf(stderr,"\n");
  fprintf(stderr,"Each file may be preceded by the following extra option:\n");
  fprintf(stderr,"  -r name        Override resource name of following resource file\n");
  fprintf(stderr,"\n");
  }


/* Print version information */
void printversion(){
  fprintf(stderr,"Reswrap %s %s.\n",version,_DEBDATE_);
  fprintf(stderr,"Copyright (C) 1997,2005 Jeroen van der Zijp. All Rights Reserved.\n");
  fprintf(stderr,"Please visit: http://www.fox-toolkit.org for further information.\n");
  fprintf(stderr,"\n");
  fprintf(stderr,"This program is free software; you can redistribute it and/or modify\n");
  fprintf(stderr,"it under the terms of the GNU General Public License as published by\n");
  fprintf(stderr,"the Free Software Foundation; either version 2 of the License, or\n");
  fprintf(stderr,"(at your option) any later version.\n");
  fprintf(stderr,"\n");
  fprintf(stderr,"This program is distributed in the hope that it will be useful,\n");
  fprintf(stderr,"but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
  fprintf(stderr,"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n");
  fprintf(stderr,"GNU General Public License for more details.\n");
  fprintf(stderr,"\n");
  fprintf(stderr,"You should have received a copy of the GNU General Public License\n");
  fprintf(stderr,"along with this program; if not, write to the Free Software\n");
  fprintf(stderr,"Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.\n");
  }


/* Build resource name */
void resourcename(char *name,const char* prefix,const char* filename,int keepdot){
  const char* begin=name;
  const char* ptr;

  /* Copy prefix */
  while(*prefix){
    *name++=*prefix++;
    }

  /* Get name only; take care of mixed path separator characters on mswindows */
  if((ptr=strrchr(filename,'/'))!=0) filename=ptr;
  if((ptr=strrchr(filename,'\\'))!=0) filename=ptr;

  /* Copy filename */
  while(*filename){

    /* C++ identifier may contain _, alpha, digit (if not first), or namespace separator : */
    if(*filename==':' || *filename=='_' || isalpha(*filename) || (isdigit(*filename) && (begin!=name))){
      *name++=*filename;
      }

    /* We can squash dot extension to _ */
    else if(*filename=='.'){
      if(!keepdot) break;
      *name++='_';
      }

    filename++;
    }
  *name=0;
  }


/* Main */
int main(int argc,char **argv){
  char *outfilename,*resfilename;
  FILE *resfile,*outfile;
  int i,b,first,col,append,maxcols,external,header,include,mode,colsset,ascii,msdos,keepext,hex,forceunsigned,declaresize,resfilesize;
  const char *thenamespace,*theprefix;
  char resname[1000];

  /* Initialize */
  outfilename=0;
  outfile=stdout;
  maxcols=16;
  colsset=0;
  external=0;
  header=1;
  include=0;
  ascii=0;
  mode=MODE_HEX;
  msdos=0;
  keepext=0;
  thenamespace=0;
  theprefix="";
  append=0;
  forceunsigned=0;
  declaresize=0;

  /* Check arguments */
  if(argc<2){
    printusage();
    exit(1);
    }

  /* Process all options first, except for the -r option */
  for(i=1; i<argc && argv[i][0]=='-' && argv[i][1]!='r'; i++){

    /* Change output file */
    if(argv[i][1]=='o'){
      if(argv[i][2]=='a') append=1;
      i++;
      if(i>=argc){
        fprintf(stderr,"reswrap: missing argument for -o option\n");
        exit(1);
        }
      outfilename=argv[i];
      }

    /* Print help */
    else if(argv[i][1]=='h'){
      printusage();
      exit(0);
      }

    /* Print version */
    else if(argv[i][1]=='v'){
      printversion();
      exit(0);
      }

    /* Switch to decimal */
    else if(argv[i][1]=='d'){
      mode=MODE_DECIMAL;
      if(!colsset) maxcols=10;
      }

    /* Switch to hex */
    else if(argv[i][1]=='x'){
      mode=MODE_HEX;
      if(!colsset) maxcols=16;
      }

    /* Switch to text */
    else if(argv[i][1]=='t'){
      if(argv[i][2]=='a') ascii=1;
      mode=MODE_TEXT;
      if(!colsset) maxcols=80;
      }

    /* Suppress header */
    else if(argv[i][1]=='s'){
      header=0;
      }

    /* Force unsigned */
    else if(argv[i][1]=='u'){
      forceunsigned=1;
      }

    /* Declare size */
    else if(argv[i][1]=='z'){
      declaresize=1;
      }

    /* Generate as external reference */
    else if(argv[i][1]=='e'){
      external=1;
      }

    /* Building include file implies also extern */
    else if(argv[i][1]=='i'){
      include=1;
      external=1;
      }

    /* Read resource with MS-DOS mode */
    else if(argv[i][1]=='m'){
      msdos=1;
      }

    /* Keep extension */
    else if(argv[i][1]=='k'){
      keepext=1;
      }

    /* Change number of columns */
    else if(argv[i][1]=='c'){
      i++;
      if(i>=argc){
        fprintf(stderr,"reswrap: missing argument for -c option\n");
        exit(1);
        }
      if(sscanf(argv[i],"%d",&maxcols)==1 && maxcols<1){
        fprintf(stderr,"reswrap: illegal argument for number of columns\n");
        exit(1);
        }
      colsset=1;
      }

    /* Embed in namespace */
    else if(argv[i][1]=='n'){
      i++;
      if(i>=argc){
        fprintf(stderr,"reswrap: missing argument for -n option\n");
        exit(1);
        }
      thenamespace=argv[i];
      }

    /* Prefix in front of declarations */
    else if(argv[i][1]=='p'){
      i++;
      if(i>=argc){
        fprintf(stderr,"reswrap: missing argument for -p option\n");
        exit(1);
        }
      theprefix=argv[i];
      }
    }

  /* To file instead out stdout */
  if(outfilename){
    outfile=fopen(outfilename,append?"a":"w");
    if(!outfile){
      fprintf(stderr,"reswrap: unable to open output file %s\n",outfilename);
      exit(1);
      }
    }

  /* Output header */
  if(header){
    fprintf(outfile,"/* Generated by reswrap version %s */\n\n",version);
    }

  /* Output namespace begin */
  if(thenamespace){
    fprintf(outfile,"namespace %s {\n\n",thenamespace);
    }

  /* Process resource files next */
  for(; i<argc; i++){

    /* Resource name override */
    if(argv[i][0]=='-' && argv[i][1]=='r'){

      /* Must have extra argument */
      if(++i>=argc){
        fprintf(stderr,"reswrap: missing name argument for -r option\n");
        exit(1);
        }

      /* Get resource name */
      resourcename(resname,theprefix,argv[i],keepext);

      /* Must have following file name */
      if(++i>=argc){
        fprintf(stderr,"reswrap: missing resource file name\n");
        exit(1);
        }

      /* Get resource file name */
      resfilename=argv[i];
      }

    else{

      /* Get resource file name */
      resfilename=argv[i];

      /* Get resource name */
      resourcename(resname,theprefix,resfilename,keepext);
      }


    /* Check resource name not empty */
    if(*resname==0){
      fprintf(stderr,"reswrap: empty resource name for %s\n",resfilename);
      exit(1);
      }

    /* Open resource file; always open as binary */
    resfile=fopen(resfilename,"rb");
    if(!resfile){
      fprintf(stderr,"reswrap: unable to open input file %s\n",resfilename);
      exit(1);
      }

    /* Get the size */
    fseek(resfile,0,SEEK_END);
    resfilesize=ftell(resfile);
    fseek(resfile,0,SEEK_SET);

    /* Add one if text mode, for end of string */
    if(mode==MODE_TEXT){
      resfilesize++;
      }

    /* Output header */
    if(header){
      fprintf(outfile,"/* created by reswrap from file %s */\n",resfilename);
      }

    /* Generate external reference for #include's */
    if(external){ fprintf(outfile,"extern "); }

    /* In text mode, output a 'char' declaration */
    if((mode==MODE_TEXT) && !forceunsigned){
      if(declaresize){
        fprintf(outfile,"const char %s[%d]",resname,resfilesize);
        }
      else{
        fprintf(outfile,"const char %s[]",resname);
        }
      }

    /* In binary mode, output a 'unsigned char' declaration */
    else{
      if(declaresize){
        fprintf(outfile,"const unsigned char %s[%d]",resname,resfilesize);
        }
      else{
        fprintf(outfile,"const unsigned char %s[]",resname);
        }
      }

    /* Generate resource array */
    if(!include){
      if(mode==MODE_TEXT){
        col=0;
        hex=0;
        fprintf(outfile,"=\n  \"");
        while((b=fgetc(resfile))!=EOF){
          if(msdos && (b=='\r')) continue;
          if(col>=maxcols){
            fprintf(outfile,"\"\n  \"");
            col=0;
            }
          if(ascii){
            if(b=='\\'){ fprintf(outfile,"\\\\"); col+=2; hex=0; }
            else if(b=='\a'){ fprintf(outfile,"\\a"); col+=2; hex=0; }
            else if(b=='\t'){ fprintf(outfile,"\\t"); col+=2; hex=0; }
            else if(b=='\r'){ fprintf(outfile,"\\r"); col+=2; hex=0; }
            else if(b=='\f'){ fprintf(outfile,"\\f"); col+=2; hex=0; }
            else if(b=='\v'){ fprintf(outfile,"\\v"); col+=2; hex=0; }
            else if(b=='\"'){ fprintf(outfile,"\\\""); col+=2; hex=0; }
            else if(b=='\n'){ fprintf(outfile,"\\n\"\n  \""); col=0; hex=0; }
            else if(b<32 || b>=127){ fprintf(outfile,"\\x%02x",b); col+=4; hex=1; }
            else if(hex && isxdigit(b)){ fprintf(outfile,"\\x%02x",b); col+=4; hex=1; }
            else{ fprintf(outfile,"%c",b); col+=1; hex=0; }
            }
          else{
            fprintf(outfile,"\\x%02x",b); col+=4;
            }
          }
        fprintf(outfile,"\"\n  ");
        }
      else{
        col=0;
        first=1;
        fprintf(outfile,"={\n  ");
        while((b=fgetc(resfile))!=EOF){
          if(msdos && (b=='\r')) continue;
          if(!first){
            fprintf(outfile,",");
            }
          if(col>=maxcols){
            fprintf(outfile,"\n  ");
            col=0;
            }
          if(mode==MODE_HEX)
            fprintf(outfile,"0x%02x",b);
          else
            fprintf(outfile,"%3d",b);
          first=0;
          col++;
          }
        fprintf(outfile,"\n  }");
        }
      }

    fprintf(outfile,";\n\n");

    /* Close resource file */
    fclose(resfile);
    }

  /* Output namespace end */
  if(thenamespace){
    fprintf(outfile,"}\n");
    }

  /* To file instead out stdout */
  if(outfilename){
    fclose(outfile);
    }

  return 0;
  }