diff options
author | Steve Bennett <steveb@workware.net.au> | 2019-10-18 07:52:34 +1000 |
---|---|---|
committer | Steve Bennett <steveb@workware.net.au> | 2019-10-18 08:00:08 +1000 |
commit | 80fea1f6e6c5af588faa9159e165198a13983efe (patch) | |
tree | e72a2283fb54bf4a753c50c5210e1d5151e24cf4 | |
parent | 058d07b597ce088b86e7e8a070bcfa07509f26e1 (diff) |
file: Add file split
This subcommand was missing as it wasn't part of the original Tcl 6.7
Signed-off-by: Steve Bennett <steveb@workware.net.au>
-rw-r--r-- | jim-file.c | 36 | ||||
-rw-r--r-- | jim_tcl.txt | 10 | ||||
-rw-r--r-- | tests/file.test | 5 |
3 files changed, 47 insertions, 4 deletions
@@ -275,6 +275,35 @@ static int file_cmd_dirname(Jim_Interp *interp, int argc, Jim_Obj *const *argv) return JIM_OK; } +static int file_cmd_split(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_Obj *listObj = Jim_NewListObj(interp, NULL, 0); + const char *path = Jim_String(argv[0]); + + if (*path == '/') { + Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, "/", 1)); + } + + while (1) { + /* Remove leading slashes */ + while (*path == '/') { + path++; + } + if (*path) { + const char *pt = strchr(path, '/'); + if (pt) { + Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, path, pt - path)); + path = pt; + continue; + } + Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, path, -1)); + } + break; + } + Jim_SetResult(interp, listObj); + return JIM_OK; +} + static int file_cmd_rootname(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { Jim_Obj *objPtr = JimStripTrailingSlashes(interp, argv[0]); @@ -890,6 +919,13 @@ static const jim_subcmd_type file_command_table[] = { 1, /* Description: Last component of the name */ }, + { "split", + "name", + file_cmd_split, + 1, + 1, + /* Description: Split path into components as a list */ + }, { "normalize", "name", file_cmd_normalize, diff --git a/jim_tcl.txt b/jim_tcl.txt index 352071e..e408e37 100644 --- a/jim_tcl.txt +++ b/jim_tcl.txt @@ -58,6 +58,7 @@ Changes since 0.78 2. `aio` now supports datagram Unix-Domain sockets 3. Add support for `aio lock -wait` 4. Add `signal block` to prevent delivery of signals +5. Add support for `file split` Changes between 0.77 and 0.78 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -2418,10 +2419,11 @@ abbreviation for +'option'+ is acceptable. The valid options are: the last '.' character in the name. If +'name'+ doesn't contain a dot, then return +'name'+. -+*file size* 'name'+:: - Return a decimal string giving the size of file +'name'+ in bytes. - If the file doesn't exist or its size cannot be queried then an - error is generated. ++*file split* 'name'+:: + Returns a list whose elements are the path components in +'name'+. + The first element of the list will have the same path type as + +'name'+. All other elements will be relative. Path separators + will be discarded. +*file stat* 'name ?varName?'+:: Invoke the 'stat' kernel call on +'name'+, and return the result diff --git a/tests/file.test b/tests/file.test index 10211cd..fb5a555 100644 --- a/tests/file.test +++ b/tests/file.test @@ -147,5 +147,10 @@ if {$tcl_platform(platform) eq {windows}} { test file.17 "picol test" {file join foo C:/bar grill} C:/bar/grill } +test file.18 "picol test" {file split {/foo/space station/bar}} {/ foo {space station} bar} +test file.19 "picol test" {file split {/foo/space station/bar/}} {/ foo {space station} bar} +test file.20 "picol test" {file split {foo/space station/bar}} {foo {space station} bar} +test file.21 "picol test" {file split foo///bar} {foo bar} +test file.22 "picol test" {file split foo} foo testreport |