Path: blob/master/Utilities/cmpdcurses/pdcurses/scr_dump.c
3153 views
/* PDCurses */12#include <curspriv.h>34/*man-start**************************************************************56scr_dump7--------89### Synopsis1011int putwin(WINDOW *win, FILE *filep);12WINDOW *getwin(FILE *filep);13int scr_dump(const char *filename);14int scr_init(const char *filename);15int scr_restore(const char *filename);16int scr_set(const char *filename);1718### Description1920getwin() reads window-related data previously stored in a file by21putwin(). It then creates and initialises a new window using that22data.2324putwin() writes all data associated with a window into a file, using25an unspecified format. This information can be retrieved later using26getwin().2728scr_dump() writes the current contents of the virtual screen to the29file named by filename in an unspecified format.3031scr_restore() function sets the virtual screen to the contents of the32file named by filename, which must have been written using33scr_dump(). The next refresh operation restores the screen to the way34it looked in the dump file.3536In PDCurses, scr_init() does nothing, and scr_set() is a synonym for37scr_restore(). Also, scr_dump() and scr_restore() save and load from38curscr. This differs from some other implementations, where39scr_init() works with curscr, and scr_restore() works with newscr;40but the effect should be the same. (PDCurses has no newscr.)4142### Return Value4344On successful completion, getwin() returns a pointer to the window it45created. Otherwise, it returns a null pointer. Other functions return46OK or ERR.4748### Portability49X/Open ncurses NetBSD50putwin Y Y Y51getwin Y Y Y52scr_dump Y Y -53scr_init Y Y -54scr_restore Y Y -55scr_set Y Y -5657**man-end****************************************************************/5859#include <stdlib.h>60#include <string.h>6162#define DUMPVER 1 /* Should be updated whenever the WINDOW struct is63changed */6465int putwin(WINDOW *win, FILE *filep)66{67static const char *marker = "PDC";68static const unsigned char version = DUMPVER;6970PDC_LOG(("putwin() - called\n"));7172/* write the marker and the WINDOW struct */7374if (filep && fwrite(marker, strlen(marker), 1, filep)75&& fwrite(&version, 1, 1, filep)76&& fwrite(win, sizeof(WINDOW), 1, filep))77{78int i;7980/* write each line */8182for (i = 0; i < win->_maxy && win->_y[i]; i++)83if (!fwrite(win->_y[i], win->_maxx * sizeof(chtype), 1, filep))84return ERR;8586return OK;87}8889return ERR;90}9192WINDOW *getwin(FILE *filep)93{94WINDOW *win;95char marker[4];96int i, nlines, ncols;9798PDC_LOG(("getwin() - called\n"));99100win = malloc(sizeof(WINDOW));101if (!win)102return (WINDOW *)NULL;103104/* check for the marker, and load the WINDOW struct */105106if (!filep || !fread(marker, 4, 1, filep) || strncmp(marker, "PDC", 3)107|| marker[3] != DUMPVER || !fread(win, sizeof(WINDOW), 1, filep))108{109free(win);110return (WINDOW *)NULL;111}112113nlines = win->_maxy;114ncols = win->_maxx;115116/* allocate the line pointer array */117118win->_y = malloc(nlines * sizeof(chtype *));119if (!win->_y)120{121free(win);122return (WINDOW *)NULL;123}124125/* allocate the minchng and maxchng arrays */126127win->_firstch = malloc(nlines * sizeof(int));128if (!win->_firstch)129{130free(win->_y);131free(win);132return (WINDOW *)NULL;133}134135win->_lastch = malloc(nlines * sizeof(int));136if (!win->_lastch)137{138free(win->_firstch);139free(win->_y);140free(win);141return (WINDOW *)NULL;142}143144/* allocate the lines */145146win = PDC_makelines(win);147if (!win)148return (WINDOW *)NULL;149150/* read them */151152for (i = 0; i < nlines; i++)153{154if (!fread(win->_y[i], ncols * sizeof(chtype), 1, filep))155{156delwin(win);157return (WINDOW *)NULL;158}159}160161touchwin(win);162163return win;164}165166int scr_dump(const char *filename)167{168FILE *filep;169170PDC_LOG(("scr_dump() - called: filename %s\n", filename));171172if (filename && (filep = fopen(filename, "wb")) != NULL)173{174int result = putwin(curscr, filep);175fclose(filep);176return result;177}178179return ERR;180}181182int scr_init(const char *filename)183{184PDC_LOG(("scr_init() - called: filename %s\n", filename));185186return OK;187}188189int scr_restore(const char *filename)190{191FILE *filep;192193PDC_LOG(("scr_restore() - called: filename %s\n", filename));194195if (filename && (filep = fopen(filename, "rb")) != NULL)196{197WINDOW *replacement = getwin(filep);198fclose(filep);199200if (replacement)201{202int result = overwrite(replacement, curscr);203delwin(replacement);204return result;205}206}207208return ERR;209}210211int scr_set(const char *filename)212{213PDC_LOG(("scr_set() - called: filename %s\n", filename));214215return scr_restore(filename);216}217218219