1 /******************************************************************************
2 * Copyright (c) 2011 Texas Instruments Incorporated - http://www.ti.com
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the
14 * distribution.
15 *
16 * Neither the name of Texas Instruments Incorporated nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 *****************************************************************************/
34 /***************************************************************************************
35 * FILE NAME: hex2h.c
36 *
37 * DESCRIPTION: A simple utility to convert an ASCII hex file to a header file .
38 *
39 ***************************************************************************************/
40 #include <stdlib.h>
41 #include <stdio.h>
42 #include <string.h>
44 int main(int argc, char **argv)
45 {
46 FILE *out, *in, *in_hdr;
47 char line[200] = "uint8_t ";
48 char line_hdr[200];
50 if (argc != 4)
51 {
52 printf ("Usage: hfile2array.exe <in_file> <out_file> <array_name>\n");
53 return (-1);
54 }
56 argv++;
58 in = fopen((const char *)(*argv++), "rt");
60 if (in == NULL)
61 {
62 printf ("Can not open %s\n", *argv[0]);
63 return (-1);
64 }
66 out = fopen((const char *)(*argv++), "wt");
67 if (out == NULL)
68 {
69 printf ("Can not open %s\n", *argv[1]);
70 return (-1);
71 }
73 in_hdr = fopen("header.txt", "rt");
75 if (in_hdr == NULL)
76 {
77 printf ("Can not open header.tx\n");
78 return (-1);
79 }
81 while (1)
82 {
83 if (fgets(line_hdr, 200, in_hdr) != NULL)
84 {
85 if (fputs(line_hdr, out) < 0)
86 {
87 printf ("fputs %s error\n", line_hdr);
88 goto error;
89 }
90 }
91 else
92 {
93 /* EOF reached */
94 break;
95 }
96 }
98 strcat (line, (const char *)(*argv++));
99 strcat (line, "[] = {\n");
101 if (fputs(line, out) < 0)
102 {
103 printf ("fputs %s error\n", line);
104 goto error;
105 }
107 while (1)
108 {
109 if (fgets(line, 200, in) != NULL)
110 {
111 if (fputs(line, out) < 0)
112 {
113 printf ("fputs %s error\n", line);
114 goto error;
115 }
116 }
117 else
118 {
119 /* EOF reached */
120 break;
121 }
122 }
124 if (fputs("};\n", out) < 0)
125 {
126 printf ("fputs };\\n error\n");
127 goto error;
128 }
130 fclose(in);
131 fclose(in_hdr);
132 fclose(out);
134 return 0;
136 error:
137 fclose(in);
138 fclose(in_hdr);
139 fclose(out);
141 return (-1);
142 }