lib_screen.c revision 174994
1/****************************************************************************
2 * Copyright (c) 1998-2006,2007 Free Software Foundation, Inc.              *
3 *                                                                          *
4 * Permission is hereby granted, free of charge, to any person obtaining a  *
5 * copy of this software and associated documentation files (the            *
6 * "Software"), to deal in the Software without restriction, including      *
7 * without limitation the rights to use, copy, modify, merge, publish,      *
8 * distribute, distribute with modifications, sublicense, and/or sell       *
9 * copies of the Software, and to permit persons to whom the Software is    *
10 * furnished to do so, subject to the following conditions:                 *
11 *                                                                          *
12 * The above copyright notice and this permission notice shall be included  *
13 * in all copies or substantial portions of the Software.                   *
14 *                                                                          *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22 *                                                                          *
23 * Except as contained in this notice, the name(s) of the above copyright   *
24 * holders shall not be used in advertising or otherwise to promote the     *
25 * sale, use or other dealings in this Software without prior written       *
26 * authorization.                                                           *
27 ****************************************************************************/
28
29/****************************************************************************
30 *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31 *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32 *     and: Thomas E. Dickey 1996 on                                        *
33 ****************************************************************************/
34
35#include <curses.priv.h>
36
37MODULE_ID("$Id: lib_screen.c,v 1.30 2007/03/10 23:20:41 tom Exp $")
38
39NCURSES_EXPORT(WINDOW *)
40getwin(FILE *filep)
41{
42    WINDOW tmp, *nwin;
43    int n;
44
45    T((T_CALLED("getwin(%p)"), filep));
46
47    clearerr(filep);
48    (void) fread(&tmp, sizeof(WINDOW), 1, filep);
49    if (ferror(filep))
50	returnWin(0);
51
52    if (tmp._flags & _ISPAD) {
53	nwin = newpad(tmp._maxy + 1, tmp._maxx + 1);
54    } else {
55	nwin = newwin(tmp._maxy + 1, tmp._maxx + 1, 0, 0);
56    }
57
58    /*
59     * We deliberately do not restore the _parx, _pary, or _parent
60     * fields, because the window hierarchy within which they
61     * made sense is probably gone.
62     */
63    if (nwin != 0) {
64	nwin->_curx = tmp._curx;
65	nwin->_cury = tmp._cury;
66	nwin->_maxy = tmp._maxy;
67	nwin->_maxx = tmp._maxx;
68	nwin->_begy = tmp._begy;
69	nwin->_begx = tmp._begx;
70	nwin->_yoffset = tmp._yoffset;
71	nwin->_flags = tmp._flags & ~(_SUBWIN);
72
73	WINDOW_ATTRS(nwin) = WINDOW_ATTRS(&tmp);
74	nwin->_nc_bkgd = tmp._nc_bkgd;
75
76	nwin->_notimeout = tmp._notimeout;
77	nwin->_clear = tmp._clear;
78	nwin->_leaveok = tmp._leaveok;
79	nwin->_idlok = tmp._idlok;
80	nwin->_idcok = tmp._idcok;
81	nwin->_immed = tmp._immed;
82	nwin->_scroll = tmp._scroll;
83	nwin->_sync = tmp._sync;
84	nwin->_use_keypad = tmp._use_keypad;
85	nwin->_delay = tmp._delay;
86
87	nwin->_regtop = tmp._regtop;
88	nwin->_regbottom = tmp._regbottom;
89
90	if (tmp._flags & _ISPAD)
91	    nwin->_pad = tmp._pad;
92
93	for (n = 0; n <= nwin->_maxy; n++) {
94	    clearerr(filep);
95	    (void) fread(nwin->_line[n].text,
96			 sizeof(NCURSES_CH_T),
97			 (size_t) (nwin->_maxx + 1),
98			 filep);
99	    if (ferror(filep)) {
100		delwin(nwin);
101		returnWin(0);
102	    }
103	}
104	touchwin(nwin);
105    }
106    returnWin(nwin);
107}
108
109NCURSES_EXPORT(int)
110putwin(WINDOW *win, FILE *filep)
111{
112    int code = ERR;
113    int n;
114
115    T((T_CALLED("putwin(%p,%p)"), win, filep));
116
117    if (win != 0) {
118	size_t len = (win->_maxx + 1);
119
120	clearerr(filep);
121	if (fwrite(win, sizeof(WINDOW), 1, filep) != 1
122	    || ferror(filep))
123	      returnCode(code);
124
125	for (n = 0; n <= win->_maxy; n++) {
126	    if (fwrite(win->_line[n].text,
127		       sizeof(NCURSES_CH_T), len, filep) != len
128		|| ferror(filep)) {
129		returnCode(code);
130	    }
131	}
132	code = OK;
133    }
134    returnCode(code);
135}
136
137NCURSES_EXPORT(int)
138scr_restore(const char *file)
139{
140    FILE *fp = 0;
141
142    T((T_CALLED("scr_restore(%s)"), _nc_visbuf(file)));
143
144    if (_nc_access(file, R_OK) < 0
145	|| (fp = fopen(file, "rb")) == 0) {
146	returnCode(ERR);
147    } else {
148	delwin(newscr);
149	SP->_newscr = getwin(fp);
150#if !USE_REENTRANT
151	newscr = SP->_newscr;
152#endif
153	(void) fclose(fp);
154	returnCode(OK);
155    }
156}
157
158NCURSES_EXPORT(int)
159scr_dump(const char *file)
160{
161    FILE *fp = 0;
162
163    T((T_CALLED("scr_dump(%s)"), _nc_visbuf(file)));
164
165    if (_nc_access(file, W_OK) < 0
166	|| (fp = fopen(file, "wb")) == 0) {
167	returnCode(ERR);
168    } else {
169	(void) putwin(newscr, fp);
170	(void) fclose(fp);
171	returnCode(OK);
172    }
173}
174
175NCURSES_EXPORT(int)
176scr_init(const char *file)
177{
178    FILE *fp = 0;
179
180    T((T_CALLED("scr_init(%s)"), _nc_visbuf(file)));
181
182    if (exit_ca_mode && non_rev_rmcup)
183	returnCode(ERR);
184
185    if (_nc_access(file, R_OK) < 0
186	|| (fp = fopen(file, "rb")) == 0) {
187	returnCode(ERR);
188    } else {
189	delwin(curscr);
190	SP->_curscr = getwin(fp);
191#if !USE_REENTRANT
192	curscr = SP->_curscr;
193#endif
194	(void) fclose(fp);
195	returnCode(OK);
196    }
197}
198
199NCURSES_EXPORT(int)
200scr_set(const char *file)
201{
202    T((T_CALLED("scr_set(%s)"), _nc_visbuf(file)));
203
204    if (scr_init(file) == ERR) {
205	returnCode(ERR);
206    } else {
207	delwin(newscr);
208	SP->_newscr = dupwin(curscr);
209#if !USE_REENTRANT
210	newscr = SP->_newscr;
211#endif
212	returnCode(OK);
213    }
214}
215