diff options
author | William Wilgus <me.theuser@yahoo.com> | 2019-07-26 01:30:00 -0500 |
---|---|---|
committer | William Wilgus <me.theuser@yahoo.com> | 2019-07-29 02:51:29 -0500 |
commit | 90118f14cf078358f9ebdee110450b976c9a9e11 (patch) | |
tree | 1cbde562e0c83719db2e16f23fc33cef690fd2c3 /apps/plugins/lua_scripts/dbgettags.lua | |
parent | 60c5a29408f7ca05a88ce1a98a4858293925169f (diff) | |
download | rockbox-90118f14cf078358f9ebdee110450b976c9a9e11.tar.gz rockbox-90118f14cf078358f9ebdee110450b976c9a9e11.zip |
lua add demo scripts, atexit handler, gui_scrollbar_draw
Change-Id: Ie8794e8a487f73952dae43e036787b6972fdbbee
Diffstat (limited to 'apps/plugins/lua_scripts/dbgettags.lua')
-rw-r--r-- | apps/plugins/lua_scripts/dbgettags.lua | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/apps/plugins/lua_scripts/dbgettags.lua b/apps/plugins/lua_scripts/dbgettags.lua new file mode 100644 index 0000000000..06fa6e8830 --- /dev/null +++ b/apps/plugins/lua_scripts/dbgettags.lua @@ -0,0 +1,116 @@ +--dbgettags.lua Bilgus 2018 +--[[ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2017 William Wilgus + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ +]] + +require("actions") +local CANCEL_BUTTON = rb.actions.PLA_CANCEL + +local sINVALIDDATABASE = "Invalid Database" +local sERROROPENING = "Error opening" + +-- tag cache header +local sTCVERSION = string.char(0x0F) +local sTCHEADER = string.reverse("TCH" .. sTCVERSION) +local DATASZ = 4 -- int32_t +local TCHSIZE = 3 * DATASZ -- 3 x int32_t + +local function bytesLE_n(str) + str = str or "" + local tbyte={str:byte(1, -1)} + local bpos = 1 + local num = 0 + for k = 1,#tbyte do -- (k = #t, 1, -1 for BE) + num = num + tbyte[k] * bpos + bpos = bpos * 256 + end + return num +end + +-- uses database files to retrieve database tags +-- adds all unique tags into a lua table +function get_tags(filename, hstr) + + if not filename then return end + + hstr = hstr or filename + + local file = io.open('/' .. filename or "", "r") --read + if not file then rb.splash(100, sERROROPENING .. " " .. filename) return end + + local fsz = file:seek("end") + + local posln = 0 + local tag_len = TCHSIZE + local idx + + local function readchrs(count) + if posln >= fsz then return nil end + file:seek("set", posln) + posln = posln + count + return file:read(count) + end + + local tagcache_header = readchrs(DATASZ) or "" + local tagcache_sz = readchrs(DATASZ) or "" + local tagcache_entries = readchrs(DATASZ) or "" + + if tagcache_header ~= sTCHEADER or + bytesLE_n(tagcache_sz) ~= (fsz - TCHSIZE) then + rb.splash(100, sINVALIDDATABASE .. " " .. filename) + return + end + + -- local tag_entries = bytesLE_n(tagcache_entries) + + local ftable = {} + table.insert(ftable, 1, hstr) + + local tline = #ftable + 1 + ftable[tline] = "" + + local str = "" + + while true do + tag_len = bytesLE_n(readchrs(DATASZ)) + readchrs(DATASZ) -- idx = bytesLE_n(readchrs(DATASZ)) + str = readchrs(tag_len) or "" + str = string.match(str, "(%Z+)%z") + + if str then + if ftable[tline - 1] ~= str then -- Remove dupes + ftable[tline] = str + tline = tline + 1 + end + elseif posln >= fsz then + break + end + + if rb.get_plugin_action(0) == CANCEL_BUTTON then + break + end + end + + file:close() + + return ftable +end -- get_tags |