Debian Codegraph @

lua/include/table.lua

raw symbols SCIP
1 --[[
2 Copyright (C) 2014 HarpyWar ([email protected])
3
4 This file is a part of the PvPGN Project http://pvpgn.pro
5 Licensed under the same terms as Lua itself.
6 ]]--
7
8
9 -- Clear table
10 function table.clear(_table)
11 for k in pairs(_table) do
12 _table[k] = nil
13 end
14 end
15
16 -- Get table size
17 function table.count(T)
18 if not T or not next(t) then return 0 end
19
20 local count = 0
21 for _ in pairs(T) do count = count + 1 end
22 return count
23 end
24
25 -- Get table slice from offset to length
26 function table.slice(values, offset, length)
27 local res = {}
28 local n = #values
29 -- default values for range
30 offset = offset or 0
31 if offset < 0 or offset > n then
32 return {}
33 end
34 length = length or n-offset
35 if length < 1 then
36 length = 1
37 elseif length > n-offset then
38 length = n-offset
39 end
40
41 for i = offset,offset+length-1 do
42 res[i-offset] = values[i]
43 end
44 return res
45 end
46
47
48 -- (Debug) Print contents of `tbl`, with indentation.
49 -- `indent` sets the initial level of indentation.
50 function table.dump(t, ident)
51 local output = ""
52 if not indent then indent = 0 end
53
54 -- if table has 0 index then display this key at start
55 if not (t[0] == nil) then
56 for k=0, #t do
57 local formatting = "\n" .. string.rep(" ", indent) .. k .. ": "
58 local v = t[k]
59 if type(v) == "table" then
60 v = "[" .. table.print(v, indent+1) .. "]"
61 elseif type(v) == 'boolean' then
62 v = tostring(v)
63 end
64
65 output = output .. formatting .. v
66 end
67 else
68 for k,v in pairs(t) do
69 local formatting = "\n" .. k .. ": "
70 local v = t[k]
71 if type(v) == "table" then
72 v = "[" .. table.print(v, indent+1) .. "]"
73 elseif type(v) == 'boolean' then
74 v = tostring(v)
75 end
76
77 output = output .. formatting .. v
78 end
79 end
80 return output
81 end
82
83 -- Find table key by value
84 function get_key_for_value( t, value )
85 for k,v in pairs(t) do
86 if v==value then return k end
87 end
88 return nil
89 end
90
91 --[[
92 Save Table to File
93 Load Table from File
94 v 1.0
95
96 Lua 5.2 compatible
97
98 Only Saves Tables, Numbers and Strings
99 Insides Table References are saved
100 Does not save Userdata, Metatables, Functions and indices of these
101 ----------------------------------------------------
102 table.save( table , filename )
103
104 on failure: returns an error msg
105
106 ----------------------------------------------------
107 table.load( filename or stringtable )
108
109 Loads a table that has been saved via the table.save function
110
111 on success: returns a previously saved table
112 on failure: returns as second argument an error msg
113 ----------------------------------------------------
114
115 Licensed under the same terms as Lua itself.
116 ]]--
117 do
118 -- declare local variables
119 --// exportstring( string )
120 --// returns a "Lua" portable version of the string
121 local function exportstring( s )
122 return string.format("%q", s)
123 end
124
125 --// The Save Function
126 function table.save( tbl,filename )
127 local charS,charE = " ","\n"
128 local file,err = io.open( filename, "wb" )
129 if err then return err end
130
131 -- initiate variables for save procedure
132 local tables,lookup = { tbl },{ [tbl] = 1 }
133 file:write( "return {"..charE )
134
135 for idx,t in ipairs( tables ) do
136 file:write( "-- Table: {"..idx.."}"..charE )
137 file:write( "{"..charE )
138 local thandled = {}
139
140 for i,v in ipairs( t ) do
141 thandled[i] = true
142 local stype = type( v )
143 -- only handle value
144 if stype == "table" then
145 if not lookup[v] then
146 table.insert( tables, v )
147 lookup[v] = #tables
148 end
149 file:write( charS.."{"..lookup[v].."},"..charE )
150 elseif stype == "string" then
151 file:write( charS..exportstring( v )..","..charE )
152 elseif stype == "number" then
153 file:write( charS..tostring( v )..","..charE )
154 end
155 end
156
157 for i,v in pairs( t ) do
158 -- escape handled values
159 if (not thandled[i]) then
160
161 local str = ""
162 local stype = type( i )
163 -- handle index
164 if stype == "table" then
165 if not lookup[i] then
166 table.insert( tables,i )
167 lookup[i] = #tables
168 end
169 str = charS.."[{"..lookup[i].."}]="
170 elseif stype == "string" then
171 str = charS.."["..exportstring( i ).."]="
172 elseif stype == "number" then
173 str = charS.."["..tostring( i ).."]="
174 end
175
176 if str ~= "" then
177 stype = type( v )
178 -- handle value
179 if stype == "table" then
180 if not lookup[v] then
181 table.insert( tables,v )
182 lookup[v] = #tables
183 end
184 file:write( str.."{"..lookup[v].."},"..charE )
185 elseif stype == "string" then
186 file:write( str..exportstring( v )..","..charE )
187 elseif stype == "number" then
188 file:write( str..tostring( v )..","..charE )
189 end
190 end
191 end
192 end
193 file:write( "},"..charE )
194 end
195 file:write( "}" )
196 file:close()
197 end
198
199 --// The Load Function
200 function table.load( sfile )
201 local ftables,err = loadfile( sfile )
202 if err or not ftables() then return _,err end
203 local tables = ftables()
204
205 for idx = 1,#tables do
206 local tolinki = {}
207 for i,v in pairs( tables[idx] ) do
208 if type( v ) == "table" then
209 tables[idx][i] = tables[v[1]]
210 end
211 if type( i ) == "table" and tables[i[1]] then
212 table.insert( tolinki,{ i,tables[i[1]] } )
213 end
214 end
215 -- link indices
216 for _,v in ipairs( tolinki ) do
217 tables[idx][v[2]],tables[idx][v[1]] = tables[idx][v[1]],nil
218 end
219 end
220 return tables[1]
221 end
222 -- close do
223 end
224
225 -- ChillCode