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