Skip to content

Commit 80147a9

Browse files
committed
lib/dtugiles.file - Moved filename/path/extension string functions to string library. Left
the existing function calls in place for backward compatibility. They now call the new functions in the string library lib/dtutils.string - Moved the filename/path/extension string functions to the string library to prevent a circular library depnedency. The file library includes the string library. If the string library needed to manipulate filename related strings then the string library would need to include the file library which would lead to a stack overflow.
1 parent 800cbee commit 80147a9

File tree

2 files changed

+132
-104
lines changed

2 files changed

+132
-104
lines changed

lib/dtutils/file.lua

Lines changed: 11 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -394,125 +394,32 @@ function dtutils_file.check_if_bin_exists(bin)
394394
return result
395395
end
396396

397-
dtutils_file.libdoc.functions["split_filepath"] = {
398-
Name = [[split_filepath]],
399-
Synopsis = [[split a filepath into parts]],
400-
Usage = [[local df = require "lib/dtutils.file"
397+
-- the following path, filename, etc functions have
398+
-- moved to the string library since they are string
399+
-- manipulation functions, and to prevent circular
400+
-- library inclusiion.
401401

402-
local result = df.split_filepath(filepath)
403-
filepath - string - path and filename]],
404-
Description = [[split_filepath splits a filepath into the path, filename, basename and filetype and puts
405-
that in a table]],
406-
Return_Value = [[result - table - a table containing the path, filename, basename, and filetype]],
407-
Limitations = [[]],
408-
Example = [[]],
409-
See_Also = [[]],
410-
Reference = [[]],
411-
License = [[]],
412-
Copyright = [[]],
413-
}
402+
-- these functions are left here for compatibility
403+
-- with older scripts
414404

415405
function dtutils_file.split_filepath(str)
416-
-- strip out single quotes from quoted pathnames
417-
str = string.gsub(str, "'", "")
418-
str = string.gsub(str, '"', '')
419-
local result = {}
420-
-- Thank you Tobias Jakobs for the awesome regular expression, which I tweaked a little
421-
result["path"], result["filename"], result["basename"], result["filetype"] = string.match(str, "(.-)(([^\\/]-)%.?([^%.\\/]*))$")
422-
if result["basename"] == "" and result["filetype"]:len() > 1 then
423-
result["basename"] = result["filetype"]
424-
result["filetype"] = ""
425-
end
426-
return result
406+
return ds.split_filepath(str)
427407
end
428408

429-
dtutils_file.libdoc.functions["get_path"] = {
430-
Name = [[get_path]],
431-
Synopsis = [[get the path from a file path]],
432-
Usage = [[local df = require "lib/dtutils.file"
433-
434-
local result = df.get_path(filepath)
435-
filepath - string - path and filename]],
436-
Description = [[get_path strips the filename and filetype from a path and returns the path]],
437-
Return_Value = [[result - string - the path]],
438-
Limitations = [[]],
439-
Example = [[]],
440-
See_Also = [[]],
441-
Reference = [[]],
442-
License = [[]],
443-
Copyright = [[]],
444-
}
445-
446409
function dtutils_file.get_path(str)
447-
local parts = dtutils_file.split_filepath(str)
448-
return parts["path"]
410+
return ds.get_path(str)
449411
end
450412

451-
dtutils_file.libdoc.functions["get_filename"] = {
452-
Name = [[get_filename]],
453-
Synopsis = [[get the filename and extension from a file path]],
454-
Usage = [[local df = require "lib/dtutils.file"
455-
456-
local result = df.get_filename(filepath)
457-
filepath - string - path and filename]],
458-
Description = [[get_filename strips the path from a filepath and returns the filename]],
459-
Return_Value = [[result - string - the file name and type]],
460-
Limitations = [[]],
461-
Example = [[]],
462-
See_Also = [[]],
463-
Reference = [[]],
464-
License = [[]],
465-
Copyright = [[]],
466-
}
467-
468413
function dtutils_file.get_filename(str)
469-
local parts = dtutils_file.split_filepath(str)
470-
return parts["filename"]
414+
return ds.get_filename(str)
471415
end
472416

473-
dtutils_file.libdoc.functions["get_basename"] = {
474-
Name = [[get_basename]],
475-
Synopsis = [[get the filename without the path or extension]],
476-
Usage = [[local df = require "lib/dtutils.file"
477-
478-
local result = df.get_basename(filepath)
479-
filepath - string - path and filename]],
480-
Description = [[get_basename returns the name of the file without the path or filetype
481-
]],
482-
Return_Value = [[result - string - the basename of the file]],
483-
Limitations = [[]],
484-
Example = [[]],
485-
See_Also = [[]],
486-
Reference = [[]],
487-
License = [[]],
488-
Copyright = [[]],
489-
}
490-
491417
function dtutils_file.get_basename(str)
492-
local parts = dtutils_file.split_filepath(str)
493-
return parts["basename"]
418+
return ds.get_basename(str)
494419
end
495420

496-
dtutils_file.libdoc.functions["get_filetype"] = {
497-
Name = [[get_filetype]],
498-
Synopsis = [[get the filetype from a filename]],
499-
Usage = [[local df = require "lib/dtutils.file"
500-
501-
local result = df.get_filetype(filepath)
502-
filepath - string - path and filename]],
503-
Description = [[get_filetype returns the filetype from the supplied filepath]],
504-
Return_Value = [[result - string - the filetype]],
505-
Limitations = [[]],
506-
Example = [[]],
507-
See_Also = [[]],
508-
Reference = [[]],
509-
License = [[]],
510-
Copyright = [[]],
511-
}
512-
513421
function dtutils_file.get_filetype(str)
514-
local parts = dtutils_file.split_filepath(str)
515-
return parts["filetype"]
422+
return ds.get_filetype(str)
516423
end
517424

518425

lib/dtutils/string.lua

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,127 @@ function dtutils_string.sanitize_lua(str)
294294
return str
295295
end
296296

297+
dtutils_string.libdoc.functions["split_filepath"] = {
298+
Name = [[split_filepath]],
299+
Synopsis = [[split a filepath into parts]],
300+
Usage = [[local ds = require "lib/dtutils.string"
301+
302+
local result = ds.split_filepath(filepath)
303+
filepath - string - path and filename]],
304+
Description = [[split_filepath splits a filepath into the path, filename, basename and filetype and puts
305+
that in a table]],
306+
Return_Value = [[result - table - a table containing the path, filename, basename, and filetype]],
307+
Limitations = [[]],
308+
Example = [[]],
309+
See_Also = [[]],
310+
Reference = [[]],
311+
License = [[]],
312+
Copyright = [[]],
313+
}
314+
315+
function dtutils_string.split_filepath(str)
316+
-- strip out single quotes from quoted pathnames
317+
str = string.gsub(str, "'", "")
318+
str = string.gsub(str, '"', '')
319+
local result = {}
320+
-- Thank you Tobias Jakobs for the awesome regular expression, which I tweaked a little
321+
result["path"], result["filename"], result["basename"], result["filetype"] = string.match(str, "(.-)(([^\\/]-)%.?([^%.\\/]*))$")
322+
if result["basename"] == "" and result["filetype"]:len() > 1 then
323+
result["basename"] = result["filetype"]
324+
result["filetype"] = ""
325+
end
326+
return result
327+
end
328+
329+
dtutils_string.libdoc.functions["get_path"] = {
330+
Name = [[get_path]],
331+
Synopsis = [[get the path from a file path]],
332+
Usage = [[local ds = require "lib/dtutils.string"
333+
334+
local result = ds.get_path(filepath)
335+
filepath - string - path and filename]],
336+
Description = [[get_path strips the filename and filetype from a path and returns the path]],
337+
Return_Value = [[result - string - the path]],
338+
Limitations = [[]],
339+
Example = [[]],
340+
See_Also = [[]],
341+
Reference = [[]],
342+
License = [[]],
343+
Copyright = [[]],
344+
}
345+
346+
function dtutils_string.get_path(str)
347+
local parts = dtutils_string.split_filepath(str)
348+
return parts["path"]
349+
end
350+
351+
dtutils_string.libdoc.functions["get_filename"] = {
352+
Name = [[get_filename]],
353+
Synopsis = [[get the filename and extension from a file path]],
354+
Usage = [[local ds = require "lib/dtutils.string"
355+
356+
local result = ds.get_filename(filepath)
357+
filepath - string - path and filename]],
358+
Description = [[get_filename strips the path from a filepath and returns the filename]],
359+
Return_Value = [[result - string - the file name and type]],
360+
Limitations = [[]],
361+
Example = [[]],
362+
See_Also = [[]],
363+
Reference = [[]],
364+
License = [[]],
365+
Copyright = [[]],
366+
}
367+
368+
function dtutils_string.get_filename(str)
369+
local parts = dtutils_string.split_filepath(str)
370+
return parts["filename"]
371+
end
372+
373+
dtutils_string.libdoc.functions["get_basename"] = {
374+
Name = [[get_basename]],
375+
Synopsis = [[get the filename without the path or extension]],
376+
Usage = [[local ds = require "lib/dtutils.string"
377+
378+
local result = ds.get_basename(filepath)
379+
filepath - string - path and filename]],
380+
Description = [[get_basename returns the name of the file without the path or filetype
381+
]],
382+
Return_Value = [[result - string - the basename of the file]],
383+
Limitations = [[]],
384+
Example = [[]],
385+
See_Also = [[]],
386+
Reference = [[]],
387+
License = [[]],
388+
Copyright = [[]],
389+
}
390+
391+
function dtutils_string.get_basename(str)
392+
local parts = dtutils_string.split_filepath(str)
393+
return parts["basename"]
394+
end
395+
396+
dtutils_string.libdoc.functions["get_filetype"] = {
397+
Name = [[get_filetype]],
398+
Synopsis = [[get the filetype from a filename]],
399+
Usage = [[local ds = require "lib/dtutils.string"
400+
401+
local result = ds.get_filetype(filepath)
402+
filepath - string - path and filename]],
403+
Description = [[get_filetype returns the filetype from the supplied filepath]],
404+
Return_Value = [[result - string - the filetype]],
405+
Limitations = [[]],
406+
Example = [[]],
407+
See_Also = [[]],
408+
Reference = [[]],
409+
License = [[]],
410+
Copyright = [[]],
411+
}
412+
413+
function dtutils_string.get_filetype(str)
414+
local parts = dtutils_string.split_filepath(str)
415+
return parts["filetype"]
416+
end
417+
297418

298419

299420
return dtutils_string

0 commit comments

Comments
 (0)