1 |
/************************************************* -*- mode: C -*- |
2 |
** |
3 |
** $Header: /u/gcmpack/MITgcm/pkg/embed_files/encode_files.c,v 1.1 2006/01/11 01:38:09 edhill Exp $ |
4 |
** $Name: $ |
5 |
** |
6 |
*/ |
7 |
|
8 |
/* Encode one or more files for later writing */ |
9 |
|
10 |
#include <stdio.h> |
11 |
#include <stdlib.h> |
12 |
#include <unistd.h> |
13 |
#include <sys/stat.h> |
14 |
#include <sys/types.h> |
15 |
|
16 |
int main(int argc, char** argv) |
17 |
{ |
18 |
FILE * fout; |
19 |
FILE * fin; |
20 |
int ii; |
21 |
int nbytes; |
22 |
struct stat astat; |
23 |
unsigned char c; |
24 |
unsigned char buf[1024]; |
25 |
|
26 |
/* |
27 |
fout = fopen("t8", "w"); |
28 |
for (ii=0; ii<256; ii++) { |
29 |
c = (unsigned char)ii; |
30 |
fprintf(fout,"%c",c); |
31 |
} |
32 |
fclose(fout); |
33 |
*/ |
34 |
|
35 |
if (argc < 3) { |
36 |
printf("ERROR: usage is: " |
37 |
"\"%s OUTPUT_FILE INPUT_FILE [...INPUT_FILES...]\"\n", |
38 |
argv[0]); |
39 |
return 1; |
40 |
} |
41 |
|
42 |
fout = fopen(argv[1], "w"); |
43 |
if (! fout) { |
44 |
printf("ERROR: cannot open \"%s\" for output\n", argv[1]); |
45 |
return 1; |
46 |
} |
47 |
fprintf(fout,"struct embeded_file {\n char * name;\n" |
48 |
" int ndat;\n char * dat;\n};\n"); |
49 |
fprintf(fout,"const int n_flist = %d;\n", argc-2); |
50 |
fprintf(fout,"struct embeded_file flist[] = {\n"); |
51 |
|
52 |
for (ii=2; ii<argc; ii++) { |
53 |
printf(" encoding: \"%s\"\n",argv[ii]); |
54 |
if (! stat(argv[ii], &astat)) { |
55 |
nbytes = (int)(astat.st_size); |
56 |
} |
57 |
else { |
58 |
printf(" ERROR: cannot stat() \"%s\"\n",argv[ii]); |
59 |
return 3; |
60 |
} |
61 |
|
62 |
fin = fopen(argv[ii], "r"); |
63 |
if (!fin) { |
64 |
printf(" ERROR: cannot fopen() \"%s\"\n",argv[ii]); |
65 |
return 4; |
66 |
} |
67 |
|
68 |
fprintf(fout," %s{ \"%s\", %d,", ((ii==2) ? " " : ","), |
69 |
argv[ii], nbytes); |
70 |
while(fin) { |
71 |
size_t ir; |
72 |
size_t nread; |
73 |
|
74 |
/* fgets(buf, 255, fin); */ |
75 |
nread = fread(buf, sizeof(char), 200, fin); |
76 |
if (nread < 1) |
77 |
break; |
78 |
fprintf(fout,"\n\""); |
79 |
for (ir=0; ir<nread; ir++) { |
80 |
fprintf(fout,"\\x%02x",buf[ir]); |
81 |
/* |
82 |
if (isalnum(buf[ir])) |
83 |
fprintf(fout,"%c",buf[ir]); |
84 |
else |
85 |
fprintf(fout,"\\x%02x",buf[ir]); |
86 |
*/ |
87 |
} |
88 |
fprintf(fout,"\""); |
89 |
} |
90 |
fclose(fin); |
91 |
fprintf(fout," }\n"); |
92 |
} |
93 |
fprintf(fout," };\n"); |
94 |
fclose(fout); |
95 |
|
96 |
return 0; |
97 |
} |
98 |
|