View Raw SPL
/*****************************************************************************
*                                                                            *
*   FORMREADALL.SPL Copyright (C) 1995-2010 DSP Development Corporation      *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Cathy Zouval                                                *
*                                                                            *
*   Synopsis:    Reads formulae 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_FORMREADALL

    FORMREADALL

    Purpose: Reads and restores the formulae in a formula file.

    Syntax:  FORMREADALL("filename", num)

              "filename" - A string, the formula file.

                    num  - Optional. An integer, the number formulae in the
                           file. Defaults to the number of windows in the
                           current worksheet.

    Returns: 1 if successful.

    Example:
             W1: grand(10, 1)
             W2: integ(W1)

             formwrite("ws.txt");

             newworksheet(2, 0);
             formreadall("ws.txt");

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

    Remarks:
             FORMREADALL reads enough formulas from a file to fill up
             the current worksheet.  The file must be in a format
             similar to that produced by FORMREADALL, where each line
             has a label, followed by a space, followed by the window
             formula, and the first line contains the first window
             formula.

             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
             formwrite or if the first formula is on the first line,
             the file line offset should be used with a value of 1. 
             FORMREADALL reads in as many lines as there are windows in
             the worksheet.

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


/* read all formula from a formula file */
formreadall(file, tnl)
{
        local n_win, j, winform, fstr, winstr;

        /* verify file argument */
        if (argc < 2)
        {
                if (argc < 1)
                {
                        error("formreadn - filename required");
                }
                
                tnl = numwin;
        }

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

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

        n_win = numwin;

        calc(off);

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

                        winstr = strget(1, fstr, ":");
                        winform = strfind(' ', fstr);
                        eval(sprintf("setwform(%s, strext(winform,2,strlen(winform)-2))", winstr));
                }
        }

        calc(on);
        fclose(file);
}

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