# get_proper_dir

Definition: | string path = get_proper_dir(string filepath, bool remove_slash=false)
Description: | Return the full directory path of the supplied file with the name removed.
pwa/p2js: | Implemented by hand in p2js.js - see technicalia below.
Comments: | Result includes a trailing path separator, unless the optional parameter remove_slash is 1. Note that when a directory is specified the presence or otherwise of a separator at the end of the input string directly affects the output, as shown in the example below. Both forwardslash ('/') and backslash ('\\') are supported on all platforms, and converted appropriately in the output. Also as shown below, get_proper_dir(".") may sometimes be a slightly better alternative to current_dir(). The result should not be taken as confirmation of the actual existence of a directory.
Examples: |
    constant fullpath = `C:\Program Files (x86)\Phix\p.exw`
    printf(1,"`%s`\n",get_proper_dir(fullpath))     -- prints `C:\Program Files (x86)\Phix\`
    printf(1,"`%s`\n",get_proper_dir("p.exw"))      -- prints `C:\Program Files (x86)\Phix\`        (`` on p2js)
    printf(1,"`%s`\n",get_proper_dir("demo"))       -- prints `C:\Program Files (x86)\Phix\`        (   ""     )
    printf(1,"`%s`\n",get_proper_dir(`demo\`))      -- prints `C:\Program Files (x86)\Phix\demo\`   (demo\ p2js)
    printf(1,"`%s`\n",get_proper_dir("."))          -- prints `C:\Program Files (x86)\Phix\`        (`` on p2js)
    printf(1,"`%s`\n",get_proper_dir(".",true))     -- prints `C:\Program Files (x86)\Phix`         (   ""     )
    printf(1,"`%s`\n",current_dir())                -- prints `C:\Program Files (x86)\Phix`         ( not p2js )
The use of a named parameter when setting remove_slash is recommended, to make the intent clear and the code easier to read. As previously noted above only compatible/consistent under p2js when a full path is provided.
Implementation: | See builtins\pgetpath.e (an autoinclude) for details of the actual implementation.
See Also: | get_proper_path, current_dir
Technicalia | Note that while desktop/Phix invokes get_proper_path(), under pwa/p2js that’s a null-op, so obviously that call was omitted from the manual translation/rewrite, which I did in preference to including builtins\find.js either for each and every program, or as a special condition whenever get_proper_dir() was being invoked. So 12 lines of JavaScript by hand was just the better/faster/simpler fix. Also note that get_proper_path("p.exw") yields the full path on desktop/Phix but "" (the empty string) under pwa/p2js, they are only compatible/consistent when a full path is provided. As with get_proper_path(), implemented so that builtins\file_utils.e can be auto-transpiled, since there is no particular reason not to given all routines in that file are (deliberately) just string/number manipulation.