From 01cccaf2d27dcd92a7a6d4b7f5658e780a6da68c Mon Sep 17 00:00:00 2001 From: William Wilgus Date: Fri, 27 Sep 2019 20:08:58 -0500 Subject: 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 --- apps/plugins/lua/include_lua/files.lua | 42 ++++++++++++++++++++++++++++++++++ apps/plugins/lua/lua.make | 2 +- apps/plugins/lua/rocklib.c | 19 +++++++++++++++ apps/plugins/lua/rocklib_aux.pl | 2 ++ 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 apps/plugins/lua/include_lua/files.lua diff --git a/apps/plugins/lua/include_lua/files.lua b/apps/plugins/lua/include_lua/files.lua new file mode 100644 index 0000000000..ccb36c5157 --- /dev/null +++ b/apps/plugins/lua/include_lua/files.lua @@ -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 diff --git a/apps/plugins/lua/lua.make b/apps/plugins/lua/lua.make index 5e46692f3f..ebdef1e24e 100644 --- a/apps/plugins/lua/lua.make +++ b/apps/plugins/lua/lua.make @@ -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) diff --git a/apps/plugins/lua/rocklib.c b/apps/plugins/lua/rocklib.c index e617f3e4bf..6b24984130 100644 --- a/apps/plugins/lua/rocklib.c +++ b/apps/plugins/lua/rocklib.c @@ -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} }; diff --git a/apps/plugins/lua/rocklib_aux.pl b/apps/plugins/lua/rocklib_aux.pl index 8454da2369..f53fc7bd61 100755 --- a/apps/plugins/lua/rocklib_aux.pl +++ b/apps/plugins/lua/rocklib_aux.pl @@ -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$', -- cgit v1.2.3