View Raw SPL
/*****************************************************************************
*                                                                            *
*   SPLLOADALL.SPL  Copyright (C) 2010 DSP Development Corporation           *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    compiles and loads all SPL functions in a folder            *
*                                                                            *
*   Revisions:    8 Dec 2010  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/

#if @HELP_SPLLOADALL


    SPLLOADALL

    Purpose: Compiles and loads one or more SPL functions in a folder.

    Syntax:  SPLLOADALL("filespec", recurse, overwrite, expand, verbose)

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

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

              overwrite - Optional. An integer, macro overwrite.
                           0: do not replace macro of same name
                           1: replace macro if it exists (default)

                 expand - Optional. An integer, add debugging info to OPL file. 
                           0: do not include source code
                           1: include source code (default)

                verbose - Optional. An integer, display messages. 
                           0: do not provide error messages
                           1: display compile errors (default)
 
    Returns: An integer, the number of files compiled.

    Example:
             splloadall(".\spl\dsp\*.spl")

             compiles all .SPL files in the .\spl\dsp directory.

    Example:
             splloadall(".\spl\*.spl", 1)

             compiles all .SPL files in the .\spl directory and all 
             sub-directories.

    Remarks:
             SPLLOADALL creates OPL files and also loads the functions
             into the Worksheet.

             Any errors found during compile are written to the ASCII
             text file, filename.err.

    See Also:

             Functions
             Splcompile
             Splload
             Splread
             Splwrite
#endif


/* compile all SPL functions in a folder as specified by filespec */
splloadall(filespec, recurse, ovr, expand, verbose)
{
        local i, cnt = 0, filelist, dir, fname, mes;

        if (argc < 5)
        {
                if (argc < 4)
                {
                        if (argc < 3)
                        {
                                if (argc < 2)
                                {
                                        if (argc < 1) error("splloadall - filename required");
                                        
                                        recurse = 0
                                }
                                
                                ovr = 1;
                        }
                        
                        expand = 1;
                }
                
                verbose = 1;
        }

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

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

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

        /* dir + splloadall.spl */
        ourname = which("splloadall");

        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;
                        }

                        /* don't compile ourself */
                        if (strcmp(ourname, file, 0) != 0)
                        {
                                splload(file, ovr, expand, verbose);
                                cnt++;
                        }
                        
                        i++;
                }
                else
                {
                        break;
                }
        }
        
        if (outargc > 0)
        {
                /* return successful count */
                return(cnt);
        }
        else if (verbose)
        {
                /* display message */
                mes = (cnt == 1) ? "%d SPL Function Loaded" : "%d SPL Functions Loaded";
                return(sprintf(mes, cnt));
        }
}


splloadall_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);
}