View Raw SPL
/*****************************************************************************
*                                                                            *
*   FORMREAD.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_FORMREAD

    FORMREAD

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

    Syntax:  FORMREAD("filename", lineno, winnum)

              "filename" - A string, the formula file.

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

                 winnum  - Optional. An integer, the destination window.
                           -1 specifies the original window number.
                           Defaults to 0, the current window.


    Returns: 1 if successful.

    Example:
             W1: grand(10, 1)
             W2: gcos(1000, 1/1000, 10)

             formwrite("ws.txt");

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


             W1 and W2 contain formulae that are written to a file. The
             second formula is read and placed into W1.

    Remarks:

             FORMREAD reads one formula from a file and puts it into a
             window.  The file must be in a format similar to that
             produced by FORMWRITE, where the line to be read in has a
             label, followed by a space, followed by the window
             formula.

             See FORMREADALL to read all the formulae in a file.
  
    See Also:
             formreadn
             formreadall
             formwrite
             winread
             winreadall
             winwrite
             winwriteall
#endif


/* read a single formula */
formread(file, fln, dwn)
{
        local winform, j, fstr;

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

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

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

        /* default line number and destination window */
        if (argc < 3)
        {
                dwn = 0;
                
                if (argc < 2)
                {
                        fln = 1;
                }
        }

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

                if (dwn >= 0)
                {
                        winstr = sprintf("w%d", dwn);
                }
                else
                {
                        winstr  = strget(1, fstr, ":");
                }
                
                winform = strfind(' ', fstr);
        }
        
        fclose(file);

        eval(sprintf("setwform(%s, strextract(winform,2,strlen(winform)-2))", winstr));
}


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