1 /*============================================================================
2 KWSys - Kitware System Library
3 Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
8 This software is distributed WITHOUT ANY WARRANTY; without even the
9 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License for more information.
11 ============================================================================*/
14 #define _unlink unlink
16 int main(int argc, char* argv[])
23 unsigned char buffer[1024];
25 /* Check arguments. */
28 fprintf(stderr, "Usage: %s <input> <output> <kwsys-name> <array>\n",
33 /* Open the input file. */
34 ifp = fopen(argv[1], "rb");
37 fprintf(stderr, "Cannot open input file: \"%s\"\n", argv[1]);
40 ofp = fopen(argv[2], "w");
43 fprintf(stderr, "Cannot open output file: \"%s\"\n", argv[2]);
48 /* Prepend header comment. */
49 fprintf(ofp, "/*\n * DO NOT EDIT\n * This file is generated by:\n");
50 fprintf(ofp, " * %s\n */\n\n", argv[0]);
51 fprintf(ofp, "#include \"kwsysPrivate.h\"\n");
52 fprintf(ofp, "#include KWSYS_HEADER(Configure.h)\n\n");
53 fprintf(ofp, "#include <stdio.h>\n\n");
54 fprintf(ofp, "#if defined(_WIN32)\n");
55 fprintf(ofp, "# include <io.h>\n");
56 fprintf(ofp, "#else\n");
57 fprintf(ofp, "# include <unistd.h>\n");
58 fprintf(ofp, "#endif\n");
60 fprintf(ofp, "static void kwsys_unlink(const char* fname)\n");
62 fprintf(ofp, "#if defined(__WATCOMC__)\n");
63 fprintf(ofp, " unlink(fname);\n");
64 fprintf(ofp, "#else\n");
65 fprintf(ofp, " _unlink(fname);\n");
66 fprintf(ofp, "#endif\n");
70 /* Split file up in 1024-byte chunks. */
71 while((n = (int)fread(buffer, 1, 1024, ifp)) > 0)
73 fprintf(ofp, "static unsigned char kwsysEncodedArray%s_%d[%d] = {\n",
75 for(i=0; i < n-1; ++i)
77 fprintf(ofp, "0x%02X", buffer[i]);
87 fprintf(ofp, "0x%02X};\n\n", buffer[n-1]);
91 /* Provide a function to write the data to a file. */
92 fprintf(ofp, "extern %s_EXPORT int %sEncodedWriteArray%s(const char* fname)\n",
93 argv[3], argv[3], argv[4]);
95 fprintf(ofp, " FILE* ofp = fopen(fname, \"wb\");\n");
96 fprintf(ofp, " if(!ofp) { return 0; }\n");
97 for(i=0; i < count; ++i)
99 fprintf(ofp, " if(fwrite(kwsysEncodedArray%s_%d, 1,\n"
100 " sizeof(kwsysEncodedArray%s_%d), ofp) !=\n"
101 " sizeof(kwsysEncodedArray%s_%d))\n",
102 argv[4], i, argv[4], i, argv[4], i);
103 fprintf(ofp, " {\n");
104 fprintf(ofp, " fclose(ofp);\n");
105 fprintf(ofp, " kwsys_unlink(fname);\n");
106 fprintf(ofp, " return 0;\n");
107 fprintf(ofp, " }\n");
109 fprintf(ofp, " fclose(ofp);\n");
110 fprintf(ofp, " return 1;\n");