Debian Codegraph @

terps/tads/glk/osbuffer.c

raw symbols SCIP
1 /******************************************************************************
2 * *
3 * Copyright (C) 2010-2011 by Ben Cressey. *
4 * *
5 * This file is part of Gargoyle. *
6 * *
7 * Gargoyle is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * Gargoyle is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with Gargoyle; if not, write to the Free Software *
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
20 * *
21 *****************************************************************************/
22
23 /* osbuffer.c - handle text i/o */
24
25 #include "os.h"
26 #include "glk.h"
27
28 extern winid_t mainwin;
29
30 #ifndef GLK_UNICODE
31
32 void os_put_buffer (unsigned char *buf, size_t len)
33 {
34 glk_put_buffer(buf, len);
35 }
36
37 void os_get_buffer (unsigned char *buf, size_t len, size_t init)
38 {
39 glk_request_line_event(mainwin, buf, len - 1, init);
40 }
41
42 unsigned char *os_fill_buffer (unsigned char *buf, size_t len)
43 {
44 buf[len] = '\0';
45 return buf;
46 }
47
48 #else
49
50 static glui32 *input = 0;
51 static glui32 max = 0;
52
53 extern glui32 os_parse_chars(unsigned char *buf, glui32 buflen,
54 glui32 *out, glui32 outlen);
55
56 extern glui32 os_prepare_chars (glui32 *buf, glui32 buflen,
57 unsigned char *out, glui32 outlen);
58
59 void os_put_buffer (unsigned char *buf, size_t len)
60 {
61 glui32 *out;
62 glui32 outlen;
63
64 if (!len)
65 return;
66
67 out = malloc(sizeof(glui32)*(len+1));
68 if (!out)
69 return;
70 outlen = len;
71
72 outlen = os_parse_chars(buf, len, out, outlen);
73
74 if (outlen)
75 glk_put_buffer_uni(out, outlen);
76 else
77 glk_put_buffer((char *)buf, len);
78
79 free(out);
80 }
81
82 void os_get_buffer (unsigned char *buf, size_t len, size_t init)
83 {
84 input = malloc(sizeof(glui32)*(len+1));
85 max = len;
86
87 if (init)
88 os_parse_chars(buf, init + 1, input, len);
89
90 glk_request_line_event_uni(mainwin, input, len - 1, init);
91 }
92
93 unsigned char *os_fill_buffer (unsigned char *buf, size_t len)
94 {
95 glui32 res = os_prepare_chars(input, len, buf, max);
96 buf[res] = '\0';
97
98 free(input);
99 input = 0;
100 max = 0;
101
102 return buf;
103 }
104
105 #endif /* GLK_UNICODE */