11local file = require " soluna.file"
2+ local zip = require " soluna.zip"
3+ local lfs = require " soluna.lfs"
24
35local package = package
46local string = string
@@ -9,8 +11,79 @@ global load, print
911local dir_sep , temp_sep , temp_marker = package.config :match " (.)\n (.)\n (.)"
1012local temp_pat = " [^" .. temp_sep .. " ]+"
1113
14+ local zipfile = zip .zipfile
15+ local file_load = file .load
16+ local file_exist = file .exist
17+
18+ if zipfile then
19+ function file_load (fullname )
20+ local name = fullname :match " %./(.*)" or fullname
21+ return zipfile :readfile (name )
22+ end
23+ function file_exist (fullname )
24+ local name = fullname :match " %./(.*)" or fullname
25+ return zipfile :exist (name )
26+ end
27+ file .local_load = file .load
28+ file .local_exist = file .exist
29+ file .load = file_load
30+ file .exist = file_exist
31+ local list
32+ function file .dir (root )
33+ list = list or zipfile :list ()
34+ root = root :gsub (" [^/]$" , " %0/" )
35+ local iter = 1
36+ local n = # list
37+ local root_n = # root
38+ local last
39+ return function ()
40+ while iter <= n do
41+ local t = list [iter ]
42+ iter = iter + 1
43+ if t :sub (1 , root_n ) == root then
44+ local sname = t :sub (root_n + 1 ):match " [^/]+"
45+ if sname ~= last then
46+ last = sname
47+ return sname
48+ end
49+ end
50+ end
51+ end
52+ end
53+ function file .attributes (fullname )
54+ list = list or zipfile :list ()
55+ local pathname = fullname .. " /"
56+ local pathn = # pathname
57+ for i = 1 , # list do
58+ local t = list [i ]
59+ if fullname == t then
60+ return " file"
61+ elseif t :sub (1 , pathn ) == pathname then
62+ return " directory"
63+ end
64+ end
65+ end
66+ function file .searchpath (name , path )
67+ local cname = name :gsub (" %." , " /" )
68+ for temp in path :gmatch (temp_pat ) do
69+ local fullname = temp :gsub (temp_marker , cname )
70+ if dir_sep ~= ' /' then
71+ fullname = fullname :gsub (dir_sep , " /" )
72+ end
73+ if file_exist (fullname ) then
74+ return fullname
75+ end
76+ end
77+ end
78+ else
79+ file .dir = lfs .dir
80+ file .attributes = lfs .attributes
81+ file .local_load = file .load
82+ file .local_exist = file .exist
83+ end
84+
1285local function fileload (name , fullname )
13- local s , err = file . load (fullname )
86+ local s , err = file_load (fullname )
1487 local f = load (s , " @" .. fullname )
1588 return f (name , fullname )
1689end
@@ -22,7 +95,7 @@ local function search_file(name)
2295 if dir_sep ~= ' /' then
2396 fullname = fullname :gsub (dir_sep , " /" )
2497 end
25- if file . exist (fullname ) then
98+ if file_exist (fullname ) then
2699 return fileload , fullname
27100 end
28101 end
0 commit comments