-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmylib_filesystem.lua
503 lines (461 loc) · 12.5 KB
/
mylib_filesystem.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
function os.createDir(path)
if os.isUnix() then
os.execute('mkdir "'..path..'"')
else
os.execute("md "..string.gsub(path, '/', '\\'))
end
end
function os.rename(name1, name2)
if os.isUnix() then
os.execute("mv "..name1.." "..name2)
else
local cmd=string.gsub("move "..name1.." "..name2, '/', '\\')
print(cmd)
os.execute(cmd)
end
end
function os.deleteFiles(mask)
if os.isUnix() then
os.execute("rm "..mask)
else
os.execute("del "..string.gsub(mask, '/', '\\'))
end
end
function os.parentDir(currDir)
return os.rightTokenize(string.gsub(currDir, "\\","/"), "/")
end
function os.relativeToAbsolutePath(folder,currDir)
if string.sub(folder, 1,1)~="/" then
currDir=currDir or os.currentDirectory()
while(string.sub(folder,1,2)=="..") do
currDir=os.parentDir(currDir)
folder=string.sub(folder,4)
end
while(string.sub(folder,1,1)==".") do
folder=string.sub(folder,3)
end
if folder=="" then
folder=currDir
else
folder=currDir.."/"..folder
end
end
return folder
end
function os.absoluteToRelativePath(folder, currDir) -- param1: folder or file name
assert(string.sub(folder,1,1)=="/")
currDir=currDir or os.currentDirectory()
local n_ddot=0
while string.sub(folder,1,#currDir)~=currDir or currDir=="" do
currDir=os.parentDir(currDir)
n_ddot=n_ddot+1
end
local str=""
for i=1,n_ddot do
str=str.."../"
end
print(n_ddot, currDir)
str=str..string.sub(folder,#currDir+2)
return string.trimSpaces(str)
end
function os.currentDirectory()
if os.isUnix() then
return os.capture('pwd')
else
return os.capture('cd')
end
end
function os.copyFile(mask, mask2)
if os.isUnix() or os.isMsysgit() then
if mask2 then
os.execute('cp "'..mask..'" "'..mask2..'"')
else
os.execute("cp "..mask)
end
else
if mask2 then
print('copy "'.. string.gsub(mask, '/', '\\')..'" "'..string.gsub(mask2, '/', '\\')..'"')
os.execute('copy "'.. string.gsub(mask, '/', '\\')..'" "'..string.gsub(mask2, '/', '\\')..'"')
else
print("copy ".. string.gsub(mask, '/', '\\'))
os.execute("copy ".. string.gsub(mask, '/', '\\'))
end
end
end
-- copy folder a to folder b (assuming folder b doesn't exists yet.)
-- otherwise, behaviors are undefined.
-- os.copyResource("../a", "../b", {'%.txt$', '%.lua$'})
function os.copyRecursive(srcFolder, destFolder, acceptedExt)
if string.sub(destFolder, -1,-1)=="/" then
destFolder=string.sub(destFolder, 1, -2)
end
acceptedExt=acceptedExt or os.globParam.acceptedExt
local backup=os.globParam.acceptedExt
os.createDir(destFolder)
-- first copy files directly in the source folder
if true then
os.globParam.acceptedExt=acceptedExt
local files=os.glob(srcFolder.."/*")
os.globParam.acceptedExt=backup
for i,f in ipairs(files) do
os.copyFiles(f, destFolder.."/"..string.sub(f, #srcFolder+2))
end
end
-- recursive copy subfolders
local folders=os.globFolders(srcFolder)
for i, f in ipairs(folders) do
os.copyRecursive(srcFolder.."/"..f, destFolder.."/"..f, acceptedExt)
end
end
function os.copyFiles(src, dest, ext) -- copy source files to destination folder and optinally change file extensions.
if os.isUnix() then
os.execute('cp "'..src..'" "'..dest..'"')
else
local cmd="copy "..string.gsub(src, '/', '\\').." "..string.gsub(dest, '/', '\\')
print(cmd)
os.execute(cmd)
end
if ext then
local files=os.glob(dest.."/*"..ext[1])
for i,file in ipairs(files) do
if string.find(file, ext[1])~=string.len(file) then
os.rename(file, string.gsub(file, ext[1], ext[2]))
end
end
end
end
function os._globWin32(attr, mask, ignorepattern)
local cmd="dir /b/a:"..attr..' "'..string.gsub(mask, "/", "\\")..'" 2>nul'
local cap=os.capture(cmd, true)
local tbl=string.tokenize(cap, "\n")
tbl[table.getn(tbl)]=nil
local files={}
local c=1
local prefix=""
--if string.find(mask, "/") then
--prefix=os.parentDir(mask).."/"
--else
--prefix=""
--end
for i,fn in ipairs(tbl) do
if string.sub(fn, string.len(fn))~="~" then
if not (ignorepattern and string.isMatched(fn, ignorepattern)) then
files[c]=prefix..fn
c=c+1
end
end
end
return files
end
os.globParam={}
os.globParam.ignorePath={'^%.', '^CVS'}
os.globParam.acceptedExt={'%.txt$','%.pdf$', '%.inl$', '%.lua$', '%.cpp$', '%.c$', '%.cxx$', '%.h$', '%.hpp$', '%.py$','%.cc$'}
function os.globFolders(path) -- os.globFolders('..') list all folders in the .. folder
if path==nil or path=="." then
path=""
end
if os.isUnix() then
local path='"'..path..'"'
if path=='""' then path="" end
local tbl=string.tokenize(os.capture('ls -1 -p '..path.." 2> /dev/null",true), "\n")
local tbl2=array:new()
local ignorepath=os.globParam.ignorePath
for i,v in ipairs(tbl) do
if string.sub(v,-1)=="/" then
local fdn=string.sub(v, 1,-2)
if not string.isMatched(fdn, ignorepath) then
tbl2:pushBack(fdn)
end
end
end
return tbl2
else
return os._globWin32('D', path, os.globParam.ignorePath)
end
end
function os._processMask(mask)
mask=string.gsub(mask, "#", "*")
local folder, lmask=os.rightTokenize(mask, '/',true)
local wildcardCheckPass=true
if not string.find(lmask, '[?*]') then -- wild card
folder=folder..lmask
lmask="*"
end
if string.sub(folder, -1)~="/" and folder~="" then
folder=folder.."/"
end
return folder,lmask
end
function os.findgrep(mask, bRecurse, pattern)
local printFunc={
iterate=function (self, v)
util.grepFile(v, pattern)
end
}
os.find(mask, bRecurse, true, printFunc)
end
function os.find(mask, bRecurse, nomessage, printFunc)
local printFunc=printFunc or { iterate=function (self, v) print(v) end}
--mask=string.gsub(mask, "#", "*")
--mask=string.gsub(mask, "%%", "*")
if string.find(mask, "[#%%?%*]")==nil then
-- check if this is a file not a folder
if os.isFileExist(mask) then
printFunc:iterate(mask)
return
else
fn,path=os.processFileName(mask)-- fullpath
mask=path.."/*"..fn -- try full search
end
end
if not nomessage then
io.write('globbing '..string.sub(mask,-30)..' \r')
end
if bRecurse==nil then bRecurse=false end
if os.isUnix() then
local folder, lmask=os.rightTokenize(mask, '/')
if lmask=="*" then
mask=folder
folder, lmask=os.rightTokenize(mask, '/')
end
local containsRelPath=false
if string.find(lmask, '[?*]') then -- wild card
containsRelPath=true
end
local cmd='ls -1 -p '..mask..' 2>/dev/null'
local tbl=string.tokenize(os.capture(cmd,true), "\n")
local lenfolder=string.len(folder)
--print(cmd,mask,#tbl,lenfolder)
if lenfolder==0 then lenfolder=-1 end
local acceptedExt=deepCopyTable(os.globParam.acceptedExt)
if string.find(mask,"%*%.") then
local idx=string.find(mask,"%*%.")+2
acceptedExt[#acceptedExt+1]="%."..string.sub(mask,idx)..'$'
--print(acceptedExt[#acceptedExt])
end
for i=1, table.getn(tbl)-1 do
local v=tbl[i]
if string.sub(v,-1)~="/" and string.isMatched(v, acceptedExt) then
if containsRelPath then
printFunc:iterate(v)
else
if string.sub(mask,-1)=="/" then
printFunc:iterate(mask..v)
else
printFunc:iterate(mask.."/"..v)
end
end
end
end
else
local folder, lmask=os._processMask(mask)
local out=os._globWin32("-d", folder..lmask)
local acceptedExt=deepCopyTable(os.globParam.acceptedExt)
if string.find(mask,"%*%.") then
local idx=string.find(mask,"%*%.")+2
acceptedExt[#acceptedExt+1]="%."..string.sub(mask,idx)..'$'
--print(acceptedExt[#acceptedExt])
end
for i=1, table.getn(out) do
if string.isMatched(out[i], acceptedExt) then
printFunc:iterate(folder..out[i])
end
end
end
local verbose=false
if bRecurse then
local folder, lmask=os._processMask(mask)
if verbose then print(folder, lmask) end
local subfolders=os.globFolders(folder)
if verbose then printTable(subfolders) end
for i=1, table.getn(subfolders) do
local v=subfolders[i]
os.find(folder..v..'/'..lmask, true, nomessage, printFunc)
end
end
if not nomessage then
io.write(' \r')
end
end
function os.glob(mask, bRecurse, nomessage) -- you can use # or % instead of *. e.g. os.glob('#.jpg')
local tbl2=array:new()
function tbl2:iterate(v)
--print(v)
self:pushBack(v)
end
os.find(mask, bRecurse, nomessage, tbl2)
return tbl2
end
function os.home_path()
if os.isUnix() then
return os.capture("echo ~")
else
return os.capture("echo %HOMEDRIVE%")..os.capture("echo %HOMEPATH%")
end
end
function os.processFileName(target)-- fullpath
local target=string.gsub(target, '\\', '/')
local lastSep
local newSep=0
local count=0
repeat lastSep=newSep
newSep=string.find(target, "/", lastSep+1)
count=count+1
until newSep==nil
local path=string.sub(target, 0, lastSep-1)
local filename
if lastSep==0 then filename=string.sub(target,lastSep) else filename=string.sub(target, lastSep+1) end
return filename, path
end
function os.filename(target)
local f=os.processFileName(target)
return f
end
function os.isFileExist(fn)
local f=io.open(fn,'r')
if f==nil then return false end
f:close()
return true
end
function createBatchFile(fn, list, echoOff)
local fout, msg=io.open(fn, "w")
if fout==nil then print(msg) end
if os.isWindows() then
if echoOff then
fout:write("@echo off\necho off\n")
end
fout:write("setlocal\n")
end
for i,c in ipairs(list) do
fout:write(c.."\n")
end
fout:close()
end
function os.execute2(...) -- excute multiple serial operations
execute(...)
end
function os.pexecute(...) -- excute multiple serial operations
if os.isUnix() then
execute(...)
else
local list={...}
createBatchFile("_temp.bat",list)
os.execute("start _temp.bat")
end
end
-- escape so that it can be used in double quotes
function os.shellEscape(str)
if os.isUnix() then
str=string.gsub(str, '\\', '\\\\')
str=string.gsub(str, '"', '\\"')
str=string.gsub(str, '%%', '\\%%')
else
str=string.gsub(str, '\\', '\\\\')
str=string.gsub(str, '"', '\\"')
str=string.gsub(str, '%$', '\$')
end
return str
end
function os.luaExecute(str, printCmd)
local luaExecute
local gotoRoot
local endMark
local packagepath=os.shellEscape('package.path="./OgreFltk/Resource/scripts/ui/?.lua;./OgreFltk/work/?.lua"')
if os.isUnix() then
luaExecute="lua -e \""..packagepath.."dofile('OgreFltk/work/mylib.lua');"
gotoRoot="cd ../.."
endMark="\""
else
luaExecute="OgreFltk\\work\\lua -e dofile('OgreFltk/work/mylib.lua');"
gotoRoot="cd ..\\.."
endMark=""
end
str=os.shellEscape(str)
if printCmd then print(luaExecute..str..endMark) end
execute(gotoRoot, luaExecute..str..endMark)
end
-- deprecated (the same as os.execute2)
function execute(...)
local list={...}
if not math.seeded then
math.randomseed(os.time())
math.seeded=true
end
if os.isUnix() then
if #list<3 then
local cmd=""
for i,c in ipairs(list) do
cmd=cmd..";"..c
end
--print(string.sub(cmd,2))
os.execute(string.sub(cmd, 2))
else
local fn='temp/_temp'..tostring(math.random(1,10000))
createBatchFile(fn, list)
os.execute("sh "..fn)
os.deleteFiles(fn)
end
else
createBatchFile("_temp.bat",list,true)
-- os.execute("cat _temp.bat")
os.execute("_temp.bat")
end
end
function executeUsingNewCMD(...)
local list={...}
createBatchFile("_temp.bat",list,true)
-- os.execute("cat _temp.bat")
os.execute("cmd /c _temp.bat")
end
function util.grepFile(fn, pattern, prefix,raw, printFunc)
printFunc=printFunc or
{
iterate=function(self,fn,ln,idx,line)
print(fn..":"..ln..":"..string.trimLeft(line))
end
}
prefix=prefix or ""
pattern=string.lower(pattern)
local fin, msg=io.open(fn, "r")
if fin==nil then
print(msg)
return
end
local ln=1
--local c=0
--local lastFn, lastLn
for line in fin:lines() do
local lline=string.lower(line)
local res, idx
if raw then
res,idx=pcall(string.find, lline, pattern)
else
res,idx=pcall(string.find, lline, pattern, nil,true)
end
if res and idx then
-- print(prefix..fn..":"..ln..":"..idx..":"..string.trimLeft(line))
printFunc:iterate(prefix..fn,ln,idx,line)
--c=c+1
--lastFn=prefix..fn
--lastLn=ln
end
ln=ln+1
end
fin:close()
--if c==1 then
--os.vi_line(lastFn, lastLn)
--end
end
function util.grep(mask, pattern, prefix, bRecurse,raw) -- deprecated. use util.findgrep
local list=os.glob(mask, bRecurse, true)
for i, fn in ipairs(list) do
util.grepFile(fn, pattern, prefix,raw)
end
end
function os.open(t)
if os.isUnix() then
os.execute('gnome-open '..t)
else
os.execute('start cmd/c '..t)
end
end