aboutsummaryrefslogtreecommitdiff
path: root/jim-file.c
diff options
context:
space:
mode:
authorSteve Bennett <steveb@workware.net.au>2019-10-18 07:52:34 +1000
committerSteve Bennett <steveb@workware.net.au>2019-10-18 08:00:08 +1000
commit80fea1f6e6c5af588faa9159e165198a13983efe (patch)
treee72a2283fb54bf4a753c50c5210e1d5151e24cf4 /jim-file.c
parent058d07b597ce088b86e7e8a070bcfa07509f26e1 (diff)
downloadjimtcl-80fea1f6e6c5af588faa9159e165198a13983efe.zip
jimtcl-80fea1f6e6c5af588faa9159e165198a13983efe.tar.gz
jimtcl-80fea1f6e6c5af588faa9159e165198a13983efe.tar.bz2
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>
Diffstat (limited to 'jim-file.c')
-rw-r--r--jim-file.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/jim-file.c b/jim-file.c
index f47e236..24f39e5 100644
--- a/jim-file.c
+++ b/jim-file.c
@@ -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,