Back to index

tetex-bin  3.0
lib_screen.c
Go to the documentation of this file.
00001 /****************************************************************************
00002  * Copyright (c) 1998-2001,2002 Free Software Foundation, Inc.              *
00003  *                                                                          *
00004  * Permission is hereby granted, free of charge, to any person obtaining a  *
00005  * copy of this software and associated documentation files (the            *
00006  * "Software"), to deal in the Software without restriction, including      *
00007  * without limitation the rights to use, copy, modify, merge, publish,      *
00008  * distribute, distribute with modifications, sublicense, and/or sell       *
00009  * copies of the Software, and to permit persons to whom the Software is    *
00010  * furnished to do so, subject to the following conditions:                 *
00011  *                                                                          *
00012  * The above copyright notice and this permission notice shall be included  *
00013  * in all copies or substantial portions of the Software.                   *
00014  *                                                                          *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
00016  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
00017  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
00018  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
00019  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
00020  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
00021  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
00022  *                                                                          *
00023  * Except as contained in this notice, the name(s) of the above copyright   *
00024  * holders shall not be used in advertising or otherwise to promote the     *
00025  * sale, use or other dealings in this Software without prior written       *
00026  * authorization.                                                           *
00027  ****************************************************************************/
00028 
00029 /****************************************************************************
00030  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
00031  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
00032  *     and: Thomas E. Dickey 1996 on                                        *
00033  ****************************************************************************/
00034 
00035 #include <curses.priv.h>
00036 
00037 MODULE_ID("$Id: lib_screen.c,v 1.28 2002/09/14 23:30:41 tom Exp $")
00038 
00039 NCURSES_EXPORT(WINDOW *)
00040 getwin(FILE * filep)
00041 {
00042     WINDOW tmp, *nwin;
00043     int n;
00044 
00045     T((T_CALLED("getwin(%p)"), filep));
00046 
00047     clearerr(filep);
00048     (void) fread(&tmp, sizeof(WINDOW), 1, filep);
00049     if (ferror(filep))
00050        returnWin(0);
00051 
00052     if (tmp._flags & _ISPAD) {
00053        nwin = newpad(tmp._maxy + 1, tmp._maxx + 1);
00054     } else {
00055        nwin = newwin(tmp._maxy + 1, tmp._maxx + 1, 0, 0);
00056     }
00057 
00058     /*
00059      * We deliberately do not restore the _parx, _pary, or _parent
00060      * fields, because the window hierarchy within which they
00061      * made sense is probably gone.
00062      */
00063     if (nwin != 0) {
00064        nwin->_curx = tmp._curx;
00065        nwin->_cury = tmp._cury;
00066        nwin->_maxy = tmp._maxy;
00067        nwin->_maxx = tmp._maxx;
00068        nwin->_begy = tmp._begy;
00069        nwin->_begx = tmp._begx;
00070        nwin->_yoffset = tmp._yoffset;
00071        nwin->_flags = tmp._flags & ~(_SUBWIN);
00072 
00073        nwin->_attrs = tmp._attrs;
00074        nwin->_nc_bkgd = tmp._nc_bkgd;
00075 
00076        nwin->_notimeout = tmp._notimeout;
00077        nwin->_clear = tmp._clear;
00078        nwin->_leaveok = tmp._leaveok;
00079        nwin->_idlok = tmp._idlok;
00080        nwin->_idcok = tmp._idcok;
00081        nwin->_immed = tmp._immed;
00082        nwin->_scroll = tmp._scroll;
00083        nwin->_sync = tmp._sync;
00084        nwin->_use_keypad = tmp._use_keypad;
00085        nwin->_delay = tmp._delay;
00086 
00087        nwin->_regtop = tmp._regtop;
00088        nwin->_regbottom = tmp._regbottom;
00089 
00090        if (tmp._flags & _ISPAD)
00091            nwin->_pad = tmp._pad;
00092 
00093        for (n = 0; n <= nwin->_maxy; n++) {
00094            clearerr(filep);
00095            (void) fread(nwin->_line[n].text,
00096                       sizeof(NCURSES_CH_T),
00097                       (size_t) (nwin->_maxx + 1),
00098                       filep);
00099            if (ferror(filep)) {
00100               delwin(nwin);
00101               returnWin(0);
00102            }
00103        }
00104        touchwin(nwin);
00105     }
00106     returnWin(nwin);
00107 }
00108 
00109 NCURSES_EXPORT(int)
00110 putwin(WINDOW *win, FILE * filep)
00111 {
00112     int code = ERR;
00113     int n;
00114 
00115     T((T_CALLED("putwin(%p,%p)"), win, filep));
00116 
00117     if (win != 0) {
00118        size_t len = (win->_maxx + 1);
00119 
00120        clearerr(filep);
00121        if (fwrite(win, sizeof(WINDOW), 1, filep) != 1
00122            || ferror(filep))
00123              returnCode(code);
00124 
00125        for (n = 0; n <= win->_maxy; n++) {
00126            if (fwrite(win->_line[n].text,
00127                      sizeof(NCURSES_CH_T), len, filep) != len
00128               || ferror(filep)) {
00129               returnCode(code);
00130            }
00131        }
00132        code = OK;
00133     }
00134     returnCode(code);
00135 }
00136 
00137 NCURSES_EXPORT(int)
00138 scr_restore(const char *file)
00139 {
00140     FILE *fp = 0;
00141 
00142     T((T_CALLED("scr_restore(%s)"), _nc_visbuf(file)));
00143 
00144     if (_nc_access(file, R_OK) < 0
00145        || (fp = fopen(file, "rb")) == 0) {
00146        returnCode(ERR);
00147     } else {
00148        delwin(newscr);
00149        SP->_newscr = newscr = getwin(fp);
00150        (void) fclose(fp);
00151        returnCode(OK);
00152     }
00153 }
00154 
00155 NCURSES_EXPORT(int)
00156 scr_dump(const char *file)
00157 {
00158     FILE *fp = 0;
00159 
00160     T((T_CALLED("scr_dump(%s)"), _nc_visbuf(file)));
00161 
00162     if (_nc_access(file, W_OK) < 0
00163        || (fp = fopen(file, "wb")) == 0) {
00164        returnCode(ERR);
00165     } else {
00166        (void) putwin(newscr, fp);
00167        (void) fclose(fp);
00168        returnCode(OK);
00169     }
00170 }
00171 
00172 NCURSES_EXPORT(int)
00173 scr_init(const char *file)
00174 {
00175     FILE *fp = 0;
00176 
00177     T((T_CALLED("scr_init(%s)"), _nc_visbuf(file)));
00178 
00179     if (exit_ca_mode && non_rev_rmcup)
00180        returnCode(ERR);
00181 
00182     if (_nc_access(file, R_OK) < 0
00183        || (fp = fopen(file, "rb")) == 0) {
00184        returnCode(ERR);
00185     } else {
00186        delwin(curscr);
00187        SP->_curscr = curscr = getwin(fp);
00188        (void) fclose(fp);
00189        returnCode(OK);
00190     }
00191 }
00192 
00193 NCURSES_EXPORT(int)
00194 scr_set(const char *file)
00195 {
00196     T((T_CALLED("scr_set(%s)"), _nc_visbuf(file)));
00197 
00198     if (scr_init(file) == ERR) {
00199        returnCode(ERR);
00200     } else {
00201        delwin(newscr);
00202        SP->_newscr = newscr = dupwin(curscr);
00203        returnCode(OK);
00204     }
00205 }