[09 Jul 2012] Lua-Table parsing

Contains all messages from the Hollywood mailing list between 01/2006 and 08/2012
Locked
Robin
Posts: 16
Joined: Sun Feb 14, 2010 6:29 pm

[09 Jul 2012] Lua-Table parsing

Post by Robin »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Mon, 9 Jul 2012 18:19:45 +0200

Hi, Perhaps someone can point me into the right direction :) I struggle to import from a file that has a few lua tables. I only want to import one and I managed to read in the file and extract the table and created a string that looks like this (30k chars and more)

Code: Select all

p_Tab = {["p_Sub1"] = {["p_Sub2"] = {["p_Sub3"] = {["p_Data1"] =
1,["p_Data2"] = "test",["p_Data3"] = "blub"}}}}
Is there an easy way to load it back into a table ? Or would this require me parsing the string on my own ? Or is

My loading script looks like this. p_Append just strips comments before joining the lines:

Code: Select all

Local id = OpenFile(Nil, "tables.lua", #MODE_READ);
Function p_Append(s$,x$)
	Local f = FindStr(s$, "--")
	If(f > 0)
		s$ = LeftStr(s$, (f - 1))
	EndIf
	x$ = x$ .. s$
	Return(x$)
EndFunction
Local str = ""
Local start = False
While Not Eof(id)
	Local li$ = ReadLine(id)
	If(li$ = "p_Tab = {")
		start = True
	EndIf
	If(start = True)
		str$ = p_Append(li$,str$)
		If(li$ = "}")
			start = False
		EndIf
	EndIf
Wend
StringToFile(str$, "Test.lua")
DebugPrint("Done.",StrLen(str$))

Dwayne

[10 Jul 2012] RE: Lua-Table parsing

Post by Dwayne »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Tue, 10 Jul 2012 10:34:45 +0000

This is what I do. I have two helper files for you to play around with. The first is a table file and the second is a basic database file (not a real implemented database but hopefully you will understand how they work.

You can save the files as db.hws and table.hws and include it in your program. I will give you a short snippet as an example of how I use it.

THIS IS THE DATBASE HELPER FILE WHICH REQUIRES THE TABLE HELPER FILE SEE BELOW.

Code: Select all

If (not def_db)
    db=table.init ({id=1, type="db", index={}, freeids={}})

    db.sanitise=Function (line$) Return (ReplaceStr (line$, "\"", "\'")) EndFunction

    db.encapsulate=Function (line$)
        line$=ReplaceStr (line$, "\"", "\'")
        Return ("\"" .. line$ .. "\"")
    EndFunction

    db.Load=Function (file$, options)
        options=table.init (options)
        Local tempfile$=""

        If (options.compressed=True)
            tempfile$=GetTempFileName ()
            DecompressFile (file$, tempfile$)
            file$=tempfile$
        EndIf

        dbid=OpenFile (Nil, file$, #MODE_READ)
        Local dba=ReadTable (dbid)
        CloseFile (dbid)

        If (tempfile$ <> "") Then DeleteFile (tempfile$)
        Return (dba)
    EndFunction

    db.Save=Function (dba, file$, options)
        Local write=Function (table, file$)
            Local dbid=OpenFile (Nil, file$, #MODE_WRITE)
            WriteTable (dbid, dba)
            CloseFile (dbid)
        EndFunction

        options=table.init (options)
        Local tempfile$=""

        If (options.compressed=True)
            tempfile$=GetTempFileName ()
            write (dba, tempfile$)
            CompressFile (tempfile$, file$)
            DeleteFile (tempfile$)
        Else
            write (dba, file$)
        EndIf
    EndFunction

    Global def_db=True
EndIf
THIS IS THE TABLE HELPER FILE

Code: Select all

If not (def_table)
    table={}

    table.size=function (t)
      local n = 0
      for _ in pairs (t)
        n = n + 1
      next
      return (n)
    endfunction

    table.insert=Function (t, i, p)
        if (p=Nil) Then p=-1
        If (GetType (i)=#TABLE)
            table.merge (t[i], i)
        Else
            InsertItem (t, i, p)
        EndIf
    EndFunction

    table.empty=Function (t)
      for _ in pairs (t)
        return (false)
      next
      return (true)
    endfunction

    table.clone=Function (t)
        Local Function copy (t)
            local r={}
            local k, v
            for k, v in pairs (t)
                if (GetType(v)=#TABLE)
                    r[k]={}
                    r[k]=copy (v)
                    SetMetaTable (r[k], table.metatable)
                else
                    r[k]=v
                endif
            next
            return (r)
        endfunction
        Local r={}
        local k, v
        for k, v in Pairs (t)
            if (GetType (v)=#TABLE)
                r[k]={}
                r[k]=copy (v)
                SetMetaTable (r[k], table.metatable)
            else
                r[k]=v
            endif
        next
        SetMetaTable (r, table.metatable)
        return (r)
    endfunction

    table.merge=Function (t, u)
        Local Function copy (r, t)
            Local k, v
            for k, v in Pairs (t)
                if (GetType(v)=#TABLE)
                    If (GetType (r[k])<> #TABLE) Then r[k]={}
                    r[k]=copy (r[k], v)
                    SetMetaTable (r[k], table.metatable)
                Else
                    if (GetType (k)=#NUMBER) Then InsertItem (r, v) Else r[k]=v
                EndIf
            Next
            Return (r)
        EndFunction
        Local k, v
        Local r=table.clone (t)
        for k, v in Pairs (u)
            If (GetType (v)=#TABLE)
                If (GetType (r[k])<> #TABLE) Then r[k]={}
                r[k]=copy (r[k], v)
                SetMetaTable (r[k], table.metatable)
            Else
                if (GetType (k)=#NUMBER) Then InsertItem (r, v) Else r[k]=v
            EndIf
        Next
        SetMetaTable (r, table.metatable)
        Return (r)
    EndFunction

    table.metatable={
        __index=Function (t, k)
            Return (RawGet (t, k))
        EndFunction,

        __newindex=Function (t, k, v)
            RawSet (t, k, v)
            If (GetType (v)=#TABLE)
                SetMetaTable (t[k], table.metatable)
            EndIf
        EndFunction
        ,
        __concat=Function (t, u)
            Return (table.merge (t, u))
        EndFunction
        ,
        __tostring=Function (t)
           iterate=Function (t, s, i, o)
                Local k, v
                for k, v in Pairs (t)
                    s=s .. i .. k .. "="
                    if (GetType (v)=#TABLE)
                        if (v=t)
                            s=s .. "[self]"
                        Else
                            if (table.empty (v))
                                s=s .. "{}"
                            Else
                                s=s .. "{\n"
                                s=iterate (v, s, i .. "    ", i)
                            EndIf
                        EndIf
                    Else
                        v=ReplaceStr (ToString (v), "%", "%%")
                        s=s .. v
                    EndIf
                    s=s .. "\n"
                Next
                Return (s .. o .. "}")
            EndFunction

            Local i="    "
            Local s="{\n"

            Local k, v
            for k, v in Pairs (t)
                s=s .. i .. k .. "="
                if (GetType (v)=#TABLE)
                    if (v=t)
                        s=s .. "[self]"
                    Else
                        if (table.empty (v))
                            s=s .. "{}"
                        Else
                            s=s .. "{\n"
                            s=iterate (v, s, i .. "    ", i)
                        EndIf
                    EndIf
                Else
                    v=ReplaceStr (ToString (v), "%", "%%")
                    s=s .. v
                EndIf
                s=s .. "\n"
            Next
            Return (s .. "}")
        EndFunction
        ,
        __call=Function ()
;            DebugPrint ("call")
        EndFunction
    }

    table.init=Function (t)
        If (GetType (t)=#NIL)
            t={}
            setmetatable (t, table.metatable)
        else
            t=table.clone (t)
        Endif
        return (t)
    EndFunction

    Global def_table=True
EndIf
THIS IS THE SHORT CODE SNIPPET TO USE BOTH HELPER FILES

Code: Select all

@INCLUDE "stdlib/table.hws" ; this assumes you have the helper files in a sub folder called stdlib (this is what I do) if you don't then remove stdlib from the path
@INCLUDE "stdlib/db.hws"

Local tvshows, actors={}, {}
tvshows, actors=ScrapePath() ; Note SrapePath is a function that modifies the table - you obviously wont have this function in your code so insert table modifying code here.
db.Save (tvshows, "database/tvshows.dba", {compressed=True}) ; table, file location , table of OPTIONS - so far they are {compressed=True} or {compressed=False} - this is critical to know when you load the table again
db.Save (actors, "database/actors.dba", {compressed=True})

tvshows=db.Load ("database/tvshows.dba", {compressed=True}) ; file location , table of OPTIONS - so far they are {compressed=True} or {compressed=False} - compressed option must match option that table was saved in.
feel free to take what you want from the code. You may want to Hollywoodify it too. With p_ at the start of functions etc. I don't do that as I use the object oriented way of declaring tables as functions etc.

Regards,

Dwayne

Robin
Posts: 16
Joined: Sun Feb 14, 2010 6:29 pm

[10 Jul 2012] RE: Lua-Table parsing

Post by Robin »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Tue, 10 Jul 2012 16:47:39 +0200

Hi Dwayne,
You can save the files as db.hws and table.hws and include it in your program. I will give you a short snippet as an example of how I use it.
Thank you for your code. But I confess I've no clue how I would start to fill your db or table ... my problem is not to load and store an existing table/db. I want to parse a variable external file that has different lua tables that I'd like to be able to access like a table in the programm.

So I'd like to read in a textfile (which is not available at compilation time) eg:

Code: Select all

table = {
  ["key"] = "Hello World",
}
and I'd like to import this into a table that i can access as if it was created in hollywood code.

Code: Select all

local tbl = p_ReadAndParse("textfile.lua")
print(tbl['key'])  -- result 'Hello World'

Locked