drivers/wifi: Remove unnecessary data structure copy
[coreboot2.git] / payloads / libpayload / curses / PDCurses / pdcurses / scr_dump.c
blob6727ba36a205310491e74e6e205aae0fb121dc09
1 /* Public Domain Curses */
3 #include <curspriv.h>
5 RCSID("$Id: scr_dump.c,v 1.30 2008/07/13 16:08:18 wmcbrine Exp $")
7 /*man-start**************************************************************
9 Name: scr_dump
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);
19 Description:
20 getwin() reads window-related data previously stored in a file
21 by putwin(). It then creates and initialises a new window using
22 that data.
24 putwin() writes all data associated with a window into a file,
25 using an unspecified format. This information can be retrieved
26 later using getwin().
28 scr_dump() writes the current contents of the virtual screen to
29 the file named by filename in an unspecified format.
31 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
34 to the way it looked in the dump file.
36 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
41 newscr.)
43 Return Value:
44 On successful completion, getwin() returns a pointer to the
45 window it created. Otherwise, it returns a null pointer. Other
46 functions return OK or ERR.
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
56 **man-end****************************************************************/
58 #include <stdlib.h>
59 #include <string.h>
61 #define DUMPVER 1 /* Should be updated whenever the WINDOW struct is
62 changed */
64 int putwin(WINDOW *win, FILE *filep)
66 static const char *marker = "PDC";
67 static const unsigned char version = DUMPVER;
69 PDC_LOG(("putwin() - called\n"));
71 /* write the marker and the WINDOW struct */
73 if (filep && fwrite(marker, strlen(marker), 1, filep)
74 && fwrite(&version, 1, 1, filep)
75 && fwrite(win, sizeof(WINDOW), 1, filep))
77 int i;
79 /* write each line */
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;
85 return OK;
88 return ERR;
91 WINDOW *getwin(FILE *filep)
93 WINDOW *win;
94 char marker[4];
95 int i, nlines, ncols;
97 PDC_LOG(("getwin() - called\n"));
99 if ( !(win = malloc(sizeof(WINDOW))) )
100 return (WINDOW *)NULL;
102 /* check for the marker, and load the WINDOW struct */
104 if (!filep || !fread(marker, 4, 1, filep) || strncmp(marker, "PDC", 3)
105 || marker[3] != DUMPVER || !fread(win, sizeof(WINDOW), 1, filep))
107 free(win);
108 return (WINDOW *)NULL;
111 nlines = win->_maxy;
112 ncols = win->_maxx;
114 /* allocate the line pointer array */
116 if ( !(win->_y = malloc(nlines * sizeof(chtype *))) )
118 free(win);
119 return (WINDOW *)NULL;
122 /* allocate the minchng and maxchng arrays */
124 if ( !(win->_firstch = malloc(nlines * sizeof(int))) )
126 free(win->_y);
127 free(win);
128 return (WINDOW *)NULL;
131 if ( !(win->_lastch = malloc(nlines * sizeof(int))) )
133 free(win->_firstch);
134 free(win->_y);
135 free(win);
136 return (WINDOW *)NULL;
139 /* allocate the lines */
141 if ( !(win = PDC_makelines(win)) )
142 return (WINDOW *)NULL;
144 /* read them */
146 for (i = 0; i < nlines; i++)
148 if (!fread(win->_y[i], ncols * sizeof(chtype), 1, filep))
150 delwin(win);
151 return (WINDOW *)NULL;
155 touchwin(win);
157 return win;
160 int scr_dump(const char *filename)
162 FILE *filep;
164 PDC_LOG(("scr_dump() - called: filename %s\n", filename));
166 if (filename && (filep = fopen(filename, "wb")) != NULL)
168 int result = putwin(curscr, filep);
169 fclose(filep);
170 return result;
173 return ERR;
176 int scr_init(const char *filename)
178 PDC_LOG(("scr_init() - called: filename %s\n", filename));
180 return OK;
183 int scr_restore(const char *filename)
185 FILE *filep;
187 PDC_LOG(("scr_restore() - called: filename %s\n", filename));
189 if (filename && (filep = fopen(filename, "rb")) != NULL)
191 WINDOW *replacement = getwin(filep);
192 fclose(filep);
194 if (replacement)
196 int result = overwrite(replacement, curscr);
197 delwin(replacement);
198 return result;
202 return ERR;
205 int scr_set(const char *filename)
207 PDC_LOG(("scr_set() - called: filename %s\n", filename));
209 return scr_restore(filename);