-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalio.lua
795 lines (524 loc) · 13.9 KB
/
localio.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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
require 'lfs'
require 'stringLib'
local t = {}
local function getFileName(path, noExtension)
assert(path, 'no path')
path = path:gsub('/', '\\')
path = path:match('\\([^\\]*)$') or path
if noExtension then
path = path:match('([^%..]*)')
end
return path
end
io.getFileName = getFileName
local function getFolder(path)
assert(path, 'no path')
path = path:gsub('/', '\\')
path = path:match('(.*\\)')
return path
end
t.getFolder = getFolder
local function getFileExtension(path)
assert(path, 'no path')
local ext = getFileName(path):sub(getFileName(path, true):len() + 2, path:len())
if (ext == '') then
return nil
end
return ext
end
t.getFileExtension = getFileExtension
--[[function getWorkingDir()
return io.popen('cd'):read('*l')
end
function getScriptDir()
return getFolder(debug.getinfo(1).source:sub(1))
end]]
local function isAbsPath(path)
assert(path, 'no path')
if path:find(':') then
return true
end
return false
end
t.isAbsPath = isAbsPath
local function getCallStack()
local t = {}
local c = 2
while debug.getinfo(c, 'S') do
local what = debug.getinfo(c, 'S').what
if ((what == 'Lua') or (what == 'main')) then
t[#t + 1] = debug.getinfo(c, 'S')
end
c = c + 1
end
return t
end
local function toFolderPath(path, shortened)
assert(path, 'no path')
path = path:gsub('/', '\\')
if shortened then
path = path:match('(.*)\\$') or path
else
if not path:match('\\$') then
path = path..'\\'
end
end
return path
end
t.toFolderPath = toFolderPath
local function toAbsPath(path, basePath)
assert(path, 'no path')
path = path:gsub('/', '\\')
if isAbsPath(path) then
return path
end
--local scriptDir = getFolder(scriptPath:gsub('/[^/]+$', ''))
if (basePath == nil) then
basePath = io.curDir()
end
local result = toFolderPath(basePath)
while (path:find('..\\') == 1) do
result = result:reduceFolder()
path = path:sub(4)
end
result = result..path
return result
end
t.toAbsPath = toAbsPath
local function curDir()
return toFolderPath(lfs.currentdir())
end
t.curDir = curDir
local function local_path(level)
if (level == nil) then
level = 0
end
local path = getCallStack()[2 + level].source
path = path:match('^@(.*)$')
while ((path:find('.', 1, true) == 1) or (path:find('\\', 1, true) == 1)) do
path = path:sub(2)
end
path = path:gsub('/', '\\')
--path = path:match('(.*\\)') or ''
if not io.isAbsPath(path) then
path = io.curDir()..path
end
return path
end
t.local_path = local_path
local function local_dir(level)
if (level == nil) then
level = 0
end
local path = getCallStack()[2 + level].source
path = path:match('^@(.*)$')
while ((path:find('.', 1, true) == 1) or (path:find('\\', 1, true) == 1)) do
path = path:sub(2)
end
path = path:gsub('/', '\\')
path = path:match('(.*\\)') or ''
if not io.isAbsPath(path) then
path = io.curDir()..path
end
return path
end
t.local_dir = local_dir
local function pathExists(path)
assert(path, 'no path')
path = path:match('(.*[^\\])')
return (lfs.attributes(toAbsPath(path, io.local_dir(1))) ~= nil)
end
t.pathExists = pathExists
local function pathIsFile(path)
return (lfs.attributes(toAbsPath(path, io.local_dir(1)), 'mode') == 'file')
end
t.pathIsFile = pathIsFile
local function pathIsOpenable(path)
assert(path, 'no path')
local f = io.open(path, 'r')
local result = (f ~= nil)
if result then
f:close()
end
return result
end
t.pathIsOpenable = pathIsOpenable
local function pathIsWritable(path)
assert(path, 'no path')
local f = io.open(path, 'w+')
local result = (f ~= nil)
if result then
f:close()
end
return result
end
t.pathIsWritable = pathIsWritable
local function pathIsLocked(path)
if not io.pathExists(path) then
return true
end
local f = io.open(path, 'a')
if (f ~= nil) then
f:close()
return false
end
return true
end
t.pathIsLocked = pathIsLocked
local function local_require(path1)
local path = toAbsPath(path1, io.local_dir(1))
if package.loaded[path] then
return
end
local packagePath = package.path
package.path = getFolder(path)..'?.lua'
--print('require', path1, path, getFolder(path))
local result = require(getFileName(path, true))
package.path = packagePath
return result
end
t.local_require = local_require
local function local_open(path, options)
--print('local_open', path, toAbsPath(path, io.local_dir(1)))
return io.open(toAbsPath(path, io.local_dir(1)), options)
end
t.local_open = local_open
local function local_loadfile(path)
return loadfile(toAbsPath(path, io.local_dir(1)))
end
t.local_loadfile = local_loadfile
local function isFolderPath(path)
if (path == '') then
return false
end
if (getFolder(path) == '') then
return false
end
if (getFolder(path) ~= path) then
return false
end
return true
end
t.isFolderPath = isFolderPath
string.reduceFolder = function(s, amount)
assert(s, 'no path')
if (amount == nil) then
amount = 1
end
if (amount == 0) then
return s
end
local dir = getFolder(s:sub(1, getFolder(s):len() - 1))
local fileName = getFileName(s)
if (dir == nil) then
return fileName
end
return string.reduceFolder(dir..fileName, amount - 1)
end
local function getFiles(dir, filePath)
assert(dir, 'no dir')
dir = toFolderPath(dir)
filePath = filePath or '*'
local c = 0
local t = {}
local cmd = [[dir 2>NUL ]]..(dir..filePath):quote()..[[ /b /s]]
for line in io.popen(cmd):lines() do
if io.pathIsFile(line) then
c = c + 1
t[c] = line
end
end
return t
end
t.getFiles = getFiles
local function getScriptDir(level)
if (level == nil) then
level = 1
end
return getFolder(debug.getinfo(level).source:sub(1))
end
t.getScriptDir = getScriptDir
local function changeWorkingDir(path)
assert(path, 'no path specified')
os.execute([[cd /d ]]..path:quote())
end
t.changeWorkingDir = changeWorkingDir
local function moveFile(sourcePath, targetPath)
os.execute(string.format([[move 1>NUL 2>NUL %q %q]], sourcePath, targetPath))
end
t.moveFile = moveFile
local function renameFile(sourcePath, targetPath)
os.execute(string.format([[rename 1>NUL 2>NUL %q %q]], sourcePath, targetPath))
end
t.renameFile = renameFile
local function createDir(path)
assert(path, 'no path')
path = toAbsPath(path, io.local_dir(1))
assert(isFolderPath(path), 'createDir: path is no directory '..tostring(path))
local function nest(path)
local p = toFolderPath(path, true)
if ((path == nil) or (path == '') or lfs.mkdir(p)) then
return
end
nest(path:reduceFolder())
local result, errorMsg = lfs.mkdir(p)
--print('result', result, errorMsg)
end
nest(path)
end
t.createDir = createDir
local function createFile(path, overwrite)
path = toAbsPath(path, io.local_dir(1))
if not overwrite and io.pathExists(path) then
print('createFile: path exists', path)
return false
end
createDir(getFolder(path))
local f = io.open(path, 'w+b')
assert(f, 'cannot open '..tostring(path))
f:close()
return true
end
t.createFile = createFile
local function copyFile3(source, target)
local sourceFile = io.open(source, 'rb')
local targetFile = io.open(target, 'w+b')
if (targetFile == nil) then
os.execute([[mkdir ]]..getFolder(target):quote())
targetFile = io.open(target, 'w+b')
end
targetFile:write(sourceFile:read('*a'))
sourceFile:close()
targetFile:close()
end
t.copyFile3 = copyFile3
local function copyDir3(source, target)
os.execute([[xcopy ]]..source:quote()..[[ ]]..target:quote()..[[ /e /i /y /q]])
end
t.copyDir3 = copyDir3
local function copyFile(source, target, overwrite)
assert(source, 'no source path')
assert(target, 'no target path')
source = toAbsPath(source, io.local_dir(1))
target = toAbsPath(target, io.local_dir(1))
createDir(getFolder(target))
if isFolderPath(target) then
target = target..getFileName(source)
end
if (not overwrite and io.pathExists(target)) then
print('copyFile: target path exists', target)
return
end
local sourceFile = io.open(source, 'rb')
local targetFile = io.open(target, 'w+b')
assert(sourceFile, 'copyFile: cannot open source '..tostring(source))
assert(targetFile, 'copyFile: cannot open target '..tostring(target))
local bufSize = 10000
local fileSize = io.getFileSize(source)
while (fileSize > 0) do
targetFile:write(sourceFile:read(math.min(bufSize, fileSize)))
fileSize = fileSize - bufSize
end
sourceFile:close()
targetFile:close()
return true
end
t.copyFile = copyFile
local function copyFileIfNewer(source, target)
assert(source, 'no source path')
assert(target, 'no target path')
source = toAbsPath(source, io.local_dir(1))
target = toAbsPath(target, io.local_dir(1))
local targetMod = lfs.attributes(target, 'modification')
if (targetMod ~= nil) then
local sourceMod = lfs.attributes(source, 'modification')
if ((sourceMod ~= nil) and (targetMod >= sourceMod)) then
return
end
end
copyFile(source, target, true)
end
t.copyFileIfNewer = copyFileIfNewer
local function copyFile2(source, target)
createDir(getFolder(target))
local sourceFile = io.open(source, 'rb')
local targetFile = io.open(target, 'w+b')
assert(sourceFile, 'copyFile: cannot open sourcePath', sourcePath)
assert(targetFile, 'copyFile: cannot open targetPath', targetPath)
targetFile:write(sourceFile:read('*a'))
sourceFile:close()
targetFile:close()
end
t.copyFile2 = copyFile2
local function copyDir2(source, target)
--os.execute([[xcopy ]]..source:quote()..[[ ]]..target:quote()..[[ /e /i /y /q]])
source = io.toAbsPath(source, io.local_dir(1))
target = io.toAbsPath(target, io.local_dir(1))
assert(source, 'copyDir: no source', source)
assert(target, 'copyDir: no target', target)
local sourceFolder = getFolder(source)
local targetFolder = getFolder(target)
createDir(targetFolder)
local iter = lfs.dir(source)
assert(iter, 'copyDir: source is no directory', source)
for path in iter do
if ((path ~= '.') and (path ~= '..')) then
copyFile(sourceFolder..path, targetFolder..path)
end
end
end
t.copyDir2 = copyDir2
local function copyDir(source, target, overwrite)
--os.execute([[xcopy ]]..source:quote()..[[ ]]..target:quote()..[[ /e /i /y /q]])
source = toFolderPath(source)
target = toFolderPath(target)
source = io.toAbsPath(source, io.local_dir(1))
target = io.toAbsPath(target, io.local_dir(1))
assert(source, 'copyDir: no source', source)
assert(target, 'copyDir: no target', target)
local sourceFolder = getFolder(source)
local targetFolder = getFolder(target)
local iter = lfs.dir(source)
local t = {}
for path in iter do
t[#t + 1] = path
end
createDir(targetFolder)
assert(iter, 'copyDir: source is no directory', source)
for _, path in pairs(t) do
if ((path ~= '.') and (path ~= '..')) then
local sourcePath = sourceFolder..path
local targetPath = targetFolder..path
if io.pathIsFile(sourcePath) then
copyFile(sourcePath, targetPath, overwrite)
else
sourcePath = toFolderPath(sourcePath)
targetPath = toFolderPath(targetPath)
copyDir(sourcePath, targetPath, overwrite)
end
end
end
end
t.copyDir = copyDir
local function getFilesEx(path)
path = toAbsPath(path, io.local_dir(1))
local iter = lfs.dir(path)
local t = {}
for targetPath in iter do
if ((targetPath ~= '.') and (targetPath ~= '..')) then
targetPath = path..targetPath
if (lfs.attributes(targetPath, 'mode') == 'directory') then
targetPath = targetPath..'\\'
end
t[#t + 1] = targetPath
end
end
return t
end
t.getFilesEx = getFilesEx
local function removeFile(path)
path = toAbsPath(path, io.local_dir(1))
os.remove(path)
end
t.removeFile = removeFile
local function removeDir(path)
path = toAbsPath(path, io.local_dir(1))
assert(isFolderPath(path), 'removeDir: path is no directory '..tostring(path))
local function nest(path)
if ((path == nil) or (path == '')) then
return
end
for _, targetPath in pairs(getFilesEx(path)) do
nest(targetPath)
end
removeFile(path)
lfs.rmdir(path)
end
nest(path)
end
t.removeDir = removeDir
local function flushDir(path)
assert(path, 'no path')
path = toAbsPath(path, io.local_dir(1))
assert(isFolderPath(path), 'recreateDir: path is no directory '..tostring(path))
removeDir(path)
createDir(path)
end
t.flushDir = flushDir
local function chdir(path)
assert(path, 'no path')
lfs.chdir(path)
end
t.chdir = chdir
local function getGlobal(name)
local f = io.local_open(name, 'r')
if not f then
return nil
end
local valType = f:read()
local val = f:read()
f:close()
if (val == nil) then
return valType
end
val = stringToType(val, valType)
return val
end
t.getGlobal = getGlobal
local function setGlobal(name, val)
if (val == nil) then
removeFile(name)
return
end
local valType = type(val)
assert((valType ~= 'table'), 'cannot save table')
local f = io.local_open(name, 'w+')
f:write(valType, '\n', tostring(val))
f:close()
end
t.setGlobal = setGlobal
local function getFileSize(path)
assert(path, 'no path')
assert(io.pathExists(path), 'path '..tostring(path)..' does not exist')
assert(io.pathIsOpenable(path), 'cannot open '..tostring(path))
return lfs.attributes(path, 'size')
end
t.getFileSize = getFileSize
local function syntaxCheck(path)
assert(path, 'no path')
local f, errorMsg = loadfile(path)
return (errorMsg == nil), errorMsg
--[[local ring = rings.new()
ring:dostring('package.path = '..valToLua(getFolder(path)..'?.lua'))
local res, msg, trace = ring:dostring('require '..valToLua(getFileName(path, true)))
return res, msg, trace]]
end
t.syntaxCheck = syntaxCheck
local function loadfileSyntaxCheck(path, throwError)
assert(path, 'no path')
local res, errorMsg = syntaxCheck(path)
if not res then
if throwError then
error(errorMsg)
end
return nil, errorMsg
end
local f = loadfile(path)
if (f == nil) then
local errorMsg = 'could not open '..tostring(path)
if throwError then
error(errorMsg)
end
return nil, errorMsg
end
return f
end
t.loadfileSyntaxCheck = loadfileSyntaxCheck
local function printTrace(s)
print(debug.traceback(tostring(s), 2))
io.flush(io.stdout)
end
t.printTrace = printTrace
for k, v in pairs(t) do
io[k] = v
end