A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 42 lines 1.5 kB view raw
1rb = rb or {} 2rb.create_numbered_filename = function (sPath, sPrefix, sSuffix, iNumLen, iNum) 3 iNum = iNum or -1 4 local dir_iter, dir_data = luadir.dir(sPath) 5 local status = true 6 local name, isdir, num 7 local name_pat = sPrefix .. '(%d+)' .. sSuffix 8 local file_pat 9 local max_num = iNum < 0 and -1 or iNum -- Number specified 10 11 if max_num < 0 then 12 max_num = 0 -- automatic numbering 13 repeat 14 status, name, isdir = pcall(dir_iter, dir_data) 15 if status then 16 if name and not isdir then 17 num = string.match(name, name_pat) 18 if (not iNumLen) and num then -- try to match existing zero padding 19 local s, e = string.find(num, "^0+") 20 if s and e then iNumLen = (e - s) end 21 end 22 num = tonumber(num) 23 if num and (num > max_num) then 24 max_num = num 25 end 26 end 27 end 28 until not status 29 end 30 max_num = max_num + 1 31 iNumLen = iNumLen or 0 32 file_pat = "%s/%s%0" .. iNumLen .. "d%s" 33 return string.format(file_pat, sPath, sPrefix, max_num, sSuffix), max_num 34end 35 36rb.strip_extension = function (sFileName) 37 sFileName = sFileName or "" 38 local ext = rb.strrchr(sFileName, string.byte(".")); 39 local len = string.len(ext or "") 40 if len > 0 then sFileName = string.sub(sFileName, 1, -(len + 1)) end 41 return sFileName 42end