diff options
author | cracyc <cracyc@users.noreply.github.com> | 2017-12-30 18:56:31 -0600 |
---|---|---|
committer | cracyc <cracyc@users.noreply.github.com> | 2017-12-30 18:56:31 -0600 |
commit | 3f219514184dae2eec0f7b8b25f4aeafa5f165f2 (patch) | |
tree | 7e4b471eb834108d7dd4f133ccb11c55c4236910 /plugins | |
parent | 3bb13e40d6124e23b1fdd6c05ed06d257ddd4292 (diff) |
portname: use a subdir and emu.file so they can be packaged in zips (nw)
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/portname/init.lua | 47 |
1 files changed, 29 insertions, 18 deletions
diff --git a/plugins/portname/init.lua b/plugins/portname/init.lua index b30763aee66..bcd38e43809 100644 --- a/plugins/portname/init.lua +++ b/plugins/portname/init.lua @@ -24,15 +24,17 @@ function portname.startplugin() end emu.register_start(function() - local file = io.open(ctrlrpath .. "/" .. get_filename(), "r") - if not file then - file = io.open(ctrlrpath .. "/" .. get_filename(true), "r") - if not file then + local file = emu.file(ctrlrpath .. "/portname", "r") + local ret = file:open(get_filename()) + if ret then + ret = file:open(get_filename(true)) + if ret then return end end + local names = file:read(file:size()) local orig, rep - for line in file:lines() do + names:gsub("[^\n\r]*", function (line) if line:find("^msgid") then orig = line:match("^msgid \"(.+)\"") elseif line:find("^msgstr") then @@ -47,8 +49,8 @@ function portname.startplugin() end end end - end - file:close() + return line + end) end) local function menu_populate() @@ -69,35 +71,44 @@ function portname.startplugin() end end end - local attr = lfs.attributes(ctrlrpath) - if not attr then - lfs.mkdir(ctrlrpath) - if not lfs.attributes(ctrlrpath) then + local function check_path(path) + local attr = lfs.attributes(path) + if not attr 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")) - emu.print_verbose("portname: unable to create ctrlrpath " .. ctrlrpath .. "\n") + emu.print_verbose("portname: path exists but isn't directory " .. path .. "\n") return false end - elseif attr.mode ~= "directory" then - manager:machine():popmessage(_("Failed to save input name file")) - emu.print_verbose("portname: ctrlrpath exists but isn't directory " .. ctrlrpath .. "\n") + return true + end + if not check_path(ctrlrpath) then + return false + end + if not check_path(ctrlrpath .. "/portname") then return false end local filename = get_filename() - local file = io.open(ctrlrpath .. "/" .. filename, "r") + local file = io.open(ctrlrpath .. "/portname/" .. filename, "r") if file then emu.print_verbose("portname: input name file exists " .. filename .. "\n") manager:machine():popmessage(_("Failed to save input name file")) file:close() return false end - file = io.open(ctrlrpath .. "/" .. filename, "w") + file = io.open(ctrlrpath .. "/portname/" .. filename, "w") for def, custom in pairs(fields) do def = def:gsub("[\\\"]", function (s) return "\\" .. s end) custom = custom:gsub("[\\\"]", function (s) return "\\" .. s end) file:write("msgid \"" .. def .."\"\nmsgstr \"" .. custom .. "\"\n\n") end 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 return false end |