1 |
786 |
skrzyp |
#!/usr/bin/env tclsh
|
2 |
|
|
|
3 |
|
|
#===============================================================================
|
4 |
|
|
#
|
5 |
|
|
# file2c.tcl
|
6 |
|
|
#
|
7 |
|
|
# Convert a file into a header that can be #included from C.
|
8 |
|
|
#
|
9 |
|
|
#===============================================================================
|
10 |
|
|
## ####ECOSGPLCOPYRIGHTBEGIN####
|
11 |
|
|
## -------------------------------------------
|
12 |
|
|
## This file is part of eCos, the Embedded Configurable Operating System.
|
13 |
|
|
## Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
|
14 |
|
|
##
|
15 |
|
|
## eCos is free software; you can redistribute it and/or modify it under
|
16 |
|
|
## the terms of the GNU General Public License as published by the Free
|
17 |
|
|
## Software Foundation; either version 2 or (at your option) any later
|
18 |
|
|
## version.
|
19 |
|
|
##
|
20 |
|
|
## eCos is distributed in the hope that it will be useful, but WITHOUT
|
21 |
|
|
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
22 |
|
|
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
23 |
|
|
## for more details.
|
24 |
|
|
##
|
25 |
|
|
## You should have received a copy of the GNU General Public License
|
26 |
|
|
## along with eCos; if not, write to the Free Software Foundation, Inc.,
|
27 |
|
|
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
28 |
|
|
##
|
29 |
|
|
## As a special exception, if other files instantiate templates or use
|
30 |
|
|
## macros or inline functions from this file, or you compile this file
|
31 |
|
|
## and link it with other works to produce a work based on this file,
|
32 |
|
|
## this file does not by itself cause the resulting work to be covered by
|
33 |
|
|
## the GNU General Public License. However the source code for this file
|
34 |
|
|
## must still be made available in accordance with section (3) of the GNU
|
35 |
|
|
## General Public License v2.
|
36 |
|
|
##
|
37 |
|
|
## This exception does not invalidate any other reasons why a work based
|
38 |
|
|
## on this file might be covered by the GNU General Public License.
|
39 |
|
|
## -------------------------------------------
|
40 |
|
|
## ####ECOSGPLCOPYRIGHTEND####
|
41 |
|
|
#===============================================================================
|
42 |
|
|
######DESCRIPTIONBEGIN####
|
43 |
|
|
#
|
44 |
|
|
# Author(s): jlarmour,bartv
|
45 |
|
|
# Contact(s):
|
46 |
|
|
# Date: 2001-07-20
|
47 |
|
|
# Purpose:
|
48 |
|
|
# Description:
|
49 |
|
|
# Usage: file2c.tcl <file to encode> <output C header file>
|
50 |
|
|
#
|
51 |
|
|
#####DESCRIPTIONEND####
|
52 |
|
|
#===============================================================================
|
53 |
|
|
|
54 |
|
|
|
55 |
|
|
|
56 |
|
|
if { $argc != 2 } {
|
57 |
|
|
puts "Usage: file2c.tcl <file to encode> <output C header file>"
|
58 |
|
|
exit 1
|
59 |
|
|
}
|
60 |
|
|
set infile [lindex $argv 0]
|
61 |
|
|
set outfile [lindex $argv 1]
|
62 |
|
|
|
63 |
|
|
set status [ catch {
|
64 |
|
|
set infilefd [open $infile "r"]
|
65 |
|
|
fconfigure $infilefd -translation binary
|
66 |
|
|
set data [read $infilefd]
|
67 |
|
|
close $infilefd
|
68 |
|
|
} message]
|
69 |
|
|
|
70 |
|
|
if { $status != 0 } {
|
71 |
|
|
error "Unable to read file $infile: $message"
|
72 |
|
|
}
|
73 |
|
|
|
74 |
|
|
set result ""
|
75 |
|
|
|
76 |
|
|
set status [ catch {
|
77 |
|
|
set outfilefd [ open $outfile "w" ]
|
78 |
|
|
} message ]
|
79 |
|
|
|
80 |
|
|
if { $status != 0 } {
|
81 |
|
|
error "Unable to create file $outfile: $message"
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
append result "/* This is a generated file. Do not edit. */\n\n"
|
85 |
|
|
append result "static CYGBLD_ATTRIB_ALIGN(4) const unsigned char filedata\[\] = {\n"
|
86 |
|
|
|
87 |
|
|
set datalength [ string length $data ]
|
88 |
|
|
|
89 |
|
|
set aligned_datalength [expr $datalength - ($datalength % 8)]
|
90 |
|
|
|
91 |
|
|
for { set i 0 } {$i < $aligned_datalength} {incr i 8} {
|
92 |
|
|
binary scan $data "@[set i]H16" var0
|
93 |
|
|
append result [format " 0x%2s, 0x%2s, 0x%2s, 0x%2s, 0x%2s, 0x%2s, 0x%2s, 0x%2s,\n" \
|
94 |
|
|
[string range $var0 0 1] \
|
95 |
|
|
[string range $var0 2 3] \
|
96 |
|
|
[string range $var0 4 5] \
|
97 |
|
|
[string range $var0 6 7] \
|
98 |
|
|
[string range $var0 8 9] \
|
99 |
|
|
[string range $var0 10 11] \
|
100 |
|
|
[string range $var0 12 13] \
|
101 |
|
|
[string range $var0 14 15]]
|
102 |
|
|
}
|
103 |
|
|
|
104 |
|
|
if { $aligned_datalength != $datalength } {
|
105 |
|
|
append result " "
|
106 |
|
|
for { set i $aligned_datalength } {$i < $datalength} {incr i} {
|
107 |
|
|
binary scan $data "@[set i]H2" var0
|
108 |
|
|
append result [format "0x%2s, " $var0]
|
109 |
|
|
}
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
# Remove either comma+newline or comma+space from the end
|
113 |
|
|
set result [string range $result 0 [expr [string length $result] - 3]]
|
114 |
|
|
|
115 |
|
|
append result "\n};"
|
116 |
|
|
|
117 |
|
|
puts $outfilefd $result
|
118 |
|
|
close $outfilefd
|