1 /*=========================================================================
3 Program: KWSys - Kitware System Library
4 Module: $RCSfile: EncodeExecutable.c,v $
6 Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved.
7 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the above copyright notices for more information.
13 =========================================================================*/
16 #define _unlink unlink
18 int main(int argc
, char* argv
[])
25 unsigned char buffer
[1024];
27 /* Check arguments. */
30 fprintf(stderr
, "Usage: %s <input> <output> <kwsys-name> <array>\n",
35 /* Open the input file. */
36 ifp
= fopen(argv
[1], "rb");
39 fprintf(stderr
, "Cannot open input file: \"%s\"\n", argv
[1]);
42 ofp
= fopen(argv
[2], "w");
45 fprintf(stderr
, "Cannot open output file: \"%s\"\n", argv
[2]);
49 /* Prepend header comment. */
50 fprintf(ofp
, "/*\n * DO NOT EDIT\n * This file is generated by:\n");
51 fprintf(ofp
, " * %s\n */\n\n", argv
[0]);
52 fprintf(ofp
, "#include \"kwsysPrivate.h\"\n");
53 fprintf(ofp
, "#include KWSYS_HEADER(Configure.h)\n\n");
54 fprintf(ofp
, "#include <stdio.h>\n\n");
55 fprintf(ofp
, "#if defined(_WIN32)\n");
56 fprintf(ofp
, "# include <io.h>\n");
57 fprintf(ofp
, "#else\n");
58 fprintf(ofp
, "# include <unistd.h>\n");
59 fprintf(ofp
, "#endif\n");
61 fprintf(ofp
, "static void kwsys_unlink(const char* fname)\n");
63 fprintf(ofp
, "#if defined(__WATCOMC__)\n");
64 fprintf(ofp
, " unlink(fname);\n");
65 fprintf(ofp
, "#else\n");
66 fprintf(ofp
, " _unlink(fname);\n");
67 fprintf(ofp
, "#endif\n");
71 /* Split file up in 1024-byte chunks. */
72 while((n
= (int)fread(buffer
, 1, 1024, ifp
)) > 0)
74 fprintf(ofp
, "static unsigned char kwsysEncodedArray%s_%d[%d] = {\n",
76 for(i
=0; i
< n
-1; ++i
)
78 fprintf(ofp
, "0x%02X", buffer
[i
]);
88 fprintf(ofp
, "0x%02X};\n\n", buffer
[n
-1]);
92 /* Provide a function to write the data to a file. */
93 fprintf(ofp
, "extern %s_EXPORT int %sEncodedWriteArray%s(const char* fname)\n",
94 argv
[3], argv
[3], argv
[4]);
96 fprintf(ofp
, " FILE* ofp = fopen(fname, \"wb\");\n");
97 fprintf(ofp
, " if(!ofp) { return 0; }\n");
98 for(i
=0; i
< count
; ++i
)
100 fprintf(ofp
, " if(fwrite(kwsysEncodedArray%s_%d, 1,\n"
101 " sizeof(kwsysEncodedArray%s_%d), ofp) !=\n"
102 " sizeof(kwsysEncodedArray%s_%d))\n",
103 argv
[4], i
, argv
[4], i
, argv
[4], i
);
104 fprintf(ofp
, " {\n");
105 fprintf(ofp
, " fclose(ofp);\n");
106 fprintf(ofp
, " kwsys_unlink(fname);\n");
107 fprintf(ofp
, " return 0;\n");
108 fprintf(ofp
, " }\n");
110 fprintf(ofp
, " fclose(ofp);\n");
111 fprintf(ofp
, " return 1;\n");