1 /* Public Domain Curses */
5 RCSID("$Id: scr_dump.c,v 1.30 2008/07/13 16:08:18 wmcbrine Exp $")
7 /*man-start**************************************************************
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);
20 getwin() reads window-related data previously stored in a file
21 by putwin(). It then creates and initialises a new window using
24 putwin() writes all data associated with a window into a file,
25 using an unspecified format. This information can be retrieved
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
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
56 **man-end****************************************************************/
61 #define DUMPVER 1 /* Should be updated whenever the WINDOW struct is
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))
81 for (i = 0; i < win->_maxy && win->_y[i]; i++)
82 if (!fwrite(win->_y[i], win->_maxx * sizeof(chtype), 1, filep))
91 WINDOW *getwin(FILE *filep)
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))
108 return (WINDOW *)NULL;
114 /* allocate the line pointer array */
116 if ( !(win->_y = malloc(nlines * sizeof(chtype *))) )
119 return (WINDOW *)NULL;
122 /* allocate the minchng and maxchng arrays */
124 if ( !(win->_firstch = malloc(nlines * sizeof(int))) )
128 return (WINDOW *)NULL;
131 if ( !(win->_lastch = malloc(nlines * sizeof(int))) )
136 return (WINDOW *)NULL;
139 /* allocate the lines */
141 if ( !(win = PDC_makelines(win)) )
142 return (WINDOW *)NULL;
146 for (i = 0; i < nlines; i++)
148 if (!fread(win->_y[i], ncols * sizeof(chtype), 1, filep))
151 return (WINDOW *)NULL;
160 int scr_dump(const char *filename)
164 PDC_LOG(("scr_dump() - called: filename %s\n", filename));
166 if (filename && (filep = fopen(filename, "wb")) != NULL)
168 int result = putwin(curscr, filep);
176 int scr_init(const char *filename)
178 PDC_LOG(("scr_init() - called: filename %s\n", filename));
183 int scr_restore(const char *filename)
187 PDC_LOG(("scr_restore() - called: filename %s\n", filename));
189 if (filename && (filep = fopen(filename, "rb")) != NULL)
191 WINDOW *replacement = getwin(filep);
196 int result = overwrite(replacement, curscr);
205 int scr_set(const char *filename)
207 PDC_LOG(("scr_set() - called: filename %s\n", filename));
209 return scr_restore(filename);