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;
47 char line[200] = "uint8_t ";
49 if (argc != 4)
50 {
51 printf ("Usage: hfile2array.exe <in_file> <out_file> <array_name>\n");
52 return (-1);
53 }
55 argv++;
57 in = fopen((const char *)(*argv++), "rt");
59 if (in == NULL)
60 {
61 printf ("Can not open %s\n", *argv[0]);
62 return (-1);
63 }
65 out = fopen((const char *)(*argv++), "wt");
66 if (out == NULL)
67 {
68 printf ("Can not open %s\n", *argv[1]);
69 return (-1);
70 }
71 strcat (line, (const char *)(*argv++));
72 strcat (line, "[] = {\n");
74 if (fputs(line, out) != 0)
75 {
76 printf ("fputs %s error\n", fputs);
77 goto error;
78 }
80 while (1)
81 {
82 if (fgets(line, 200, in) != NULL)
83 {
84 if (fputs(line, out) != 0)
85 {
86 printf ("fputs %s error\n", fputs);
87 goto error;
88 }
89 }
90 else
91 {
92 /* EOF reached */
93 break;
94 }
95 }
97 if (fputs("};\n", out) != 0)
98 {
99 printf ("fputs %s error\n", fputs);
100 goto error;
101 }
103 fclose(in);
104 fclose(out);
106 return 0;
108 error:
109 fclose(in);
110 fclose(out);
112 return (-1);
113 }