lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Wed, May 27, 2009 at 1:07 PM, Bulat Ziganshin
<bulat.ziganshin@gmail.com> wrote:
> path should be removed *before* extension:

Well, that's why I always use a loop myself!  Bear in mind that
backslashes have another meaning in Unix paths.

local function at(s,i)
    return sub(s,i,i)
end

local other_sep
-- sep is the directory separator for this platform.
if is_windows then sep = '\\'; other_sep = '/' else sep = '/' end

--- given a path, return the directory part and a file part.
-- if there's no directory part, the first value will be empty
-- @param path A file path
function splitpath(path)
    local i = #path
    local ch = at(path,i)
    while i > 0 and ch ~= sep and ch ~= other_sep do
        i = i - 1
        ch = at(path,i)
    end
    if i == 0 then
        return '',path
    else
        return sub(path,1,i-1), sub(path,i+1)
    end
end

Not the full solution, but the extension is straightforward; basically
have to check for '.' as well, and see what you end up with as you go
backwards. String patterns are not always the sensible way to do
things.

steve d.