plugins/data: loading fixes (nw)

This commit is contained in:
cracyc 2019-11-28 22:35:36 -06:00
parent b4bc924018
commit 8dbea98e20
3 changed files with 50 additions and 50 deletions

View File

@ -3,7 +3,7 @@ local info, ver
local datread = require("data/load_dat")
do
local convert = require("data/button_char")
datread, ver = datread.open("command.dat", "# Command List%-.+hand", convert)
datread, ver = datread.open("command.dat", "# Version:", convert)
end
function dat.check(set, softlist)

View File

@ -36,8 +36,6 @@ local function init()
romset VARCHAR NOT NULL,
data CLOB NOT NULL)]])
db.check("creating marp table")
db.exec("CREATE INDEX \"romset_" .. file .. "\" ON \"" .. file .. "_idx\"(romset)")
db.check("creating marp index")
end
for line in fh:lines() do
@ -61,8 +59,6 @@ local function init()
if dbver then
db.exec("DELETE FROM \"" .. file .. "\"")
db.check("deleting marp")
db.exec("DELETE FROM \"" .. file .. "_idx\"")
db.check("deleting marp index")
stmt = db.prepare("UPDATE version SET version = ? WHERE datfile = ?")
db.check("updating marp version")
else

View File

@ -92,7 +92,6 @@ function datfile.open(file, vertag, fixupcb)
stmt:finalize()
do
local inblock = false
fh:seek("set")
local buffer = fh:read("a")
db.exec("BEGIN TRANSACTION")
@ -100,61 +99,66 @@ function datfile.open(file, vertag, fixupcb)
local function gmatchpos()
local pos = 1
local function iter()
local tag1, tag2, data, start, inblock = false
local tags, data
while not data do
local spos, epos, match = buffer:find("\n($[^\n\r]*)", pos)
local npos
local spos, epos = buffer:find("[\n\r]$[^=]*=[^\n\r]*", pos)
if not spos then
return nil
end
if match ~= "$end" and not inblock then
if not tag1 then
tag1 = match
else
tag2 = match
start = epos + 1
inblock = true
end
elseif inblock == true then
data = buffer:sub(start, spos)
inblock = false
npos, epos = buffer:find("[\n\r]$[%w]*[\n\r]+", epos)
if not npos then
return nil
end
pos = epos
tags = buffer:sub(spos, epos)
spos, npos = buffer:find("[\n\r]$end[\n\r]", epos)
if not spos then
return nil
end
data = buffer:sub(epos, spos)
pos = npos
end
return tag1, tag2, data
return tags, data
end
return iter
end
for info1, info2, data in gmatchpos() do
local tag, set = info1:match("^%$([^%s=]+)=?([^%s]*)")
if set and set ~= "" then
local tags = {}
local sets = {}
tag:gsub("([^,]+)", function(s) tags[#tags + 1] = s end)
set:gsub("([^,]+)", function(s) sets[#sets + 1] = s end)
if #tags > 0 and #sets > 0 then
local tag1 = info2:match("^$([^%s]*)")
data = data:gsub("\r", "") -- strip crs
if fixupcb then
data = fixupcb(data)
end
stmt = db.prepare("INSERT INTO \"" .. file .. "\" VALUES (?)")
db.check("inserting values")
stmt:bind_values(data)
stmt:step()
local row = stmt:last_insert_rowid()
stmt:finalize()
for num1, tag2 in pairs(tags) do
for num2, set in pairs(sets) do
if fixupcb then
fixupcb(data)
end
stmt = db.prepare("INSERT INTO \"" .. file .. "_idx\" VALUES (?, ?, ?, ?)")
db.check("inserting into index")
stmt:bind_values(tag1, tag2, set, row)
stmt:step()
stmt:finalize()
for info, data in gmatchpos() do
local tags = {}
local infotype
for s in info:gmatch("[\n\r]$([^\n\r]*)") do
if s:find("=", 1, true) then
local m1, m2 = s:match("([^=]*)=(.*)")
for tag in m1:gmatch("[^,]+") do
for set in m2:gmatch("[^,]+") do
tags[#tags + 1] = { tag = tag, set = set }
end
end
else
infotype = s
break
end
end
if #tags > 0 and infotype then
data = data:gsub("\r", "") -- strip crs
if fixupcb then
data = fixupcb(data)
end
stmt = db.prepare("INSERT INTO \"" .. file .. "\" VALUES (?)")
db.check("inserting values")
stmt:bind_values(data)
stmt:step()
local row = stmt:last_insert_rowid()
stmt:finalize()
for num, tag in pairs(tags) do
if fixupcb then
fixupcb(data)
end
stmt = db.prepare("INSERT INTO \"" .. file .. "_idx\" VALUES (?, ?, ?, ?)")
db.check("inserting into index")
stmt:bind_values(infotype, tag.tag, tag.set, row)
stmt:step()
stmt:finalize()
end
end
end