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