blob: 6727ba36a205310491e74e6e205aae0fb121dc09 [file] [log] [blame]
Patrick Georgi3b77b722011-07-07 15:41:53 +02001/* Public Domain Curses */
2
3#include <curspriv.h>
4
5RCSID("$Id: scr_dump.c,v 1.30 2008/07/13 16:08:18 wmcbrine Exp $")
6
7/*man-start**************************************************************
8
9 Name: scr_dump
10
11 Synopsis:
12 int putwin(WINDOW *win, FILE *filep);
13 WINDOW *getwin(FILE *filep);
14 int scr_dump(const char *filename);
15 int scr_init(const char *filename);
16 int scr_restore(const char *filename);
17 int scr_set(const char *filename);
18
19 Description:
Stefan Reinauere11835e2011-10-31 12:54:00 -070020 getwin() reads window-related data previously stored in a file
21 by putwin(). It then creates and initialises a new window using
Patrick Georgi3b77b722011-07-07 15:41:53 +020022 that data.
23
Stefan Reinauere11835e2011-10-31 12:54:00 -070024 putwin() writes all data associated with a window into a file,
25 using an unspecified format. This information can be retrieved
Patrick Georgi3b77b722011-07-07 15:41:53 +020026 later using getwin().
27
Stefan Reinauere11835e2011-10-31 12:54:00 -070028 scr_dump() writes the current contents of the virtual screen to
Patrick Georgi3b77b722011-07-07 15:41:53 +020029 the file named by filename in an unspecified format.
30
Stefan Reinauere11835e2011-10-31 12:54:00 -070031 scr_restore() function sets the virtual screen to the contents
32 of the file named by filename, which must have been written
33 using scr_dump(). The next refresh operation restores the screen
Patrick Georgi3b77b722011-07-07 15:41:53 +020034 to the way it looked in the dump file.
35
Stefan Reinauere11835e2011-10-31 12:54:00 -070036 In PDCurses, scr_init() does nothing, and scr_set() is a synonym
37 for scr_restore(). Also, scr_dump() and scr_restore() save and
38 load from curscr. This differs from some other implementations,
39 where scr_init() works with curscr, and scr_restore() works with
40 newscr; but the effect should be the same. (PDCurses has no
Patrick Georgi3b77b722011-07-07 15:41:53 +020041 newscr.)
42
43 Return Value:
Stefan Reinauere11835e2011-10-31 12:54:00 -070044 On successful completion, getwin() returns a pointer to the
45 window it created. Otherwise, it returns a null pointer. Other
Patrick Georgi3b77b722011-07-07 15:41:53 +020046 functions return OK or ERR.
47
48 Portability X/Open BSD SYS V
49 putwin Y
50 getwin Y
51 scr_dump Y
52 scr_init Y
53 scr_restore Y
54 scr_set Y
55
56**man-end****************************************************************/
57
58#include <stdlib.h>
59#include <string.h>
60
61#define DUMPVER 1 /* Should be updated whenever the WINDOW struct is
62 changed */
63
64int putwin(WINDOW *win, FILE *filep)
65{
66 static const char *marker = "PDC";
67 static const unsigned char version = DUMPVER;
68
69 PDC_LOG(("putwin() - called\n"));
70
71 /* write the marker and the WINDOW struct */
72
73 if (filep && fwrite(marker, strlen(marker), 1, filep)
74 && fwrite(&version, 1, 1, filep)
75 && fwrite(win, sizeof(WINDOW), 1, filep))
76 {
77 int i;
78
79 /* write each line */
80
81 for (i = 0; i < win->_maxy && win->_y[i]; i++)
82 if (!fwrite(win->_y[i], win->_maxx * sizeof(chtype), 1, filep))
83 return ERR;
84
85 return OK;
86 }
87
88 return ERR;
89}
90
91WINDOW *getwin(FILE *filep)
92{
93 WINDOW *win;
94 char marker[4];
95 int i, nlines, ncols;
96
97 PDC_LOG(("getwin() - called\n"));
98
99 if ( !(win = malloc(sizeof(WINDOW))) )
100 return (WINDOW *)NULL;
101
102 /* check for the marker, and load the WINDOW struct */
103
104 if (!filep || !fread(marker, 4, 1, filep) || strncmp(marker, "PDC", 3)
105 || marker[3] != DUMPVER || !fread(win, sizeof(WINDOW), 1, filep))
106 {
107 free(win);
108 return (WINDOW *)NULL;
109 }
110
111 nlines = win->_maxy;
112 ncols = win->_maxx;
113
114 /* allocate the line pointer array */
115
116 if ( !(win->_y = malloc(nlines * sizeof(chtype *))) )
117 {
118 free(win);
119 return (WINDOW *)NULL;
120 }
121
122 /* allocate the minchng and maxchng arrays */
123
124 if ( !(win->_firstch = malloc(nlines * sizeof(int))) )
125 {
126 free(win->_y);
127 free(win);
128 return (WINDOW *)NULL;
129 }
130
131 if ( !(win->_lastch = malloc(nlines * sizeof(int))) )
132 {
133 free(win->_firstch);
134 free(win->_y);
135 free(win);
136 return (WINDOW *)NULL;
137 }
138
139 /* allocate the lines */
140
141 if ( !(win = PDC_makelines(win)) )
142 return (WINDOW *)NULL;
143
144 /* read them */
145
146 for (i = 0; i < nlines; i++)
147 {
148 if (!fread(win->_y[i], ncols * sizeof(chtype), 1, filep))
149 {
150 delwin(win);
151 return (WINDOW *)NULL;
152 }
153 }
154
155 touchwin(win);
156
157 return win;
158}
159
160int scr_dump(const char *filename)
161{
162 FILE *filep;
163
164 PDC_LOG(("scr_dump() - called: filename %s\n", filename));
165
166 if (filename && (filep = fopen(filename, "wb")) != NULL)
167 {
168 int result = putwin(curscr, filep);
169 fclose(filep);
170 return result;
171 }
172
173 return ERR;
174}
175
176int scr_init(const char *filename)
177{
178 PDC_LOG(("scr_init() - called: filename %s\n", filename));
179
180 return OK;
181}
182
183int scr_restore(const char *filename)
184{
185 FILE *filep;
186
187 PDC_LOG(("scr_restore() - called: filename %s\n", filename));
188
189 if (filename && (filep = fopen(filename, "rb")) != NULL)
190 {
191 WINDOW *replacement = getwin(filep);
192 fclose(filep);
193
194 if (replacement)
195 {
196 int result = overwrite(replacement, curscr);
197 delwin(replacement);
198 return result;
199 }
200 }
201
202 return ERR;
203}
204
205int scr_set(const char *filename)
206{
207 PDC_LOG(("scr_set() - called: filename %s\n", filename));
208
209 return scr_restore(filename);
210}