@@ -48,7 +48,7 @@ dtutils_file.libdoc.functions["check_if_bin_exists"] = {
48
48
quoted and returned. If no preference is specified and the operating system is
49
49
linux then the which command is used to check for a binary in the path. If found
50
50
that path is returned. If no binary is found, false is returned.]] ,
51
- Return_Value = [[ result - string - the path of the binary, false if not found]] ,
51
+ Return_Value = [[ result - string - the sanitized path of the binary, false if not found]] ,
52
52
Limitations = [[ ]] ,
53
53
Example = [[ ]] ,
54
54
See_Also = [[ ]] ,
@@ -70,17 +70,17 @@ function dtutils_file.check_if_bin_exists(bin)
70
70
if string.len (path ) > 0 then
71
71
if dtutils_file .check_if_file_exists (path ) then
72
72
if (string.match (path , " .exe$" ) or string.match (path , " .EXE$" )) and dt .configuration .running_os ~= " windows" then
73
- result = " wine " .. " \" " .. path .. " \" "
73
+ result = dtutils_file . sanitize_filename ( " wine " .. path )
74
74
else
75
- result = " \" " .. path .. " \" "
75
+ result = dtutils_file . sanitize_filename ( path )
76
76
end
77
77
end
78
78
elseif dt .configuration .running_os == " linux" then
79
79
local p = io.popen (" which " .. bin )
80
80
local output = p :read (" *a" )
81
81
p :close ()
82
82
if string.len (output ) > 0 then
83
- result = output :sub (1 ,- 2 )
83
+ result = dtutils_file . sanitize_filename ( output :sub (1 ,- 2 ) )
84
84
end
85
85
end
86
86
return result
@@ -107,9 +107,14 @@ dtutils_file.libdoc.functions["split_filepath"] = {
107
107
function dtutils_file .split_filepath (str )
108
108
-- strip out single quotes from quoted pathnames
109
109
str = string.gsub (str , " '" , " " )
110
+ str = string.gsub (str , ' "' , ' ' )
110
111
local result = {}
111
112
-- Thank you Tobias Jakobs for the awesome regular expression, which I tweaked a little
112
113
result [" path" ], result [" filename" ], result [" basename" ], result [" filetype" ] = string.match (str , " (.-)(([^\\ /]-)%.?([^%.\\ /]*))$" )
114
+ if result [" basename" ] == " " and result [" filetype" ]:len () > 1 then
115
+ result [" basename" ] = result [" filetype" ]
116
+ result [" filetype" ] = " "
117
+ end
113
118
return result
114
119
end
115
120
@@ -222,15 +227,21 @@ dtutils_file.libdoc.functions["check_if_file_exists"] = {
222
227
}
223
228
224
229
function dtutils_file .check_if_file_exists (filepath )
225
- local result
230
+ local result = false
226
231
if (dt .configuration .running_os == ' windows' ) then
227
232
filepath = string.gsub (filepath , ' [\\ /]+' , ' \\ ' )
228
- result = os.execute (' if exist "' .. filepath .. ' " (cmd /c exit 0) else (cmd /c exit 1)' )
229
- if not result then
230
- result = false
233
+ local p = io.popen (" if exist " .. filepath .. " (echo 'yes') else (echo 'no')" )
234
+ local ans = p :read (" *all" )
235
+ p :close ()
236
+ if string.match (ans , " yes" ) then
237
+ result = true
231
238
end
239
+ -- result = os.execute('if exist "'..filepath..'" (cmd /c exit 0) else (cmd /c exit 1)')
240
+ -- if not result then
241
+ -- result = false
242
+ -- end
232
243
elseif (dt .configuration .running_os == " linux" ) then
233
- result = os.execute (' test -e ' .. " \" " .. filepath .. " \" " )
244
+ result = os.execute (' test -e ' .. dtutils_file . sanitize_filename ( filepath ) )
234
245
if not result then
235
246
result = false
236
247
end
@@ -266,7 +277,11 @@ dtutils_file.libdoc.functions["chop_filetype"] = {
266
277
267
278
function dtutils_file .chop_filetype (path )
268
279
local length = dtutils_file .get_filetype (path ):len () + 2
269
- return string.sub (path , 1 , - length )
280
+ if length > 2 then
281
+ return string.sub (path , 1 , - length )
282
+ else
283
+ return path
284
+ end
270
285
end
271
286
272
287
dtutils_file .libdoc .functions [" file_copy" ] = {
@@ -373,7 +388,7 @@ dtutils_file.libdoc.functions["filename_increment"] = {
373
388
"01" is added to the basename. If the filename already has an increment, then
374
389
1 is added to it and the filename returned.]] ,
375
390
Return_Value = [[ result - string - the incremented filename]] ,
376
- Limitations = [[ ]] ,
391
+ Limitations = [[ The filename will be incremented to 99 ]] ,
377
392
Example = [[ ]] ,
378
393
See_Also = [[ ]] ,
379
394
Reference = [[ ]] ,
@@ -396,6 +411,9 @@ function dtutils_file.filename_increment(filepath)
396
411
if string.len (increment ) > 2 then
397
412
-- we got the filename so set the increment to 01
398
413
increment = " 01"
414
+ elseif increment == " 99" then
415
+ dt .print_error (" not incrementing, filename has already been incremented 99 times." )
416
+ return filepath
399
417
else
400
418
increment = string.format (" %02d" , tonumber (increment ) + 1 )
401
419
basename = string.gsub (basename , " _(%d-)$" , " " )
@@ -433,8 +451,9 @@ function dtutils_file.create_unique_filename(filepath)
433
451
while dtutils_file .check_if_file_exists (filepath ) do
434
452
filepath = dtutils_file .filename_increment (filepath )
435
453
-- limit to 99 more exports of the original export
436
- if string.match (dtutils_file .get_basename (filepath ), " _(d-)$" ) == " 99" then
437
- break
454
+ local increment = string.match (dtutils_file .get_basename (filepath ), " _(%d-)$" )
455
+ if increment == " 99" then
456
+ break
438
457
end
439
458
end
440
459
return filepath
@@ -576,6 +595,7 @@ dtutils_file.libdoc.functions["mkdir"] = {
576
595
License = [[ ]] ,
577
596
Copyright = [[ ]] ,
578
597
}
598
+
579
599
function dtutils_file .mkdir (path )
580
600
if not dtutils_file .check_if_file_exists (path ) then
581
601
local mkdir_cmd = dt .configuration .running_os == " windows" and " mkdir" or " mkdir -p"
0 commit comments