CVS resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / kwsys / EncodeExecutable.c
blob7e96fa90da44910b0d0fd3e22eb7f48f375d647f
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 =========================================================================*/
14 #include <stdio.h>
15 #ifdef __WATCOMC__
16 #define _unlink unlink
17 #endif
18 int main(int argc, char* argv[])
20 FILE* ifp;
21 FILE* ofp;
22 int i;
23 int n;
24 int count = 0;
25 unsigned char buffer[1024];
27 /* Check arguments. */
28 if(argc != 5)
30 fprintf(stderr, "Usage: %s <input> <output> <kwsys-name> <array>\n",
31 argv[0]);
32 return 1;
35 /* Open the input file. */
36 ifp = fopen(argv[1], "rb");
37 if(!ifp)
39 fprintf(stderr, "Cannot open input file: \"%s\"\n", argv[1]);
40 return 2;
42 ofp = fopen(argv[2], "w");
43 if(!ofp)
45 fprintf(stderr, "Cannot open output file: \"%s\"\n", argv[2]);
46 return 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");
60 fprintf(ofp, "\n");
61 fprintf(ofp, "static void kwsys_unlink(const char* fname)\n");
62 fprintf(ofp, "{\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");
68 fprintf(ofp, "}\n");
69 fprintf(ofp, "\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",
75 argv[4], count++, n);
76 for(i=0; i < n-1; ++i)
78 fprintf(ofp, "0x%02X", buffer[i]);
79 if(i%10 == 9)
81 fprintf(ofp, ",\n");
83 else
85 fprintf(ofp, ", ");
88 fprintf(ofp, "0x%02X};\n\n", buffer[n-1]);
90 fclose(ifp);
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]);
95 fprintf(ofp, "{\n");
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");
112 fprintf(ofp, "}\n");
113 fclose(ofp);
114 return 0;