View Raw SPL
/*****************************************************************************
*                                                                            *
*   FORMWRITE.SPL Copyright (C) 1995-2010 DSP Development Corporation        *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Cathy Zouval                                                *
*                                                                            *
*   Synopsis:    Writes all formulae of the current worksheet 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_FORMWRITE

    FORMWRITE

    Purpose: Writes all the formulas of the current worksheet to a file.

    Syntax:  FORMWRITE("filename")

              "filename" - A string, the output file.


    Returns: 1 if successful.

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

             formwrite("ws.txt");

             newworksheet(2, 0);
             formread("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:
             FORMWRITE writes all the formulas of the current worksheet
             to a file.  in the file, each formula is on a separate
             line and is preceded by its window number.  If a file
             already exists with same name before FORMWRITE, it is
             overwritten without warning.

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


/* write window formula */
formwrite(file)
{
        local n_win, j, wstr, str, zz, cstr;

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

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

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

        npathchr = 0;
        n_win = numwin;

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

                        /* 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);
                }
         }
         
         fclose(file);
}


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