1 /* $NetBSD: fileio.c,v 1.4 2009/07/22 16:57:14 roy Exp $ */
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: fileio.c,v 1.4 2009/07/22 16:57:14 roy Exp $");
38 #include "curses_private.h"
44 static int __putnsp(nschar_t
*, FILE *);
45 static int __getnsp(nschar_t
*, FILE *);
46 #endif /* HAVE_WCHAR */
51 * Write non-spacing character chain to file, consisting of:
52 * ((int) 1, (wchar_t) ch) pairs followed by (int) 0.
55 __putnsp(nschar_t
*nsp
, FILE *fp
)
61 if (fwrite(&n
, sizeof(int), 1, fp
) != 1)
63 if (fwrite(&nsp
->ch
, sizeof(wchar_t), 1, fp
) != 1)
67 if (fwrite(&n
, sizeof(int), 1, fp
) != 1)
72 #endif /* HAVE_WCHAR */
76 * Write window data to file
79 putwin(WINDOW
*win
, FILE *fp
)
81 int major
= CURSES_LIB_MAJOR
;
82 int minor
= CURSES_LIB_MINOR
;
87 __CTRACE(__CTRACE_FILEIO
, "putwin: win %p\n", win
);
93 /* win can't be a subwin */
94 if (win
->orig
!= NULL
)
98 if (fwrite(&major
, sizeof(int), 1, fp
) != 1)
100 if (fwrite(&minor
, sizeof(int), 1, fp
) != 1)
103 /* Window parameters */
104 if (fwrite(win
, sizeof(WINDOW
), 1, fp
) != 1)
107 /* Background non-spacing character */
108 if (__putnsp(win
->bnsp
, fp
) == ERR
)
110 #endif /* HAVE_WCHAR */
112 /* Lines and line data */
113 for (y
= 0; y
< win
->maxy
; y
++)
114 for (sp
= win
->alines
[y
]->line
, x
= 0; x
< win
->maxx
;
116 if (fwrite(&sp
->ch
, sizeof(wchar_t), 1, fp
) != 1)
118 if (fwrite(&sp
->attr
, sizeof(attr_t
), 1, fp
) != 1)
121 if (sp
->nsp
!= NULL
) {
122 if (__putnsp(win
->bnsp
, fp
) == ERR
)
125 #endif /* HAVE_WCHAR */
134 * Read non-spacing character chain from file
137 __getnsp(nschar_t
*nsp
, FILE *fp
)
140 nschar_t
*onsp
, *tnsp
;
142 if (fread(&n
, sizeof(int), 1, fp
) != 1)
146 tnsp
= (nschar_t
*)malloc(sizeof(nschar_t
));
148 __cursesi_free_nsp(nsp
);
151 if (fread(&tnsp
->ch
, sizeof(wchar_t), 1, fp
) != 1) {
152 __cursesi_free_nsp(nsp
);
158 if (fread(&n
, sizeof(int), 1, fp
) != 1) {
159 __cursesi_free_nsp(nsp
);
165 #endif /* HAVE_WCHAR */
169 * Read window data from file
180 __CTRACE(__CTRACE_FILEIO
, "getwin\n");
183 /* Check library version */
184 if (fread(&major
, sizeof(int), 1, fp
) != 1)
186 if (fread(&minor
, sizeof(int), 1, fp
) != 1)
188 if(major
!= CURSES_LIB_MAJOR
|| minor
!= CURSES_LIB_MINOR
)
191 /* Window parameters */
192 wtmp
= (WINDOW
*)malloc(sizeof(WINDOW
));
195 if (fread(wtmp
, sizeof(WINDOW
), 1, fp
) != 1)
197 win
= __newwin(_cursesi_screen
, wtmp
->maxy
, wtmp
->maxx
,
198 wtmp
->begy
, wtmp
->begx
, FALSE
);
201 win
->cury
= wtmp
->cury
;
202 win
->curx
= wtmp
->curx
;
203 win
->reqy
= wtmp
->reqy
;
204 win
->reqx
= wtmp
->reqx
;
205 win
->flags
= wtmp
->flags
;
206 win
->delay
= wtmp
->delay
;
207 win
->wattr
= wtmp
->wattr
;
208 win
->bch
= wtmp
->bch
;
209 win
->battr
= wtmp
->battr
;
210 win
->scr_t
= wtmp
->scr_t
;
211 win
->scr_b
= wtmp
->scr_b
;
217 if (__getnsp(win
->bnsp
, fp
) == ERR
)
219 #endif /* HAVE_WCHAR */
221 /* Lines and line data */
222 for (y
= 0; y
< win
->maxy
; y
++) {
223 for (sp
= win
->alines
[y
]->line
, x
= 0; x
< win
->maxx
;
225 if (fread(&sp
->ch
, sizeof(wchar_t), 1, fp
) != 1)
227 if (fread(&sp
->attr
, sizeof(attr_t
), 1, fp
) != 1)
230 if (sp
->nsp
!= NULL
) {
231 if (__getnsp(win
->bnsp
, fp
) == ERR
)
234 #endif /* HAVE_WCHAR */
236 __touchline(win
, y
, 0, (int) win
->maxx
- 1);
239 __CTRACE(__CTRACE_FILEIO
, "getwin: win = 0x%p\n", win
);