View Raw SPL
/*****************************************************************************
*                                                                            *
*   WINREADALL.SPL Copyright (C) 1995-2010 DSP Development Corporation       *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Cathy Zouval                                                *
*                                                                            *
*   Synopsis:    Reads all formulae, labels and comments from a formula file *
*                                                                            *
*   Revisions:    7 Jun 1995   CZ  Creation                                  *
*                17 Jul 1997  RRR  for syntax, formating                     *
*                25 Jul 2002  RRR  error handlers                            *
*                 4 Aug 2010  RRR  separate functions                        *
*                                                                            *
*****************************************************************************/

#include 


#if @HELP_WINREADALL

    WINREADALL

    Purpose: Reads all formulae, labels and comments in a formula file.

    Syntax:  WINREADALL("filename", lineno, num)

              "filename" - A string, the formula file.

                 lineno  - Optional. An integer, the line number to
                           read the formula. Defaults to 1.

                    num  - Optional. An integer, the number of formulae to
                           read. Defaults to the number of windows.


    Returns: 1 if successful.

    Example:
             W1: grand(10, 1)
             W2: gcos(1000, 1/1000, 10)
             label(w1, "My Window 1");
             label(w2, "My Window 2");

             winwrite("ws.txt");

             newworksheet(2, 0);
             winreadall("ws.txt", 1, 2);

             W1 and W2 contain formulae and labels that are written to
             a file.  The file is read into a new worksheet to rebuild
             the original.

    Remarks:
             WINREADALL reads enough formulas, labels, and comments
             from a file to fill up the current worksheet.  The file
             must be in a format similar to that produced by WINWRITE,
             where each block of three lines to be read in has the
             following Syntax: 

                 the first line has a label, followed by a space,
                 followed by the window formula;

                 the second line has 4 spaces, followed by the window
                 label;

                 and the third line has 4 spaces, followed by the
                 window comment.

             If comments have been added to the top of the file, you
             can use the file line offset parameter with a value of
             greater than 1; if the file is unmodified from a winwrite
             or if the first formula is on the first line, the file
             line offset should be used with a value of 1.  WINREADALL
             reads in as many blocks of three lines as there are
             windows in the worksheet.

             See WINWRITE to write the formula, label and comment.
  
    See Also:
             formread
             formreadn
             formreadall
             formwrite
             winread
             winwrite
             winwriteall
#endif


/* read all formulae, labels and comments in a formula file */
winreadall(file, fln, nforms)
{
        local winform, j, temp, putform, n_win, putlabel, putcomm, labelstr;
        local winstr, fstr;

        /* verify file argument */
        if (argc < 1)
        {
                error("winreadall - filename required");
        }

        if (not(isstring(file)))
        {
                error("winreadall - filename required");
        }

        if (fopen(file, "r") != TRUE)
        {
                error(sprintf("winreadall - cannot open % s", file));
        }

        n_win = numwin;

        /* default arguments */
        if (argc < 3)
        {
                nforms = n_win;
                if (argc < 2)
                {
                        fln = 1;
                }
        }

        if (not(fln == 1))
        {
                for (j = 1; j < fln; j++)
                {
                        temp = fgets(file);
                }
        }

        for (j = 1; j <= n_win; j++)
        {
                if (j <= nforms )
                {
                        fstr = fgets(file);
                        if (strlen(fstr) <= 0) break;

                        winstr = strget(1, fstr, ":");

                        winform  = strfind(" ", fstr);
                        putform  = sprintf("setwform(%s, strextract(winform,2,strlen(winform)-2))", winstr);

                        labelstr = fgets(file);
                        labelstr = winreadall_stripspace(labelstr);

                        putlabel = sprintf("label(%s, labelstr)", winstr);

                        commstr  = fgets(file);
                        commstr  = winreadall_stripspace(commstr);

                        putcomm  = sprintf("comment(%s, commstr)", winstr);

                        eval(putform);
                        eval(putlabel);
                        eval(putcomm);
                }
        }

        fclose(file);
}


/* strip leading whitespace */
winreadall_stripspace(str)
{
        if (strlen(str) > 0)
        {
                if (str[1] == charstr(strescape("\t")))
                {
                        (str, rem) = strtok(str, strescape("\t"));
                        str = str + rem;
                }
                if (str[1] == charstr(" "))
                {
                        (str, rem) = strtok(str, " ");
                        str = str + rem;
                }
        }
        return(str);
}


winreadall_error(errnum, errmes)
{
        /* close files if error */
        fcloseall();
}