View Raw SPL
/*****************************************************************************
*                                                                            *
*   WINWRITE.SPL Copyright (C) 1995-2010 DSP Development Corporation         *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Cathy Zouval                                                *
*                                                                            *
*   Synopsis:    Writes all formulae, labels and comments to a 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_WINWRITE

    WINWRITE

    Purpose: Writes all formulae, labels and comments to a file.

    Syntax:  WINWRITE("filename")

              "filename" - A string, the output file.


    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");

             W1 and W2 contain formulae and labels that are written to
             a file. The formulae and labels are read and restored to a
             new worksheet.

    Remarks:
             WINWRITE writes the formula, label, and comment of all the
             windows of the current worksheet to a file. In the file,
             three lines are output for each window: 
               
               The first line contains the window number and the
               formula.

               The second line contains the window's label preceeded by
               four spaces.

               The third line contains the series comment preceeded by
               four spaces.

             If a file already exists with same name before winwrite,
             it is overwritten without warning.

             See WINREADEALL to read all formulae, label and comment.
  
    See Also:
             formread
             formreadn
             formreadall
             formwrite
             winread
             winreadall
             winwriteall
#endif


/* write all formulae, labels and comments */
winwrite(file)
{
        local n_win, j, wstr, str, zz, wlab_str, lab_str;
        local wcom_str, com_str, formstr, cstr;

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

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

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

        n_win = numwin;

        for (j = 1; j <= n_win; j++)
        {
                wstr = eval(sprintf("getwform(w%d)", j));
                str  = fixslash(wstr);

                wlab_str = eval(sprintf("getlabel(w%d)", j));
                lab_str  = fixslash(wlab_str);

                if (eval(sprintf("length(w%d)", j)) > 0)
                {
                        wcom_str = eval(sprintf("getcomment(w%d)", j));
                        com_str  = fixslash(wcom_str);
                }
                else
                {
                        com_str = "";
                }

                if (strlen(wstr) > 0 || strlen(wlab_str) > 0)
                {
                        /* prepend space if necessary */
                        cstr = charstrs(str);
                        
                        if (cstr[1] != 32) str = strcat(" ", str);

                        formstr = sprintf("w%d:%s\n", j, str);
                        fputs(formstr, file, 2);

                        formstr = sprintf("    %s\n", lab_str);
                        fputs(formstr, file, 2);

                        formstr = sprintf("    %s\n", com_str);
                        fputs(formstr, file, 2);
                }
        }
        
        fclose(file);
}


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