1
0
Fork 0
forked from len0rd/rockbox

lua move strip_extension and create_numbered_filename out of main binary

rb.strip_extension and rb.create_numbered_filename
have been moved to include_lua/files.lua
to use simply add require('files') to your script

Change-Id: I95af7b312c8614cb10da4b71b22714b3e282e08a
This commit is contained in:
William Wilgus 2019-09-27 20:08:58 -05:00
parent 4fb783582f
commit 01cccaf2d2
4 changed files with 64 additions and 1 deletions

View file

@ -0,0 +1,42 @@
rb = rb or {}
rb.create_numbered_filename = function (sPath, sPrefix, sSuffix, iNumLen, iNum)
iNum = iNum or -1
local dir_iter, dir_data = luadir.dir(sPath)
local status = true
local name, isdir, num
local name_pat = sPrefix .. '(%d+)' .. sSuffix
local file_pat
local max_num = iNum < 0 and -1 or iNum -- Number specified
if max_num < 0 then
max_num = 0 -- automatic numbering
repeat
status, name, isdir = pcall(dir_iter, dir_data)
if status then
if name and not isdir then
num = string.match(name, name_pat)
if (not iNumLen) and num then -- try to match existing zero padding
local s, e = string.find(num, "^0+")
if s and e then iNumLen = (e - s) end
end
num = tonumber(num)
if num and (num > max_num) then
max_num = num
end
end
end
until not status
end
max_num = max_num + 1
iNumLen = iNumLen or 0
file_pat = "%s/%s%0" .. iNumLen .. "d%s"
return string.format(file_pat, sPath, sPrefix, max_num, sSuffix), max_num
end
rb.strip_extension = function (sFileName)
sFileName = sFileName or ""
local ext = rb.strrchr(sFileName, string.byte("."));
local len = string.len(ext or "")
if len > 0 then sFileName = string.sub(sFileName, 1, -(len + 1)) end
return sFileName
end

View file

@ -17,7 +17,7 @@ OTHER_SRC += $(LUA_SRC)
LUA_INCLUDEDIR := $(LUA_SRCDIR)/include_lua
LUA_INCLUDELIST := $(addprefix $(LUA_BUILDDIR)/,audio.lua blit.lua color.lua draw.lua draw_floodfill.lua draw_poly.lua \
draw_num.lua draw_text.lua image.lua image_save.lua lcd.lua math_ex.lua \
draw_num.lua draw_text.lua files.lua image.lua image_save.lua lcd.lua math_ex.lua \
print.lua timer.lua playlist.lua pcm.lua sound.lua \
rbcompat.lua rbsettings.lua poly_points.lua printtable.lua)

View file

@ -637,6 +637,7 @@ RB_WRAP(buttonlight_brightness_set)
/* DEVICE STRING / FILENAME MANIPULATION */
#if 0 /*See files.lua */
RB_WRAP(strip_extension)
{
const char* filename = luaL_checkstring(L, -1);
@ -672,6 +673,7 @@ RB_WRAP(create_numbered_filename)
return 1;
}
#endif
RB_WRAP(utf8encode)
{
@ -697,6 +699,7 @@ RB_WRAP(strncasecmp)
return 1;
}
/* ROCKBOX SETTINGS / INFO */
static int mem_read_write(lua_State *L, uintptr_t address, size_t maxsize, bool isstr_p)
{
if(isstr_p) /*pointer to string (**char)*/
@ -844,6 +847,12 @@ RB_WRAP(audio_current_track)
return mem_read_write(L, address, maxsize, isstr_p);
}
RB_WRAP(settings_save)
{
rb->settings_save();
return 0;
}
#if 0
RB_WRAP(read_mem)
{
@ -906,6 +915,12 @@ RB_WRAP(restart_lua)
return -1;
}
RB_WRAP(show_logo)
{
rb->show_logo();
return 0;
}
#define RB_FUNC(func) {#func, rock_##func}
#define RB_ALIAS(name, func) {name, rock_##func}
static const luaL_Reg rocklib[] =
@ -967,8 +982,10 @@ static const luaL_Reg rocklib[] =
#endif
/* DEVICE STRING / FILENAME MANIPULATION */
#if 0 /*See files.lua */
RB_FUNC(strip_extension),
RB_FUNC(create_numbered_filename),
#endif
RB_FUNC(utf8encode),
RB_FUNC(strncasecmp),
@ -977,6 +994,7 @@ static const luaL_Reg rocklib[] =
RB_FUNC(global_settings),
RB_FUNC(audio_next_track),
RB_FUNC(audio_current_track),
RB_FUNC(settings_save),
/* SPEAKING */
{"talk_number", rock_talk},
@ -985,6 +1003,7 @@ static const luaL_Reg rocklib[] =
/* MISC */
RB_FUNC(restart_lua),
RB_FUNC(show_logo),
{NULL, NULL}
};

View file

@ -109,8 +109,10 @@ my @forbidden_functions = ('^atoi$',
'^plugin_get_current_filename$',
'^plugin_release_audio_buffer$',
'^reload_directory$',
'^settings_save$',
'^set_current_file$',
'^set_dirfilter$',
'^show_logo$',
'^sleep$',
'^system_memory_guard$',
'^system_sound_play$',