1 /* $NetBSD: fileio.c,v 1.1 2008/04/14 20:38:45 jdc Exp $ */ 2 3 /*- 4 * Copyright (c) 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Julian Coleman. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 __RCSID("$NetBSD: fileio.c,v 1.1 2008/04/14 20:38:45 jdc Exp $"); 39 #endif /* not lint */ 40 41 #include "curses.h" 42 #include "curses_private.h" 43 #include "fileio.h" 44 #include <stdio.h> 45 #include <stdlib.h> 46 47 #ifdef HAVE_WCHAR 48 static int __putnsp(nschar_t *, FILE *); 49 static int __getnsp(nschar_t *, FILE *); 50 #endif /* HAVE_WCHAR */ 51 52 #ifdef HAVE_WCHAR 53 /* 54 * __putnsp -- 55 * Write non-spacing character chain to file, consisting of: 56 * ((int) 1, (wchar_t) ch) pairs followed by (int) 0. 57 */ 58 static int 59 __putnsp(nschar_t *nsp, FILE *fp) 60 { 61 int n; 62 63 n = 1; 64 while (nsp != NULL) { 65 if (fwrite(&n, sizeof(int), 1, fp) != 1) 66 return ERR; 67 if (fwrite(&nsp->ch, sizeof(wchar_t), 1, fp) != 1) 68 return ERR; 69 } 70 n = 0; 71 if (fwrite(&n, sizeof(int), 1, fp) != 1) 72 return ERR; 73 74 return OK; 75 } 76 #endif /* HAVE_WCHAR */ 77 78 /* 79 * putwin -- 80 * Write window data to file 81 */ 82 int 83 putwin(WINDOW *win, FILE *fp) 84 { 85 int major = CURSES_LIB_MAJOR; 86 int minor = CURSES_LIB_MINOR; 87 int y, x; 88 __LDATA *sp; 89 90 #ifdef DEBUG 91 __CTRACE(__CTRACE_FILEIO, "putwin: win %p\n", win); 92 #endif 93 94 if (win == NULL) 95 return ERR; 96 97 /* win can't be a subwin */ 98 if (win->orig != NULL) 99 return ERR; 100 101 /* Library version */ 102 if (fwrite(&major, sizeof(int), 1, fp) != 1) 103 return ERR; 104 if (fwrite(&minor, sizeof(int), 1, fp) != 1) 105 return ERR; 106 107 /* Window parameters */ 108 if (fwrite(win, sizeof(WINDOW), 1, fp) != 1) 109 return ERR; 110 #ifdef HAVE_WCHAR 111 /* Background non-spacing character */ 112 if (__putnsp(win->bnsp, fp) == ERR) 113 return ERR; 114 #endif /* HAVE_WCHAR */ 115 116 /* Lines and line data */ 117 for (y = 0; y < win->maxy; y++) 118 for (sp = win->lines[y]->line, x = 0; x < win->maxx; 119 x++, sp++) { 120 if (fwrite(&sp->ch, sizeof(wchar_t), 1, fp) != 1) 121 return ERR; 122 if (fwrite(&sp->attr, sizeof(attr_t), 1, fp) != 1) 123 return ERR; 124 #ifdef HAVE_WCHAR 125 if (sp->nsp != NULL) { 126 if (__putnsp(win->bnsp, fp) == ERR) 127 return ERR; 128 } 129 #endif /* HAVE_WCHAR */ 130 } 131 132 return OK; 133 } 134 135 #ifdef HAVE_WCHAR 136 /* 137 * __getnsp -- 138 * Read non-spacing character chain from file 139 */ 140 static int 141 __getnsp(nschar_t *nsp, FILE *fp) 142 { 143 int n; 144 nschar_t *onsp, *tnsp; 145 146 if (fread(&n, sizeof(int), 1, fp) != 1) 147 return ERR; 148 onsp = nsp; 149 while (n != 0) { 150 tnsp = (nschar_t *)malloc(sizeof(nschar_t)); 151 if (tnsp == NULL) { 152 __cursesi_free_nsp(nsp); 153 return OK; 154 } 155 if (fread(&tnsp->ch, sizeof(wchar_t), 1, fp) != 1) { 156 __cursesi_free_nsp(nsp); 157 return OK; 158 } 159 tnsp->next = NULL; 160 onsp->next = tnsp; 161 onsp = onsp->next; 162 if (fread(&n, sizeof(int), 1, fp) != 1) { 163 __cursesi_free_nsp(nsp); 164 return ERR; 165 } 166 } 167 return OK; 168 } 169 #endif /* HAVE_WCHAR */ 170 171 /* 172 * getwin -- 173 * Read window data from file 174 */ 175 WINDOW * 176 getwin(FILE *fp) 177 { 178 int major, minor; 179 WINDOW *wtmp, *win; 180 int y, x; 181 __LDATA *sp; 182 183 #ifdef DEBUG 184 __CTRACE(__CTRACE_FILEIO, "getwin\n"); 185 #endif 186 187 /* Check library version */ 188 if (fread(&major, sizeof(int), 1, fp) != 1) 189 return NULL; 190 if (fread(&minor, sizeof(int), 1, fp) != 1) 191 return NULL; 192 if(major != CURSES_LIB_MAJOR || minor != CURSES_LIB_MINOR) 193 return NULL; 194 195 /* Window parameters */ 196 wtmp = (WINDOW *)malloc(sizeof(WINDOW)); 197 if (wtmp == NULL) 198 return NULL; 199 if (fread(wtmp, sizeof(WINDOW), 1, fp) != 1) 200 goto error0; 201 win = __newwin(_cursesi_screen, wtmp->maxy, wtmp->maxx, 202 wtmp->begy, wtmp->begx, FALSE); 203 if (win == NULL) 204 goto error0; 205 win->cury = wtmp->cury; 206 win->curx = wtmp->curx; 207 win->reqy = wtmp->reqy; 208 win->reqx = wtmp->reqx; 209 win->flags = wtmp->flags; 210 win->delay = wtmp->delay; 211 win->wattr = wtmp->wattr; 212 win->bch = wtmp->bch; 213 win->battr = wtmp->battr; 214 win->scr_t = wtmp->scr_t; 215 win->scr_b = wtmp->scr_b; 216 free(wtmp); 217 __swflags(win); 218 219 #ifdef HAVE_WCHAR 220 if (__getnsp(win->bnsp, fp) == ERR) 221 goto error1; 222 #endif /* HAVE_WCHAR */ 223 224 /* Lines and line data */ 225 for (y = 0; y < win->maxy; y++) { 226 for (sp = win->lines[y]->line, x = 0; x < win->maxx; 227 x++, sp++) { 228 if (fread(&sp->ch, sizeof(wchar_t), 1, fp) != 1) 229 goto error1; 230 if (fread(&sp->attr, sizeof(attr_t), 1, fp) != 1) 231 goto error1; 232 #ifdef HAVE_WCHAR 233 if (sp->nsp != NULL) { 234 if (__getnsp(win->bnsp, fp) == ERR) 235 goto error1; 236 } 237 #endif /* HAVE_WCHAR */ 238 } 239 __touchline(win, y, 0, (int) win->maxx - 1); 240 } 241 #ifdef DEBUG 242 __CTRACE(__CTRACE_FILEIO, "getwin: win = 0x%p\n", win); 243 #endif 244 return win; 245 246 error1: 247 delwin(win); 248 error0: 249 free(wtmp); 250 return NULL; 251 } 252