forked from ComputerNerd/Retro-Graphics-Toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filereader.lua
166 lines (163 loc) · 4.59 KB
/
filereader.lua
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
--[[
This file is part of Retro Graphics Toolkit
Retro Graphics Toolkit is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or any later version.
Retro Graphics Toolkit is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Retro Graphics Toolkit. If not, see <http://www.gnu.org/licenses/>.
Copyright Sega16 (or whatever you wish to call me) (2012-2016)
--]]
function mysplit(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={} ; i=1
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
t[i] = str
i = i + 1
end
return t
end
function packCommaStr(w,pstr,hexstr)
local str=''
w=w:gsub('%[','')
w=w:gsub('%]','')
w=w:gsub('{','')
w=w:gsub('}','')
w=w:gsub('=','')
for n in w:gmatch('([^,]+)') do
local nu
if n:sub(1,#hexstr)==hexstr then
nu=tonumber(n:sub(#hexstr+1),16)
else
nu=tonumber(n)
end
if nu==nil then
fltk.alert('tonumber() returned nil.\nInput was: '..n)
else
str=str..string.pack(pstr,nu)
end
end
return str
end
function filereaderProcessText(tp,relptr,offbits,be,t,fname)-- Converts text to tables
if relptr==true then
-- Not yet supported.
fltk.alert('Error: should be unreachable')
else
if tp==rgt.tBinary then
return {{fl.filename_name(fname),t}}-- Simply return a table with one element which contains the input
elseif tp==rgt.tCheader then
t=t:gsub("/%*.-%*/", "" )--From http://rosettacode.org/wiki/Strip_block_comments#Lua
t=t:gsub("//.-[^\r\n]+", "" )
t=t:gsub("static","")
t=t:gsub("const","")
t=t:gsub("long long","int64_t")
t=t:gsub("uint","unsigned int")--Solve two problems at once. Simplifies handling of uintx_t and allows for uint to be used as unsigned int.
t=t:gsub(";","")
t = t:gsub('#include.*["%<].*["%>]', '') -- Remove include statements.
t = t:gsub('(\r?\n)%s*\r?\n', '%1') -- Remove blank lines. Based on: http://lua-users.org/wiki/SciteDeleteBlankLines
local sizes={char='b',short='h',int='i',long='j',int8_t='b',int16_t='h',int32_t='i',int64_t='j'}
local array={}--{unsigned,size,{data}}
local outArray
local firstArray=true
local needsName
local idx=0
local be=false
local unsigned
local holdType
for w in t:gmatch("%S+") do
if firstArray then
idx=idx+1
array[idx]={'',''}
firstArray=false
outArray=true
needsName=true
unsigned=false
holdType=''
end
if outArray then
if w=='unsigned' then
unsigned=true
elseif sizes[w]~=nil then
local chr=sizes[w]
if unsigned then
chr=chr:upper()
end
if be then
holdType='>'..chr
else
holdType='<'..chr
end
outArray=false
else
fltk.alert('Unknown variable keyword: '..w..'\nPlease modify filereader.lua to add support for it. Look for the table called sizes')
return nil
end
else
if needsName then
w=mysplit(w,'[')
array[idx][1]=w[1]
if w[2]~=nil then
array[idx][2]=array[idx][2]..packCommaStr(w[2],holdType,'0x')
end
needsName=false
else
array[idx][2]=array[idx][2]..packCommaStr(w,holdType,'0x')
if w:find('}')~=nil then
firstArray=true
end
end
end
end
return array
elseif tp==rgt.tBEX or tp==rgt.tASM then
local sizes={}
local holdType
if tp==rgt.tASM then
t=t:gsub(";.-[^\r\n]+", "" )
if be then
sizes={['dc.b']='>B',['dc.w']='>H',['dc.l']='>I'}
else
sizes={['dc.b']='<B',['dc.w']='<H',['dc.l']='<I'}
end
local holdType=sizes['dc.b']
else
t=t:gsub("'.-[^\r\n]+", "" )
if be then
sizes={data='>B',dataint='>H',datalong='>I'}
else
sizes={data='<B',dataint='<H',datalong='<I'}
end
local holdType=sizes['data']
end
local array={}
local anyArray=false
local idx=0
for w in t:gmatch("%S+") do
if w:find(':')~=nil then
idx=idx+1
array[idx]={w:gsub(':',''),''}
anyArray=true
else
if anyArray then
if sizes[w]~=nil then
holdType=sizes[w]
else
array[idx][2]=array[idx][2]..packCommaStr(w,holdType,'$')
end
else
fltk.alert('Unhandled word: '..w)
return nil
end
end
end
return array
end
end
return nil
end