Imported Upstream version 2.8.10.2
[platform/upstream/cmake.git] / Source / kwsys / EncodeExecutable.c
1 /*============================================================================
2   KWSys - Kitware System Library
3   Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4
5   Distributed under the OSI-approved BSD License (the "License");
6   see accompanying file Copyright.txt for details.
7
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 ============================================================================*/
12 #include <stdio.h>
13 #ifdef __WATCOMC__
14 #define _unlink unlink
15 #endif
16 int main(int argc, char* argv[])
17 {
18   FILE* ifp;
19   FILE* ofp;
20   int i;
21   int n;
22   int count = 0;
23   unsigned char buffer[1024];
24   
25   /* Check arguments.  */
26   if(argc != 5)
27     {
28     fprintf(stderr, "Usage: %s <input> <output> <kwsys-name> <array>\n",
29             argv[0]);
30     return 1;
31     }
32   
33   /* Open the input file.  */
34   ifp = fopen(argv[1], "rb");
35   if(!ifp)
36     {
37     fprintf(stderr, "Cannot open input file: \"%s\"\n", argv[1]);
38     return 2;
39     }
40   ofp = fopen(argv[2], "w");
41   if(!ofp)
42     {
43     fprintf(stderr, "Cannot open output file: \"%s\"\n", argv[2]);
44     fclose(ifp);
45     return 2;
46     }
47   
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");
59   fprintf(ofp, "\n");
60   fprintf(ofp, "static void kwsys_unlink(const char* fname)\n");
61   fprintf(ofp, "{\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");
67   fprintf(ofp, "}\n");
68   fprintf(ofp, "\n");
69   
70   /* Split file up in 1024-byte chunks.  */
71   while((n = (int)fread(buffer, 1, 1024, ifp)) > 0)
72     {
73     fprintf(ofp, "static unsigned char kwsysEncodedArray%s_%d[%d] = {\n", 
74             argv[4], count++, n);
75     for(i=0; i < n-1; ++i)
76       {
77       fprintf(ofp, "0x%02X", buffer[i]);
78       if(i%10 == 9)
79         {
80         fprintf(ofp, ",\n");
81         }
82       else
83         {
84         fprintf(ofp, ", ");
85         }
86       }
87     fprintf(ofp, "0x%02X};\n\n", buffer[n-1]);
88     }
89   fclose(ifp);
90   
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]);
94   fprintf(ofp, "{\n");
95   fprintf(ofp, "  FILE* ofp = fopen(fname, \"wb\");\n");
96   fprintf(ofp, "  if(!ofp) { return 0; }\n");
97   for(i=0; i < count; ++i)
98     {
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");
108     }
109   fprintf(ofp, "  fclose(ofp);\n");
110   fprintf(ofp, "  return 1;\n");
111   fprintf(ofp, "}\n");
112   fclose(ofp);
113   return 0;
114 }