forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin2c.tcl
94 lines (66 loc) · 2.32 KB
/
bin2c.tcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
proc convert_bin_to_c {bin_file_name} {
# Get binary file size
if {[catch {file size $bin_file_name} bin_file_size]} {
puts stderr "Error: unable to get size of $bin_file_name"
exit
}
puts "$bin_file_name is $bin_file_size bytes long."
# Open binary file
if {[catch {open $bin_file_name r} bin_file_id]} {
puts stderr "Error: unable to open $bin_file_name"
exit
}
puts "$bin_file_name is open for reading."
# Read contents of binary file
fconfigure $bin_file_id -translation binary
if {[catch {read $bin_file_id} contents]} {
puts stderr "Error: unable to read contents of $bin_file_name"
exit
}
set contents_length [string length $contents]
puts "$bin_file_name contents is stored ($contents_length bytes)."
if {$contents_length != $bin_file_size} {
puts stderr "Error: file size does not match length of contents"
exit
}
# Close binary file
if {[catch {close $bin_file_id}]} {
puts stderr "Error: unable to close $bin_file_name"
}
puts "$bin_file_name is closed."
# Open C file
set bin_file_rootname [file rootname $bin_file_name]
set bin_file_tail [file tail $bin_file_rootname]
set c_file_name "${bin_file_rootname}.cxx"
if {[catch {open $c_file_name w} c_file_id]} {
puts stderr "Error: unable to open $c_file_name"
exit
}
puts "$c_file_name is open for writing."
# Convert bin to C
puts "$c_file_name is filled."
puts $c_file_id "size_t face_${bin_file_tail}_buffer_length = $contents_length;\n"
puts -nonewline $c_file_id "unsigned char face_${bin_file_tail}_buffer\[\] = {\n "
for {set i 0} {$i < $contents_length} {incr i} {
scan [string index $contents $i] "%c" byte
puts -nonewline $c_file_id [format "%3d, " $byte]
if {[expr $i % 14] == 13} {
puts -nonewline $c_file_id "\n "
}
}
puts $c_file_id "\n};"
# Close C file
if {[catch {close $c_file_id}]} {
puts stderr "Error: unable to close $c_file_name"
}
puts "$c_file_name is closed."
}
if {$argc < 1} {
puts stderr "Error: expecting name of the binary file to convert"
exit
}
foreach arg $argv {
foreach file [glob -nocomplain $arg] {
convert_bin_to_c $file
}
}