--[[ /* * Baltisot * Copyright (C) 1999-2008 Nicolas "Pixel" Noble * * This program 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 2 of the License, or * (at your option) any later version. * * This program 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 this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ ]]-- -- -- Dumps a file or a table to the screen. -- function display(inp, n) local i if (type(inp) == "string") then inp = Input(inp) elseif (type(inp) ~= "table") then error("Display needs a string or an Input object") end if inp.__objname ~= "Handle" then local t = inp inp = Buffer() dumpvars(inp, t, "table") end i = 0 while (not inp:isclosed()) do i = i + 1 print(inp:read()) if ((n ~= nil) and (i >= n)) then return end end end -- -- Squash characters into printable chars. -- function pchar(n) if (not ((n >= 32) and (n <= 127))) then n = 46 -- aka '.' or 0x2e end return hex(n, "%c") end -- -- Does a hexadecimal dump on the screen of the file. -- function hexdump(inp, from, to, width) local size, nlines, remaining, data_array, line, byte, outstring if (type(inp) == "string") then inp = Input(inp) elseif (type(inp) ~= "table") then error("Hexdump needs a string or an Input object") end size = inp:getsize() if (from == nil) then from = 0 end if (to == nil) then to = size end if (to > size) then to = size end size = to - from if (width == nil) then width = 16 end nlines = math.floor(size / width) remaining = math.mod(size, width) inp:seek(from) data_array = inp:read(size) for line = 0, nlines - 1, 1 do outstring = hex(line * width + from, "%08x ") for byte = 0, width - 1, 1 do outstring = outstring .. hex(data_array[line * 16 + byte]) .. " " end outstring = outstring .. " " for byte = 0, width - 1, 1 do outstring = outstring .. pchar(data_array[line * 16 + byte]) end print(outstring) end if (remaining == 0) then return end outstring = hex(nlines * width + from, "%08x "); for byte = 0, remaining - 1, 1 do outstring = outstring .. hex(data_array[nlines * 16 + byte]) .. " " end for byte = remaining + 1, width - 1, 1 do outstring = outstring .. " " end outstring = outstring .. " " for byte = 0, remaining - 1, 1 do outstring = outstring .. pchar(data_array[nlines * 16 + byte]) end print(outstring) end -- -- Splits a string given a separator character into an array. -- function split(str, at) local splut = {} if (type(str) ~= "string") then return nil end if (not str) then str = "" end if (not at) then table.insert(splut, str) else for n, c in string.gfind(str, '([^%'..at..']*)(%'..at..'?)') do table.insert(splut, n) if (c == '') then break end end end return splut end -- -- String replacement functions. -- local sql_replacements = { { "\\", "\\\\" }, { "'", "\\'" }, { '"', '\\"' }, { "\n", "\\n" }, { "\r", "\\r" }, -- { "\0", "\\0" }, { "\026", "\\Z" }, } function generic_escape(str, replacements) local _, r for _, r in ipairs(replacements) do str = string.gsub(str, r[1], r[2]) end return str end -- -- Various, classique trim functions. -- function sql_escape(name) return generic_escape(name, sql_replacements) end function ltrim(str) if string.sub(str, 1, 1) == " " then return ltrim(string.sub(str, 2)) else return str end end function rtrim(str) if string.sub(str, -1) == " " then return rtrim(string.sub(str, 1, string.len(str) - 1)) else return str end end function trim(str) return ltrim(rtrim(str)) end -- -- Displays a table on screen. -- function dtable(x, y) local b = Buffer() if y then dumpvars(b, x, y) else dumpvars(b, x) end display(b) b:close() end