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