]> pilppa.org Git - familiar-h63xx-build.git/blob - org.handhelds.familiar/packages/cdump/abiword-2.0.12/cdump.c
OE tree imported from monotone branch org.openembedded.oz354fam083 at revision 8b12e3...
[familiar-h63xx-build.git] / org.handhelds.familiar / packages / cdump / abiword-2.0.12 / cdump.c
1 /* AbiSource Build Tools
2  * Copyright (C) 1998 AbiSource, Inc.
3  * 
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  
17  * 02111-1307, USA.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22
23 long _getFileLength(const char* pszFileName)
24 {
25         long iLengthOfFile;
26         
27         FILE* fp = fopen(pszFileName, "rb");
28         if (!fp)
29         {
30                 return -1;
31         }
32
33         if (0 != fseek(fp, 0, SEEK_END))
34         {
35                 fclose(fp);
36                 
37                 return -1;
38         }
39
40         iLengthOfFile = ftell(fp);
41
42         fclose(fp);
43
44         return iLengthOfFile;
45 }
46
47 long _readEntireFile(const char* pszFileName, unsigned char* pBytes, unsigned long iLen)
48 {
49         FILE* fp = fopen(pszFileName, "rb");
50         
51         if (!fp)
52         {
53                 return -1;
54         }
55
56         if (iLen != fread(pBytes, 1, iLen, fp))
57         {
58                 fclose(fp);
59
60                 return -1;
61         }
62
63         fclose(fp);
64
65         return iLen;
66 }
67
68 void _dumpHexCBytes(FILE* fp, const unsigned char* pBytes, long iLen)
69 {
70         long i;
71
72         for (i=0; i<iLen; i++)
73         {
74                 if (i
75                         && ((i % 16) == 0))
76                 {
77                         fprintf(fp, "\n");
78                 }
79                 
80                 fprintf(fp, "0x%02x,", pBytes[i]);
81         }
82
83         fprintf(fp, "\n");
84 }
85
86 int main(int argc, char** argv)
87 {
88         long iLen;
89         unsigned char* pBytes;
90         
91         if (argc != 3)
92         {
93                 fprintf(stderr, "Usage: %s datafile arrayname\n", argv[0]);
94
95                 return -1;
96         }
97
98         iLen = _getFileLength(argv[1]);
99         pBytes = malloc(iLen);
100
101         _readEntireFile(argv[1], pBytes, iLen);
102         
103         printf("unsigned char %s[] = {\n", argv[2]);
104         _dumpHexCBytes(stdout, pBytes, iLen);
105         printf("};\n");
106
107         printf("unsigned long %s_sizeof = sizeof(%s);\n",argv[2],argv[2]);
108         
109         return 0;
110 }