View Raw SPL
/*****************************************************************************
*                                                                            *
*   FILEPARTS.SPL  Copyright (C) 2016 DSP Development Corporation            *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Returns directory, filename and extension of a path string  *
*                                                                            *
*   Revisions:   23 Sep 2016  RRR  Creation - from dirpath.spl               *
*                                                                            *
*****************************************************************************/

#if @HELP_FILEPARTS

    FILEPARTS

    Purpose: Returns the directory, filename and extension of a path string.

    Syntax:  FILEPARTS(pathname)

             (path, filename, ext) = FILEPARTS(pathname)

              pathname - A string, a full path string containing a
                         directory component.

    Returns: A string, the directory name.

             (path, filename) = FILEPARTS(pathname) returns the path and 
             filename with extension as strings.

             (path, filename, ext) = FILEPARTS(pathname) returns the path, 
             filename without extension and file extension (if any) with a
             leading period (.) as strings.

    Example:
             FILEPARTS("\dsp\system.mac")

             returns the directory component "\dsp"

    Example:
             (path, fname) = FILEPARTS("\dsp\system.mac")

             path  == "\dsp"
             fname == "system.mac"

    Example:
             (path, fname, ext) = FILEPARTS("\dsp\system.mac")

             path  == "\dsp"
             fname == "system"
             ext   == ".mac"

    Remarks:
             The returned path component does not contain a trailing path
             separator.

             The extension component contains a leading period if
             it exists.

             The file specified by PATHNAME does not have to exist.

    See Also:
             Dirpath
             Strfind
             Strrev
#endif


/* get and store directory name */
fileparts(pathname)
{
        local path, fname, ext;

        path = fname = ext = "";

        if (outargc > 2)
        {
                (path, fname, ext) = dirpath(pathname, 0);
        }
        else
        {
                (path, fname) = dirpath(pathname, 0);
        }

        if (strlen(path) > 1 && strlen(strfind(pathchar, path)) > 0)
        {
                /* strip trailing pathchar */
                path = strextract(path, 1, strlen(path) - 1);
        }

        /* check for C: constructs */
        if (strlen(path) == 0 && strlen(strfind(":", fname)) > 0)
        {
                path  = strget(1, fname, ":") + ":";
                fname = strget(2, fname, ":");
        }

        if (strlen(ext) > 0)
        {
                /* add leading "." */
                ext = "." + ext;
        }

        return(path, fname, ext);
}