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