portname: use a subdir and emu.file so they can be packaged in zips (nw)

This commit is contained in:
cracyc 2017-12-30 18:56:31 -06:00
parent 3bb13e40d6
commit 3f21951418

View File

@ -24,15 +24,17 @@ function portname.startplugin()
end end
emu.register_start(function() emu.register_start(function()
local file = io.open(ctrlrpath .. "/" .. get_filename(), "r") local file = emu.file(ctrlrpath .. "/portname", "r")
if not file then local ret = file:open(get_filename())
file = io.open(ctrlrpath .. "/" .. get_filename(true), "r") if ret then
if not file then ret = file:open(get_filename(true))
if ret then
return return
end end
end end
local names = file:read(file:size())
local orig, rep local orig, rep
for line in file:lines() do names:gsub("[^\n\r]*", function (line)
if line:find("^msgid") then if line:find("^msgid") then
orig = line:match("^msgid \"(.+)\"") orig = line:match("^msgid \"(.+)\"")
elseif line:find("^msgstr") then elseif line:find("^msgstr") then
@ -47,8 +49,8 @@ function portname.startplugin()
end end
end end
end end
end return line
file:close() end)
end) end)
local function menu_populate() local function menu_populate()
@ -69,35 +71,44 @@ function portname.startplugin()
end end
end end
end end
local attr = lfs.attributes(ctrlrpath) local function check_path(path)
if not attr then local attr = lfs.attributes(path)
lfs.mkdir(ctrlrpath) if not attr then
if not lfs.attributes(ctrlrpath) then lfs.mkdir(path)
if not lfs.attributes(path) then
manager:machine():popmessage(_("Failed to save input name file"))
emu.print_verbose("portname: unable to create path " .. path .. "\n")
return false
end
elseif attr.mode ~= "directory" then
manager:machine():popmessage(_("Failed to save input name file")) manager:machine():popmessage(_("Failed to save input name file"))
emu.print_verbose("portname: unable to create ctrlrpath " .. ctrlrpath .. "\n") emu.print_verbose("portname: path exists but isn't directory " .. path .. "\n")
return false return false
end end
elseif attr.mode ~= "directory" then return true
manager:machine():popmessage(_("Failed to save input name file")) end
emu.print_verbose("portname: ctrlrpath exists but isn't directory " .. ctrlrpath .. "\n") if not check_path(ctrlrpath) then
return false
end
if not check_path(ctrlrpath .. "/portname") then
return false return false
end end
local filename = get_filename() local filename = get_filename()
local file = io.open(ctrlrpath .. "/" .. filename, "r") local file = io.open(ctrlrpath .. "/portname/" .. filename, "r")
if file then if file then
emu.print_verbose("portname: input name file exists " .. filename .. "\n") emu.print_verbose("portname: input name file exists " .. filename .. "\n")
manager:machine():popmessage(_("Failed to save input name file")) manager:machine():popmessage(_("Failed to save input name file"))
file:close() file:close()
return false return false
end end
file = io.open(ctrlrpath .. "/" .. filename, "w") file = io.open(ctrlrpath .. "/portname/" .. filename, "w")
for def, custom in pairs(fields) do for def, custom in pairs(fields) do
def = def:gsub("[\\\"]", function (s) return "\\" .. s end) def = def:gsub("[\\\"]", function (s) return "\\" .. s end)
custom = custom:gsub("[\\\"]", function (s) return "\\" .. s end) custom = custom:gsub("[\\\"]", function (s) return "\\" .. s end)
file:write("msgid \"" .. def .."\"\nmsgstr \"" .. custom .. "\"\n\n") file:write("msgid \"" .. def .."\"\nmsgstr \"" .. custom .. "\"\n\n")
end end
file:close() file:close()
manager:machine():popmessage(_("Input port name file saved to ") .. ctrlrpath .. "/" .. filename) manager:machine():popmessage(_("Input port name file saved to ") .. ctrlrpath .. "/portname/" .. filename)
end end
return false return false
end end