Last active
August 29, 2015 13:56
-
-
Save jprjr/9210572 to your computer and use it in GitHub Desktop.
Lua Indexed-List Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env lua | |
-- I'm using luajson here | |
local json = require("json"); | |
local list_file = "data.lst"; | |
local index_file = "data.idx"; | |
-- returns just the list index | |
function list_load_index() | |
local index; | |
local ifile, err = io.open(index_file,"r"); | |
if not ifile then | |
return nil; | |
end | |
index = json.decode(ifile:read("*a")); | |
ifile:close(); | |
return index; | |
end | |
-- store a passed index | |
function list_store_index(index) | |
local ifile = io.open(index_file,"w"); | |
ifile:write(json.encode(index).."\n"); | |
ifile:close() | |
end | |
-- appends data to a list | |
-- optional [i] argument replaces item | |
function list_append(data,i) | |
local index = list_load_index(); | |
if not index then | |
-- appending to a brand new list, just call list_store | |
list_store(data); | |
return 1; | |
end | |
local i = i or #index+1; | |
index[i] = {}; | |
datafile = io.open(list_file,"a"); | |
index[i].pos = datafile:seek(); | |
datafile:write(json.encode(data).."\n") | |
index[i].length = datafile:seek() - index[i].pos; | |
datafile:close(); | |
list_store_index(index); | |
end | |
-- loads the list in its entirety | |
function list_load() | |
local index = list_load_index(); | |
local datatable = {}; | |
if not index then | |
return datatable; | |
end | |
datafile = io.open(list_file,"r"); | |
if #index < 1 then | |
return datatable; | |
end | |
for i=1,#index do | |
datafile:seek("set",index[i].pos); | |
datatable[i] = json.decode(datafile:read(index[i].length)); | |
end | |
return datatable; | |
end | |
-- overwrites the list entirely | |
function list_store(data) | |
local index = {}; | |
local file, err = io.open(list_file,"w"); | |
for i, item in ipairs(data) do | |
index[i] = {} | |
index[i].pos = file:seek(); | |
file:write(json.encode(item).."\n"); | |
index[i].length = file:seek() - index[i].pos; | |
end | |
file:close() | |
list_store_index(index); | |
end | |
print("Creating datalist with two messages"); | |
local data = {}; | |
data[#data+1] = { message = "first message", somenumber = 1 }; | |
data[#data+1] = { message = "second message", somenumber = 5 }; | |
list_store(data); | |
print("Dump of list as json") | |
print("---\n" .. json.encode(list_load()) .. "\n---"); | |
print("Appending a third message"); | |
list_append( { message = "third message", somenumber = 20 } ) | |
print("Dump of list as json") | |
print("---\n" .. json.encode(list_load()) .. "\n---"); | |
print("Replacing the second message"); | |
list_append( { message = "new second message", somenumber = 15}, 2); | |
print("Dump of list as json") | |
print("---\n" .. json.encode(list_load()) .. "\n---"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment