Clean path

angerangel edited this page Jan 2, 2013 · 1 revision

Table of Contents

USAGE

  CLEAN-PATH file /only /dir

DESCRIPTION

Returns new directory path with //, . and .. processed. CLEAN-PATH is a function value.

ARGUMENTS

  • file (file! url! string!)

REFINEMENTS

  • /only -- Do not prepend current directory
  • /dir -- Add a trailing / if missing

SOURCE

clean-path: make function! [  [
    {Returns new directory path with //, . and .. processed.}
    file [file! url! string!]
    /only "Do not prepend current directory"
    /dir "Add a trailing / if missing"
    /local out cnt f
][
    case [
        any [only not file? file] [file: copy file]
        #"/" = first file [
            ++ file
            out: next what-dir
            while [
                all [
                    #"/" = first file
                    f: find/tail out #"/"
                ]
            ] [
                ++ file
                out: f
            ]
            file: append clear out file
        ]
        file: append what-dir file
    ]
    if all [dir not dir? file] [append file #"/"]
    out: make file length? file
    cnt: 0
    parse/all reverse file [
        some [
            "../" (++ cnt)
            | "./"
            | #"/" (if any [not file? file #"/" <> last out] [append out #"/"])
            | copy f [to #"/" | to end] (
                either cnt > 0 [
                    -- cnt
                ] [
                    unless find ["" "." ".."] to string! f [append out f]
                ]
            )
        ]
    ]
    if all [#"/" = last out #"/" <> last file] [remove back tail out]
    reverse out
] ] 
Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.