| 1 |
|
| 2 |
|
| 3 |
#include <curspriv.h> |
| 4 |
|
| 5 |
RCSID("$Id: scr_dump.c,v 1.30 2008/07/13 16:08:18 wmcbrine Exp $") |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
#include <stdlib.h> |
| 59 |
#include <string.h> |
| 60 |
|
| 61 |
#define DUMPVER 1 |
| 62 |
|
| 63 |
|
| 64 |
int 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 |
|
| 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 |
|
| 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 |
|
| 91 |
WINDOW *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 |
|
| 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 |
|
| 115 |
|
| 116 |
if ( !(win->_y = malloc(nlines * sizeof(chtype *))) ) |
| 117 |
{ |
| 118 |
free(win); |
| 119 |
return (WINDOW *)NULL; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 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 |
|
| 140 |
|
| 141 |
if ( !(win = PDC_makelines(win)) ) |
| 142 |
return (WINDOW *)NULL; |
| 143 |
|
| 144 |
|
| 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 |
|
| 160 |
int 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 |
|
| 176 |
int scr_init(const char *filename) |
| 177 |
{ |
| 178 |
PDC_LOG(("scr_init() - called: filename %s\n", filename)); |
| 179 |
|
| 180 |
return OK; |
| 181 |
} |
| 182 |
|
| 183 |
int 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 |
|
| 205 |
int scr_set(const char *filename) |
| 206 |
{ |
| 207 |
PDC_LOG(("scr_set() - called: filename %s\n", filename)); |
| 208 |
|
| 209 |
return scr_restore(filename); |
| 210 |
} |