1 -- Minetest: builtin/misc_helpers.lua
3 --------------------------------------------------------------------------------
4 function basic_dump2(o)
5 if type(o) == "number" then
7 elseif type(o) == "string" then
8 return string.format("%q", o)
9 elseif type(o) == "boolean" then
11 elseif type(o) == "function" then
13 elseif type(o) == "userdata" then
15 elseif type(o) == "nil" then
18 error("cannot dump a " .. type(o))
23 --------------------------------------------------------------------------------
24 function dump2(o, name, dumped)
28 if type(o) == "number" or type(o) == "string" or type(o) == "boolean"
29 or type(o) == "function" or type(o) == "nil"
30 or type(o) == "userdata" then
31 io.write(basic_dump2(o), "\n")
32 elseif type(o) == "table" then
34 io.write(dumped[o], "\n")
37 io.write("{}\n") -- new table
38 for k,v in pairs(o) do
39 local fieldname = string.format("%s[%s]", name, basic_dump2(k))
40 dump2(v, fieldname, dumped)
44 error("cannot dump a " .. type(o))
49 --------------------------------------------------------------------------------
50 function dump(o, dumped)
52 if type(o) == "number" then
54 elseif type(o) == "string" then
55 return string.format("%q", o)
56 elseif type(o) == "table" then
58 return "<circular reference>"
62 for k,v in pairs(o) do
63 t[#t+1] = "[" .. dump(k, dumped) .. "] = " .. dump(v, dumped)
65 return "{" .. table.concat(t, ", ") .. "}"
66 elseif type(o) == "boolean" then
68 elseif type(o) == "function" then
70 elseif type(o) == "userdata" then
72 elseif type(o) == "nil" then
75 error("cannot dump a " .. type(o))
80 --------------------------------------------------------------------------------
81 function string:split(sep)
82 local sep, fields = sep or ",", {}
83 local pattern = string.format("([^%s]+)", sep)
84 self:gsub(pattern, function(c) fields[#fields+1] = c end)
88 --------------------------------------------------------------------------------
89 function file_exists(filename)
90 local f = io.open(filename, "r")
99 --------------------------------------------------------------------------------
100 function string:trim()
101 return (self:gsub("^%s*(.-)%s*$", "%1"))
104 assert(string.trim("\n \t\tfoo bar\t ") == "foo bar")
106 --------------------------------------------------------------------------------
107 function math.hypot(x, y)
113 if x == 0 then return 0 end
115 return x * math.sqrt(1 + t * t)
118 --------------------------------------------------------------------------------
119 function explode_textlist_event(text)
124 local parts = text:split(":")
127 retval.typ = parts[1]:trim()
128 retval.index= tonumber(parts[2]:trim())
130 if type(retval.index) ~= "number" then
138 --------------------------------------------------------------------------------
139 function get_last_folder(text,count)
140 local parts = text:split(DIR_DELIM)
148 retval = retval .. parts[#parts - (count-i)] .. DIR_DELIM
154 --------------------------------------------------------------------------------
155 function cleanup_path(temppath)
157 local parts = temppath:split("-")
160 if temppath ~= "" then
161 temppath = temppath .. "_"
163 temppath = temppath .. parts[i]
166 parts = temppath:split(".")
169 if temppath ~= "" then
170 temppath = temppath .. "_"
172 temppath = temppath .. parts[i]
175 parts = temppath:split("'")
178 if temppath ~= "" then
179 temppath = temppath .. ""
181 temppath = temppath .. parts[i]
184 parts = temppath:split(" ")
187 if temppath ~= "" then
190 temppath = temppath .. parts[i]
196 local tbl = engine or minetest
197 function tbl.formspec_escape(text)
199 text = string.gsub(text,"\\","\\\\")
200 text = string.gsub(text,"%]","\\]")
201 text = string.gsub(text,"%[","\\[")
202 text = string.gsub(text,";","\\;")
203 text = string.gsub(text,",","\\,")
208 --------------------------------------------------------------------------------
209 -- mainmenu only functions
210 --------------------------------------------------------------------------------
211 if engine ~= nil then
212 engine.get_game = function(index)
213 local games = game.get_games()
215 if index > 0 and index <= #games then
222 function fgettext(text, ...)
223 text = engine.gettext(text)
224 local arg = {n=select('#', ...), ...}
226 -- Insert positional parameters ($1, $2, ...)
229 while pos <= text:len() do
230 newpos = text:find('[$]', pos)
231 if newpos == nil then
232 result = result .. text:sub(pos)
235 paramindex = tonumber(text:sub(newpos+1, newpos+1))
236 result = result .. text:sub(pos, newpos-1) .. tostring(arg[paramindex])
242 return engine.formspec_escape(text)
245 --------------------------------------------------------------------------------
247 --------------------------------------------------------------------------------
248 if minetest ~= nil then
249 --------------------------------------------------------------------------------
250 function minetest.pos_to_string(pos)
251 return "(" .. pos.x .. "," .. pos.y .. "," .. pos.z .. ")"