From 3ac520a8ecb1af0849d4cc40c36b284b02bf40e9 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sat, 7 Jul 2018 11:59:59 -0500 Subject: [PATCH] io.pathnames: Add canonicalize-path. The idea is to make a canonical representation of any path, taking into account . and .. and unicode-prefix on Windows. The use case is in a shell you have a current-directory and you can do crazy commands like ``cd ../foo/bar/baz/../.././././`` and get the canonical/shortened directory name. You can also use this word to compare if two paths are the same. --- basis/io/files/windows/windows.factor | 16 ++++- core/io/pathnames/pathnames-tests.factor | 81 +++++++++++++++++++++++- core/io/pathnames/pathnames.factor | 53 +++++++++++++++- 3 files changed, 142 insertions(+), 8 deletions(-) diff --git a/basis/io/files/windows/windows.factor b/basis/io/files/windows/windows.factor index 556bbfc4ee..2ed1926cdb 100755 --- a/basis/io/files/windows/windows.factor +++ b/basis/io/files/windows/windows.factor @@ -8,7 +8,7 @@ io.files.types io.pathnames io.ports io.streams.c io.streams.null io.timeouts kernel libc literals locals math math.bitwise namespaces sequences specialized-arrays system threads tr vectors windows windows.errors windows.handles windows.kernel32 windows.shell32 -windows.time windows.types windows.winsock ; +windows.time windows.types windows.winsock splitting ; SPECIALIZED-ARRAY: ushort IN: io.files.windows @@ -326,11 +326,14 @@ M: windows root-directory? ( path -- ? ) [ drop f ] } cond ; -: prepend-prefix ( string -- string' ) +: prepend-unicode-prefix ( string -- string' ) dup unicode-prefix head? [ unicode-prefix prepend ] unless ; +: remove-unicode-prefix ( string -- string' ) + unicode-prefix ?head drop ; + TR: normalize-separators "/" "\\" ; +M: windows canonicalize-path + remove-unicode-prefix canonicalize-path* ; + +M: object root-path remove-unicode-prefix root-path* ; + +M: object relative-path remove-unicode-prefix relative-path* ; + M: windows normalize-path ( string -- string' ) dup unc-path? [ normalize-separators ] [ absolute-path normalize-separators - prepend-prefix + prepend-unicode-prefix ] if ; pathname M: pathname absolute-path string>> absolute-path ; -M: pathname <=> [ string>> ] compare ; +M: pathname <=> [ string>> ] compare ; \ No newline at end of file