lua-users home
lua-l archive

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



On Feb 08, 2007, at 12:36, George Petsagourakis wrote:

There is no way to parse the following format using your proposed func:

Hmmm... as Mikko Sivulainen mentioned earlier... this is really straightforward using a function environment... perhaps even worth mentioning again :)

assuming a 'config.txt' with the same content as in your example...

--8<--
option1 = "ok"
option2 = true
option3 = 34
--8<--

function config( aPath )
        local aFile, aStatus = io.open( aPath, "rb" )
        local aContent = aFile:read( "*all" )
        local aChunk, aStatus = loadstring( aContent )
        local anEnvironment = {}

        setfenv( aChunk, anEnvironment )
        aChunk()

        return anEnvironment
end

local aConfig = config( "config.txt" )

for aKey, aValue in pairs( aConfig ) do
        print( aKey, aValue )
end

> option3 34
> option2 true
> option1 ok