View Raw SPL
/*****************************************************************************
*                                                                            *
*   DIRPATH.SPL     Copyright (C) 1999-2016 DSP Development Corporation      *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Returns directory component of a path string                *
*                                                                            *
*   Revisions:   27 Apr 1999  RRR  Creation                                  *
*                 6 Feb 2009  RRR  Added filename as optional return value   *
*                23 Oct 2016  RRR  Extension - for FILEPARTS                 *
*                                                                            *
*****************************************************************************/

#if @HELP_DIRPATH

    DIRPATH

    Purpose: Returns the directory component of a path string

    Syntax:  DIRPATH(pathname, labpath)

             (path, filename) = DIRPATH(pathname, labpath)

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

               labpath - Optional integer, add labbook path if none found.

                           0: do not add path
                           1: add path (default)

    Returns: A string, the directory name.

             (path, filename) = DIRPATH(pathname) returns both the path
             and filename component.

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

             returns the directory component "\dsp\"

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

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

    Remarks:
             DIRPATH is used internally to retrieve the last directory
             in file dialog boxes - see system.mac.

             By default, if the path string does not contain a
             directory component, the current LABBOOK path is returned.
             set LABPATH to 0 to return an emprt path string.

    See Also:
             Eval
             Strfind
             Strrev
             system.mac
#endif


/* get and store directory name */
dirpath(pathname, labpath)
{
        local dir, fname, len, ext;

        if (argc < 2)
        {
                if (argc < 1)
                {
                        /* this will default to getlabpath */
                        pathname = "";
                }
                
                labpath = 1;
        }

        /* find directory component and save */
        dir = strrev(strfind(pathchar, strrev(pathname)));

        if (strlen(dir) == 0 && labpath)
        {
                /* no directory, default to Labbook directory */
                dir = getlabpath();
        }
        
        if (outargc > 1)
        {
                /* path and fname */
                fname = strrev(strget(1, strrev(pathname), pathchar));

                if (outargc > 2)
                {
                        /* path, fname and extension */
                        ext = strrev(strfind(".", strrev(fname)));

                        if ((len = strlen(ext)) > 0)
                        {
                                ext   = strextract(fname, len + 1, -1);
                                fname = strextract(fname, 1, len - 1);
                        }

                        return(dir, fname, ext);
                }

                return(dir, fname);
        }
        
        return(dir);
}