diff options
Diffstat (limited to 'tools')
-rw-r--r-- | tools/bin2c.lua | 131 |
1 files changed, 0 insertions, 131 deletions
diff --git a/tools/bin2c.lua b/tools/bin2c.lua deleted file mode 100644 index 352d18e..0000000 --- a/tools/bin2c.lua +++ /dev/null | |||
@@ -1,131 +0,0 @@ | |||
1 | -- | ||
2 | -- BIN2C.LUA [filename] [-o output.lch] | ||
3 | -- | ||
4 | -- Convert files to byte arrays for automatic loading with 'lua_dobuffer'. | ||
5 | -- | ||
6 | -- Based on 'etc/bin2c.c' of Lua 5.0.1 sources by: | ||
7 | -- Luiz Henrique de Figueiredo (lhf@tecgraf.puc-rio.br) | ||
8 | -- | ||
9 | -- Changes: | ||
10 | -- | ||
11 | -- 12-Dec-07/AKa: changed the output to have just the "{ ... }" part; others | ||
12 | -- (variable name) can be explicitly given before the '#include' | ||
13 | -- 16-Nov-07/AKa: taken into luaSub | ||
14 | -- 16-Mar-04/AKa: added 'glua_wrap()' support | ||
15 | -- xx-Jan-04/AKa: subdirectory names are not included in debug info | ||
16 | -- | ||
17 | |||
18 | local function USAGE() | ||
19 | io.stderr:write "lua bin2c.lua [filename] [-o output.lch]" | ||
20 | os.exit(-1) | ||
21 | end | ||
22 | |||
23 | local out_f -- file to output to (stdout if nil) | ||
24 | |||
25 | local function OUT( ... ) | ||
26 | (out_f or io.stdout): write( ... ); -- ; actually needed by Lua | ||
27 | (out_f or io.stdout): write "\n" | ||
28 | end | ||
29 | |||
30 | local HEAD= "{ " | ||
31 | local START= ' ' | ||
32 | local FILL= '%3d,' | ||
33 | local STOP= "" | ||
34 | local TAIL= "};\n" | ||
35 | |||
36 | -- | ||
37 | local function dump( f ) | ||
38 | -- | ||
39 | OUT [[ | ||
40 | /* bin2c.lua generated code -- DO NOT EDIT | ||
41 | * | ||
42 | * To use from C source: | ||
43 | * char my_chunk[]= | ||
44 | * #include "my.lch" | ||
45 | */]] | ||
46 | |||
47 | local str= HEAD..'\n'..START | ||
48 | local len= 0 | ||
49 | |||
50 | while true do | ||
51 | for n=1,20 do | ||
52 | local c= f:read(1) | ||
53 | if c then | ||
54 | str= str..string.format( FILL, string.byte(c) ) | ||
55 | len= len+1 | ||
56 | else | ||
57 | OUT( str..STOP.. string.format( TAIL, len ) ) | ||
58 | return -- the end | ||
59 | end | ||
60 | end | ||
61 | OUT(str..STOP) | ||
62 | str= START | ||
63 | end | ||
64 | end | ||
65 | |||
66 | -- | ||
67 | local function fdump( fn ) | ||
68 | -- | ||
69 | local f= io.open( fn, "rb" ) -- must open as binary | ||
70 | |||
71 | if not f then | ||
72 | error( "bin2c: cannot open "..fn ) | ||
73 | else | ||
74 | dump( f ) | ||
75 | f:close() | ||
76 | end | ||
77 | end | ||
78 | |||
79 | -- | ||
80 | local function main( argv ) | ||
81 | -- | ||
82 | local fn= argv.o | ||
83 | if fn then | ||
84 | local f,err= io.open( fn, "w" ) | ||
85 | assert( f, "Unable to write '"..fn.."': "..(err or "") ) | ||
86 | |||
87 | out_f= f | ||
88 | end | ||
89 | |||
90 | if argv[2] then | ||
91 | USAGE() | ||
92 | elseif argv[1] then | ||
93 | fdump( argv[1] ) | ||
94 | else -- use stdin (no params) | ||
95 | if os.getenv("WINDIR") then | ||
96 | error "using stdin not allowed on Win32!" -- it wouldn't be binary | ||
97 | end | ||
98 | dump(io.stdin) | ||
99 | end | ||
100 | |||
101 | if out_f then | ||
102 | out_f:close() | ||
103 | end | ||
104 | end | ||
105 | |||
106 | -- | ||
107 | local argv= {} | ||
108 | local valid_flags= { o=1 } -- lookup: 0=no value, 1=value | ||
109 | |||
110 | -- Need to use while since '-o' advances 'i' by 2 | ||
111 | -- | ||
112 | local args= select('#',...) | ||
113 | local i=1 | ||
114 | |||
115 | while i<=args do | ||
116 | local v= select(i,...) | ||
117 | local flag= string.match( v, "^%-(.+)" ) | ||
118 | |||
119 | if flag then | ||
120 | if not valid_flags[flag] then | ||
121 | error( "Unknown flag: -"..flag ) | ||
122 | end | ||
123 | argv[flag]= (i+1<=args) and select(i+1,...) or true | ||
124 | i= i+1 | ||
125 | else | ||
126 | table.insert( argv, v ) -- [1..N] | ||
127 | end | ||
128 | i= i+1 | ||
129 | end | ||
130 | |||
131 | return main(argv) | ||