View Raw SPL
/*****************************************************************************
*                                                                            *
*   MACREADALL.SPL  Copyright (C) 2023 DSP Development Corporation           *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Loads all macros files in a folder                          *
*                                                                            *
*   Revisions:    9 Jun 2023  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/

static sp = "";

#if @HELP_MACREADALL

    MACREADALL

    Purpose: Loads one or more macro files in a folder.

    Syntax:  MACREADALL("filespec", recurse, verbose)

             "filespec" - String. The folder or file specification with
                          optional * or ? wildcard characters.

                recurse - Optional. An integer, recurse into sub-folders.
                           0: do not recurse (default)
                           1: recurse

                verbose - Optional. An integer, display messages. 
                           0: do not display messages
                           1: display all messages (default)
 
    Returns: An integer, the number of macro files read.

    Example:
             macreadall(gethome + "macros\*.mac")

             Reads all the .MAC macro files in the .\macros folder.

    Example:
             macreadall(gethome + "macros\*.mac", 1)

             Reads all .MAC files in the .\macros folder and all 
             sub-folders.

    Remarks:
             MACREADALL reads one or more macro files and loads
             the macros into the Worksheet.

    See Also:
             Macros
             Macread
             Macwrite
             Splloadall
#endif


/* Read all macro files in a folder as specified by filespec */
macreadall(filespec, recurse, verbose)
{
        local i, cnt = 0, filelist, dir, fname, mes;

        if (argc < 3)
        {
                if (argc < 2)
                {
                        if (argc < 1) error(sprintf("%s - filename required", __FUNC__));
                                        
                        recurse = 0
                }
                                
                verbose = 1;
        }

        /* check inputs */
        if (strlen(filespec) == 0)
        {
                return(0);
        }

        if (not(verbose))
        {
                sp = getconf("status_processing");
                setconf("status_processing", "0");
        }

        /* get directory */
        (dir, fname) = dirpath(filespec, 1);

        /* get newline delimited filelist */
        filelist = macreadall_filelist(filespec, recurse);

        i = 1;

        while (1)
        {
                file = strget(i, filelist, strescape("\n"));
                
                if (strlen(file) > 0)
                {
                        (filedir, fname) = dirpath(file, 0);

                        if (strlen(filedir) <= 0)
                        {
                                file = dir + file;
                        }

                        macread(file);
                        cnt++;
                        
                        i++;
                }
                else
                {
                        break;
                }
        }

        if (strlen(sp) > 0)
        {
                setconf("status_processing", sp);
                sp = "";
        }
        
        if (outargc > 0)
        {
                /* return successful count */
                return(cnt);
        }
        else if (verbose)
        {
                /* display message */
                mes = (cnt == 1) ? "%d Macro File Loaded" : "%d Macro Files Loaded";
                return(sprintf(mes, cnt));
        }
}


macreadall_filelist(spec, recurse)
{
        local tempfile, cmd, options, s = "";

        if (argc < 2)
        {
                if (argc < 1) spec = "*.*";
                
                recurse = 0;
        }

        /* flag to recurse into sub-directories */
        options = recurse ? "/S" : "";

        /* build temporary file */
        tempfile = getmiscpath(1, 1) + "templist.txt";

        /* directory listing command */
        cmd = sprintf('dir "%s" /b %s > "%s"', spec, options, tempfile);

        /* run listing */
        run(cmd, -1);

        /* check tempfile existence */
        if (fstat(tempfile) > 0)
        {
                /* return listing as a string */
                s = strfile(tempfile, 0, 1, 0, 0);
                delfile(tempfile);
        }
        
        return(s);
}


macreadall_error(errnum, errmes)
{
        if (strlen(sp) > 0)
        {
                setconfig("status_processing", sp);
                sp = "";
        }

        error(errmes);
}