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

#if @HELP_SPLCOMPILEALL


    SPLCOMPILEALL

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

    Syntax:  SPLCOMPILEALL("filespec", recurse, overwrite, expand, load, 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 (default)
                           1: include source code

                   load - Optional. An integer, load function.
                           0: do not load function into memory (default)
                           1: load function into memory

                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:
             splcompileall(".\spl\dsp\*.spl")

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

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

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

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

             Same as above except the compiled functions are also
             loaded into memory.

    Remarks:
             SPLCOMPILEALL creates OPL files. Set the LOAD option to 1
             to also load 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 */
splcompileall(filespec, recurse, ovr, expand, load, verbose)
{
        local i, cnt = 0, filelist, dir, fname, mes;

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

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

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

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

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

        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)
                        {
                                splcompile(file, ovr, expand, load, verbose);
                                cnt++;
                        }
                        i++;
                }
                else
                {
                        break;
                }
        }
        
        if (outargc > 0)
        {
                /* return successful count */
                return(cnt);
        }
        else if (verbose)
        {
                /* display message */
                mes = (cnt == 1) ? "%d SPL Function Compiled" : "%d SPL Functions Compiled";
                return(sprintf(mes, cnt));
        }
}


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