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