View Raw SPL
/*****************************************************************************
*                                                                            *
*   WINREAD.SPL Copyright (C) 1995-2010 DSP Development Corporation          *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Cathy Zouval                                                *
*                                                                            *
*   Synopsis:    Reads a formula, label and comment 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_WINREAD

    WINREAD

    Purpose: Reads and restores a formula, label and comment in a formula file.

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

              "filename" - A string, the output 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)
             label(w1, "My Window 1");
             label(w2, "My Window 2");

             winwrite("ws.txt");

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

             W1 and W2 contain formulae and labels that are written to
             a file. The first formula an label is read and placed
             into W2.

    Remarks:
             WINREAD reads a formula, label, and comment from a file
             and puts them into a window.  The file must be in a format
             similar to that produced by WINWRITE, where the 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.


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


/* read a formula, label and comment from a formula file */
winread(file, fln, dwn)
{
        local winform, j, temp, putform, fstr, winstr;

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

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

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

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

        if (not(fln == 1))
        {
                for (j = 1; j < fln; j++)
                {
                        temp = fgets(file);
                }
        }
        
        fstr = fgets(file);
        
        if (strlen(fstr) > 0)
        {
                if (dwn >= 0)
                {
                        winstr = sprintf("w%d", dwn);
                }
                else
                {
                        winstr  = strget(1, fstr, ":");
                }

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

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

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

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

        fclose(file);
}


/* strip leading whitespace */
winread_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);
}


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