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

    FORMREADN

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

    Syntax:  FORMREADN("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)
             W3: integ(w2)

             formwrite("ws.txt");

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


             W1, W2 and W3 contain formulae that are written to a file. The
             first two formulae are read.

    Remarks:

             FORMREADN reads the specified number of formulas starting
             with the designated formula.  Remember the number of
             windows available is a limiting factor.

             The file must be in a format similar to that produced by
             FORMWRITE, 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 FORMREADALL to read all the formulae in a file.
  
    See Also:
             formread
             formreadall
             formwrite
             winread
             winreadall
             winwrite
             winwriteall
#endif


/* read N formulae */
formreadn(file, sln, fnum)
{
        local n_win, j, zz, winform, fstr, winstr;

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

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

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

        calc(off);
        n_win = numwin;

        for (j = 1; j < sln; j++)
        {
                winform = strfind(' ', fgets(file));
        }

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

                        winstr  = strget(1, fstr, ":");
                        winform = strfind(' ', fstr);
                        eval(sprintf("setwform(%s, strextract(winform,2,strlen(winform)-2))", winstr));
                }
                else break;
        }
        
        calc(on);
        fclose(file);
}


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