luaext.lua

Sun, 06 Sep 2020 22:12:52 +0300

author
Tuomo Valkonen <tuomov@iki.fi>
date
Sun, 06 Sep 2020 22:12:52 +0300
changeset 35
2f927eae429b
parent 3
b2df1b3f2c83
permissions
-rw-r--r--

Don't auto-create directories that will be empty

--
-- ion/share/ioncore_luaext.lua
-- 
-- Copyright (c) Tuomo Valkonen 2004-2009.
--
-- See the included file LICENSE for details.
--

local lfs=require("lfs")
local path=require("mypath")

--DOC
-- Make \var{str} shell-safe.
function string.shell_safe(str)
    return "'"..string.gsub(str, "'", "'\\''").."'"
end


--DOC
-- Make copy of \var{table}. If \var{deep} is unset, shallow one-level
-- copy is made, otherwise a deep copy is made.
function table.copy(t, deep)
    local function docopy(t, deep, seen)
        local nt={}
        for k, v in pairs(t) do
            local v2=v
            if deep and type(v)=="table" then
                if seen[v] then
                    error(TR("Recursive table - unable to deepcopy"))
                end
                seen[v]=true
                v2=docopy(v, deep, seen)
                seen[v]=nil
            end
            nt[k]=v2
        end
        return nt
    end
    return docopy(t, deep, deep and {})
end


--DOC
-- Add entries that do not exist in \var{t1} from \var{t2} to \var{t1}.
function table.append(t1, t2)
    for k, v in pairs(t2) do
        if t1[k]==nil then
            t1[k]=v
        end
    end
    return t1
end


--DOC
-- Create a table containing all entries from \var{t1} and those from
-- \var{t2} that are missing from \var{t1}.
function table.join(t1, t2)
    return table.append(table.copy(t1, false), t2)
end


--DOC
-- Insert all positive integer entries from t2 into t1.
function table.icat(t1, t2)
    for _, v in ipairs(t2) do
        table.insert(t1, v)
    end
    return t1
end


--DOC
-- Map all entries of \var{t} by \var{f}.
function table.map(f, t)
    local res={}
    for k, v in pairs(t) do
        res[k]=f(v)
    end
    return res
end


--DOC
-- Export a list of functions from \var{lib} into global namespace.
function export(lib, ...)
    for k, v in pairs({...}) do
        _G[v]=lib[v]
    end
end

function io.openX(file, mode)
    if mode=='w' then
        local dn = path.dirname(file)
        if not lfs.attributes(dn) then
            print("Ensure directory", dn)
            lfs.mkdir(dn)
        end
    end
    local f, err = io.open(file, mode)
    if not f then
        error(file..": "..err)
    end
    return f
end

mercurial