diff options
Diffstat (limited to 'apps/plugins/lua_scripts/fileviewers.lua')
-rwxr-xr-x | apps/plugins/lua_scripts/fileviewers.lua | 468 |
1 files changed, 0 insertions, 468 deletions
diff --git a/apps/plugins/lua_scripts/fileviewers.lua b/apps/plugins/lua_scripts/fileviewers.lua deleted file mode 100755 index c686f3eeda..0000000000 --- a/apps/plugins/lua_scripts/fileviewers.lua +++ /dev/null | |||
@@ -1,468 +0,0 @@ | |||
1 | --[[ | ||
2 | /*************************************************************************** | ||
3 | * __________ __ ___. | ||
4 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
5 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
6 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
7 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
8 | * \/ \/ \/ \/ \/ | ||
9 | * $Id$ | ||
10 | * | ||
11 | * Copyright (C) 2017 William Wilgus | ||
12 | * | ||
13 | * This program is free software; you can redistribute it and/or | ||
14 | * modify it under the terms of the GNU General Public License | ||
15 | * as published by the Free Software Foundation; either version 2 | ||
16 | * of the License, or (at your option) any later version. | ||
17 | * | ||
18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
19 | * KIND, either express or implied. | ||
20 | * | ||
21 | ****************************************************************************/ | ||
22 | ]] | ||
23 | if ... == nil then rb.splash(rb.HZ * 3, "use 'require'") end | ||
24 | require("printtable") | ||
25 | local _clr = require("color") | ||
26 | local _lcd = require("lcd") | ||
27 | local _print = require("print") | ||
28 | local _timer = require("timer") | ||
29 | |||
30 | require("actions") | ||
31 | local CANCEL_BUTTON = rb.actions.PLA_CANCEL | ||
32 | -------------------------------------------------------------------------------- | ||
33 | -- builds an index of byte position of every line at each bufsz increment | ||
34 | -- in filename; bufsz == 1 would be every line; saves to filename.ext.idx_ext | ||
35 | -- lnbyte should be nil for text files and number of bytes per line for binary | ||
36 | local function build_file_index(filename, idx_ext, bufsz, lnbyte) | ||
37 | |||
38 | if not filename then return end | ||
39 | local file = io.open('/' .. filename, "r") --read | ||
40 | if not file then _lcd:splashf(100, "Can't open %s", filename) return end | ||
41 | local fsz = file:seek("end") | ||
42 | local fsz_kb = fsz / 1024 | ||
43 | local count | ||
44 | local ltable = {0} --first index is the beginning of the file | ||
45 | local timer = _timer() | ||
46 | local fread | ||
47 | _lcd:splashf(100, "Indexing file %d Kb", (fsz / 1024)) | ||
48 | |||
49 | if lnbyte then | ||
50 | fread = function(f) return f:read(lnbyte) end | ||
51 | else | ||
52 | lnbyte = -1 | ||
53 | fread = function(f) return f:read("*l") end | ||
54 | end | ||
55 | |||
56 | file:seek("set", 0) | ||
57 | for i = 1, fsz do | ||
58 | if i % bufsz == 0 then | ||
59 | local loc = file:seek() | ||
60 | ltable[#ltable + 1] = loc | ||
61 | _lcd:splashf(1, "Parsing %d of %d Kb", loc / 1024, fsz_kb) | ||
62 | end | ||
63 | if rb.get_plugin_action(0) == CANCEL_BUTTON then | ||
64 | return | ||
65 | end | ||
66 | if not fread(file) then | ||
67 | count = i | ||
68 | break | ||
69 | end | ||
70 | end | ||
71 | |||
72 | local fileidx = io.open('/' .. filename .. idx_ext, "w+") -- write/erase | ||
73 | if fileidx then | ||
74 | fileidx:write(fsz .. "\n") | ||
75 | fileidx:write(count .. "\n") | ||
76 | fileidx:write(bufsz .. "\n") | ||
77 | fileidx:write(lnbyte .. "\n") | ||
78 | fileidx:write(table.concat(ltable, "\n")) | ||
79 | fileidx:close() | ||
80 | _lcd:splashf(100, "Finished in %d seconds", timer.stop() / rb.HZ) | ||
81 | collectgarbage("collect") | ||
82 | else | ||
83 | error("unable to save index file") | ||
84 | end | ||
85 | end -- build_file_index | ||
86 | -------------------------------------------------------------------------------- | ||
87 | |||
88 | --- returns size of original file, total lines buffersize, and table filled | ||
89 | -- with line offsets in index file -> filename | ||
90 | local function load_index_file(filename) | ||
91 | local filesz, count, bufsz, lnbyte | ||
92 | local ltable | ||
93 | local fileidx = io.open('/' .. filename, "r") --read | ||
94 | if fileidx then | ||
95 | local idx = -3 | ||
96 | ltable = {} | ||
97 | fileidx:seek("set", 0) | ||
98 | for line in fileidx:lines() do | ||
99 | if idx == -3 then | ||
100 | filesz = tonumber(line) | ||
101 | elseif idx == -2 then | ||
102 | count = tonumber(line) | ||
103 | elseif idx == -1 then | ||
104 | bufsz = tonumber(line) | ||
105 | elseif idx == 0 then | ||
106 | lnbyte = tonumber(line) | ||
107 | else | ||
108 | ltable[idx] = tonumber(line) | ||
109 | end | ||
110 | idx = idx + 1 | ||
111 | end | ||
112 | fileidx:close() | ||
113 | end | ||
114 | return lnbyte, filesz, count, bufsz, ltable | ||
115 | end -- load_index_file | ||
116 | -------------------------------------------------------------------------------- | ||
117 | |||
118 | -- creates a fixed index with fixed line lengths, perfect for viewing hex files | ||
119 | -- not so great for reading text files but works as a fallback | ||
120 | local function load_fixed_index(bytesperline, filesz, bufsz) | ||
121 | local lnbyte = bytesperline | ||
122 | local count = (filesz + lnbyte - 1) / lnbyte + 1 | ||
123 | local idx_t = {} -- build index | ||
124 | for i = 0, filesz, bufsz do | ||
125 | idx_t[#idx_t + 1] = lnbyte * i | ||
126 | end | ||
127 | return lnbyte, filesz, count, bufsz, idx_t | ||
128 | end -- load_fixed_index | ||
129 | -------------------------------------------------------------------------------- | ||
130 | |||
131 | -- uses print_table to display a whole file | ||
132 | function print_file(filename, maxlinelen, settings) | ||
133 | |||
134 | if not filename then return end | ||
135 | local file = io.open('/' .. filename or "", "r") --read | ||
136 | if not file then _lcd:splashf(100, "Can't open %s", filename) return end | ||
137 | maxlinelen = 33 | ||
138 | local hstr = filename | ||
139 | local ftable = {} | ||
140 | table.insert(ftable, 1, hstr) | ||
141 | |||
142 | local tline = #ftable + 1 | ||
143 | local remln = maxlinelen | ||
144 | local posln = 1 | ||
145 | |||
146 | for line in file:lines() do | ||
147 | if line then | ||
148 | if maxlinelen then | ||
149 | if line == "" then | ||
150 | ftable[tline] = ftable[tline] or "" | ||
151 | tline = tline + 1 | ||
152 | remln = maxlinelen | ||
153 | else | ||
154 | line = line:match("%w.+") or "" | ||
155 | end | ||
156 | local linelen = line:len() | ||
157 | while linelen > 0 do | ||
158 | |||
159 | local fsp = line:find("%s", posln + remln - 5) or 0x0 | ||
160 | fsp = fsp - (posln + remln) | ||
161 | if fsp >= 0 then | ||
162 | local fspr = fsp | ||
163 | fsp = line:find("%s", posln + remln) or linelen | ||
164 | fsp = fsp - (posln + remln) | ||
165 | if math.abs(fspr) < fsp then fsp = fspr end | ||
166 | end | ||
167 | if fsp > 5 or fsp < -5 then fsp = 0 end | ||
168 | |||
169 | local str = line:sub(posln, posln + remln + fsp) | ||
170 | local slen = str:len() | ||
171 | ftable[tline] = ftable[tline] or "" | ||
172 | ftable[tline] = ftable[tline] .. str | ||
173 | linelen = linelen - slen | ||
174 | if linelen > 0 then | ||
175 | tline = tline + 1 | ||
176 | posln = posln + slen | ||
177 | remln = maxlinelen | ||
178 | --loop continues | ||
179 | else | ||
180 | ftable[tline] = ftable[tline] .. " " | ||
181 | remln = maxlinelen - slen | ||
182 | posln = 1 | ||
183 | --loop ends | ||
184 | end | ||
185 | |||
186 | end | ||
187 | else | ||
188 | ftable[#ftable + 1] = line | ||
189 | end | ||
190 | |||
191 | |||
192 | end | ||
193 | end | ||
194 | |||
195 | file:close() | ||
196 | |||
197 | _lcd:clear() | ||
198 | _print.clear() | ||
199 | |||
200 | if not settings then | ||
201 | settings = {} | ||
202 | settings.justify = "center" | ||
203 | settings.wrap = true | ||
204 | settings.msel = true | ||
205 | end | ||
206 | settings.hasheader = true | ||
207 | settings.co_routine = nil | ||
208 | settings.ovfl = "manual" | ||
209 | |||
210 | local sel = | ||
211 | print_table(ftable, #ftable, settings) | ||
212 | |||
213 | _lcd:splashf(rb.HZ * 2, "%d items {%s}", #sel, table.concat(sel, ", ")) | ||
214 | ftable = nil | ||
215 | end -- print_file | ||
216 | -------------------------------------------------------------------------------- | ||
217 | |||
218 | -- uses print_table to display a portion of a file | ||
219 | function print_file_increment(filename, settings) | ||
220 | |||
221 | if not filename then return end | ||
222 | local file = io.open('/' .. filename, "r") --read | ||
223 | if not file then _lcd:splashf(100, "Can't open %s", filename) return end | ||
224 | local fsz = file:seek("end") | ||
225 | local bsz = 1023 | ||
226 | --if small file do it the easier way and load whole file to table | ||
227 | if fsz < 60 * 1024 then | ||
228 | file:close() | ||
229 | print_file(filename, settings) | ||
230 | return | ||
231 | end | ||
232 | |||
233 | local ext = ".idx" | ||
234 | local lnbyte, filesz, count, bufsz, idx_t = load_index_file(filename .. ext) | ||
235 | |||
236 | if not idx_t or fsz ~= filesz then -- build file index | ||
237 | build_file_index(filename, ext, bsz) | ||
238 | lnbyte, filesz, count, bufsz, idx_t = load_index_file(filename .. ext) | ||
239 | end | ||
240 | |||
241 | -- if invalid or user canceled creation fallback to a fixed index | ||
242 | if not idx_t or fsz ~= filesz or count <= 0 then | ||
243 | _lcd:splashf(rb.HZ * 5, "Unable to read file index %s", filename .. ext) | ||
244 | lnbyte, filesz, count, bufsz, idx_t = load_fixed_index(32, fsz, bsz) | ||
245 | end | ||
246 | |||
247 | if not idx_t or fsz ~= filesz or count <= 0 then | ||
248 | _lcd:splashf(rb.HZ * 5, "Unable to load file %s", filename) | ||
249 | return | ||
250 | end | ||
251 | |||
252 | local hstr = filename | ||
253 | local file_t = setmetatable({},{__mode = "kv"}) --weak keys and values | ||
254 | -- this allows them to be garbage collected as space is needed | ||
255 | -- rebuilds when needed | ||
256 | local ovf = 0 | ||
257 | local lpos = 1 | ||
258 | local timer = _timer() | ||
259 | file:seek("set", 0) | ||
260 | |||
261 | function print_co() | ||
262 | while true do | ||
263 | collectgarbage("step") | ||
264 | file_t[1] = hstr --position 1 is ALWAYS header/title | ||
265 | |||
266 | for i = 1, bufsz + ovf do | ||
267 | file_t[lpos + i] = file:read ("*l") | ||
268 | end | ||
269 | ovf = 0 | ||
270 | lpos = lpos + bufsz | ||
271 | |||
272 | local bpos = coroutine.yield() | ||
273 | |||
274 | if bpos <= lpos then -- roll over or scroll up | ||
275 | bpos = (bpos - bufsz) + bpos % bufsz | ||
276 | timer:check(true) | ||
277 | end | ||
278 | |||
279 | lpos = bpos - bpos % bufsz | ||
280 | |||
281 | if lpos < 1 then | ||
282 | lpos = 1 | ||
283 | elseif lpos > count - bufsz then -- partial fill | ||
284 | ovf = count - bufsz - lpos | ||
285 | end | ||
286 | --get position in file of the nearest indexed line | ||
287 | file:seek("set", idx_t[bpos / bufsz + 1]) | ||
288 | |||
289 | -- on really large files if it has been more than 10 minutes | ||
290 | -- since the user scrolled up the screen wipe out the prior | ||
291 | -- items to free memory | ||
292 | if lpos % 5000 == 0 and timer:check() > rb.HZ * 600 then | ||
293 | for i = 1, lpos - 100 do | ||
294 | file_t[i] = nil | ||
295 | end | ||
296 | end | ||
297 | |||
298 | end | ||
299 | end | ||
300 | |||
301 | co = coroutine.create(print_co) | ||
302 | _lcd:clear() | ||
303 | _print.clear() | ||
304 | |||
305 | if not settings then | ||
306 | settings = {} | ||
307 | settings.justify = "center" | ||
308 | settings.wrap = true | ||
309 | end | ||
310 | settings.hasheader = true | ||
311 | settings.co_routine = co | ||
312 | settings.msel = false | ||
313 | settings.ovfl = "manual" | ||
314 | |||
315 | table.insert(file_t, 1, hstr) --position 1 is header/title | ||
316 | local sel = | ||
317 | print_table(file_t, count, settings) | ||
318 | file:close() | ||
319 | idx_t = nil | ||
320 | file_t = nil | ||
321 | return sel | ||
322 | end --print_file_increment | ||
323 | -------------------------------------------------------------------------------- | ||
324 | function print_file_hex(filename, bytesperline, settings) | ||
325 | |||
326 | if not filename then return end | ||
327 | local file = io.open('/' .. filename, "r") --read | ||
328 | if not file then _lcd:splashf(100, "Can't open %s", filename) return end | ||
329 | local hstr = filename | ||
330 | local bpl = bytesperline | ||
331 | local fsz = file:seek("end") | ||
332 | --[[ | ||
333 | local filesz = file:seek("end") | ||
334 | local bufsz = 1023 | ||
335 | local lnbyte = bytesperline | ||
336 | local count = (filesz + lnbyte - 1) / lnbyte + 1 | ||
337 | |||
338 | local idx_t = {} -- build index | ||
339 | for i = 0, filesz, bufsz do | ||
340 | idx_t[#idx_t + 1] = lnbyte * i | ||
341 | end]] | ||
342 | |||
343 | local lnbyte, filesz, count, bufsz, idx_t = load_fixed_index(bpl, fsz, 1023) | ||
344 | |||
345 | local file_t = setmetatable({},{__mode = "kv"}) --weak keys and values | ||
346 | -- this allows them to be garbage collected as space is needed | ||
347 | -- rebuilds when needed | ||
348 | local ovf = 0 | ||
349 | local lpos = 1 | ||
350 | local timer = _timer() | ||
351 | file:seek("set", 0) | ||
352 | |||
353 | function hex_co() | ||
354 | while true do | ||
355 | collectgarbage("step") | ||
356 | file_t[1] = hstr --position 1 is ALWAYS header/title | ||
357 | |||
358 | for i = 1, bufsz + ovf do | ||
359 | local pos = file:seek() | ||
360 | local s = file:read (lnbyte) | ||
361 | if not s then -- EOF | ||
362 | file_t[lpos + i] = "" | ||
363 | break; | ||
364 | end | ||
365 | local s_len = s:len() | ||
366 | |||
367 | if s_len > 0 then | ||
368 | local fmt = "0x%04X: " .. string.rep("%02X ", s_len) | ||
369 | local schrs = " " .. s:gsub("(%c)", " . ") | ||
370 | file_t[lpos + i] = string.format(fmt, pos, s:byte(1, s_len)) .. | ||
371 | schrs | ||
372 | else | ||
373 | file_t[lpos + i] = string.format("0x%04X: ", pos) | ||
374 | end | ||
375 | end | ||
376 | ovf = 0 | ||
377 | lpos = lpos + bufsz | ||
378 | |||
379 | local bpos = coroutine.yield() | ||
380 | |||
381 | if bpos < lpos then -- roll over or scroll up | ||
382 | bpos = (bpos - bufsz) + bpos % bufsz | ||
383 | timer:check(true) | ||
384 | end | ||
385 | |||
386 | lpos = bpos - bpos % bufsz | ||
387 | |||
388 | if lpos < 1 then | ||
389 | lpos = 1 | ||
390 | elseif lpos > count - bufsz then -- partial fill | ||
391 | ovf = count - bufsz - lpos | ||
392 | end | ||
393 | --get position in file of the nearest indexed line | ||
394 | file:seek("set", idx_t[bpos / bufsz + 1]) | ||
395 | |||
396 | -- on really large files if it has been more than 10 minutes | ||
397 | -- since the user scrolled up the screen wipe out the prior | ||
398 | -- items to free memory | ||
399 | if lpos % 10000 == 0 and timer:check() > rb.HZ * 600 then | ||
400 | for i = 1, lpos - 100 do | ||
401 | file_t[i] = nil | ||
402 | end | ||
403 | end | ||
404 | |||
405 | end | ||
406 | end | ||
407 | |||
408 | co = coroutine.create(hex_co) | ||
409 | |||
410 | local function repl(char) | ||
411 | local ret = "" | ||
412 | if char:sub(1,2) == "0x" then | ||
413 | return string.format("%dd:", tonumber(char:sub(3, -2), 16)) | ||
414 | else | ||
415 | return string.format("%03d ", tonumber(char, 16)) | ||
416 | end | ||
417 | end | ||
418 | |||
419 | |||
420 | _lcd:clear() | ||
421 | _print.clear() | ||
422 | |||
423 | local sel, start, vcur = 1 | ||
424 | table.insert(file_t, 1, hstr) --position 1 is header/title | ||
425 | |||
426 | if not settings then | ||
427 | settings = {} | ||
428 | settings.justify = "left" | ||
429 | settings.wrap = true | ||
430 | settings.msel = false | ||
431 | settings.hfgc = _clr.set( 0, 000, 000, 000) | ||
432 | settings.hbgc = _clr.set(-1, 255, 255, 255) | ||
433 | settings.ifgc = _clr.set(-1, 255, 255, 255) | ||
434 | settings.ibgc = _clr.set( 0, 000, 000, 000) | ||
435 | settings.iselc = _clr.set( 1, 000, 200, 100) | ||
436 | end | ||
437 | |||
438 | settings.hasheader = true | ||
439 | settings.co_routine = co | ||
440 | settings.start = start | ||
441 | settings.curpos = vcur | ||
442 | settings.ovfl = "manual" | ||
443 | |||
444 | while sel > 0 do | ||
445 | settings.start = start | ||
446 | settings.curpos = vcur | ||
447 | |||
448 | sel, start, vcur = print_table(file_t, count, settings) | ||
449 | |||
450 | if sel > 1 and file_t[sel] then -- flips between hex and decimal | ||
451 | local s = file_t[sel] | ||
452 | if s:sub(-1) == "\b" then | ||
453 | file_t[sel] = nil | ||
454 | ovf = -(bufsz - 1) | ||
455 | coroutine.resume(co, sel) --rebuild this item | ||
456 | else | ||
457 | s = s:gsub("(0x%x+:)", repl) .. "\b" | ||
458 | file_t[sel] = s:gsub("(%x%x%s)", repl) .. "\b" | ||
459 | end | ||
460 | end | ||
461 | end | ||
462 | |||
463 | file:close() | ||
464 | idx_t = nil | ||
465 | file_t = nil | ||
466 | return sel | ||
467 | end -- print_file_hex | ||
468 | -------------------------------------------------------------------------------- | ||