summaryrefslogtreecommitdiffstats
path: root/plugins/data/load_dat.lua
diff options
context:
space:
mode:
author cracyc <cracyc@users.noreply.github.com>2016-09-05 21:43:17 -0500
committer cracyc <cracyc@users.noreply.github.com>2016-09-05 21:43:17 -0500
commit46ab9213f5f49bc4d6afc63ac03459fde78ddd85 (patch)
treee460e7249fdf7f3ece72be215fb6382abe77010a /plugins/data/load_dat.lua
parent63ecb9a387e236f36d1aa6dcd3035289db3ca830 (diff)
ui/selgame: get dat info from plugin [Carl]
plugins/data: add data plugin [Carl]
Diffstat (limited to 'plugins/data/load_dat.lua')
-rw-r--r--plugins/data/load_dat.lua97
1 files changed, 97 insertions, 0 deletions
diff --git a/plugins/data/load_dat.lua b/plugins/data/load_dat.lua
new file mode 100644
index 00000000000..a113da7fd63
--- /dev/null
+++ b/plugins/data/load_dat.lua
@@ -0,0 +1,97 @@
+local datfile = {}
+
+function datfile.open(file, vertag)
+ local data = {}
+ local ver
+ local filepath
+ local fh
+ for path in mame_manager:ui():options().entries.historypath:value():gmatch("([^;]+)") do
+ filepath = lfs.env_replace(path) .. "/" .. file
+ fh = io.open(filepath, "rb")
+ if fh then
+ break
+ end
+ end
+ if not fh then
+ return nil
+ end
+ do
+ local inblock = false
+ local buffer = fh:read("a")
+ if vertag then
+ local match = buffer:match("^" .. vertag .. "%s([^%s]+)")
+ if match then
+ ver = match
+ end
+ end
+ local function gmatchpos()
+ local pos = 1
+ local function iter()
+ local spos, epos = buffer:find("\n$", pos, true)
+ if not spos then
+ return nil
+ end
+ spos = spos + 1
+ local spos, epos, match = buffer:find("([^\n]+)", spos)
+ pos = epos + 1
+ return match, pos, iter
+ end
+ return iter
+ end
+ for line, epos, iter in gmatchpos() do
+
+ local flag = line:sub(1, 1)
+ if flag ~= "#" then
+ if flag == "$" then
+ if line:sub(1, 4) == "$end" then
+ inblock = false
+ elseif not inblock then
+ local tag, set = line:match("^%$([^%s=]+)=?([^%s]*)")
+ if set and set ~= "" then
+ local tags = {}
+ local sets = {}
+ local tag1 = ""
+ tag:gsub("([^,]+)", function(s) tags[#tags + 1] = s end)
+ set:gsub("([^,]+)", function(s) sets[#sets + 1] = s end)
+ repeat
+ tag1, epos = iter()
+ until tag1:sub(1, 1) == "$"
+ tag1 = tag1:match("^$([^%s]*)")
+ if not data[tag1] then
+ data[tag1] = {}
+ end
+ for num1, tag2 in pairs(tags) do
+ if not data[tag1][tag2] then
+ data[tag1][tag2] = {}
+ end
+ for num2, set in pairs(sets) do
+ data[tag1][tag2][set] = epos
+ end
+ end
+ end
+ inblock = true
+ end
+ end
+ end
+ end
+ end
+ fh:close()
+ fh = io.open(filepath, "r")
+ local function read(tag1, tag2, set)
+ local output = {}
+ if not data[tag1][tag2][set] then
+ return nil
+ end
+ fh:seek("set", data[tag1][tag2][set])
+ for line in fh:lines() do
+ if line == "$end" then
+ return table.concat(output, "\n")
+ end
+ output[#output + 1] = line
+ end
+ end
+
+ return read, ver
+end
+
+return datfile