1
/*===========================================================================
2
* Filename : scmport-basechar.c
3
* About : ScmBaseCharPort implementation
5
* Copyright (C) 2005-2006 YAMAMOTO Kengo <yamaken AT bp.iij4u.or.jp>
6
* Copyright (c) 2007-2008 SigScheme Project <uim-en AT googlegroups.com>
10
* Redistribution and use in source and binary forms, with or without
11
* modification, are permitted provided that the following conditions
14
* 1. Redistributions of source code must retain the above copyright
15
* notice, this list of conditions and the following disclaimer.
16
* 2. Redistributions in binary form must reproduce the above copyright
17
* notice, this list of conditions and the following disclaimer in the
18
* documentation and/or other materials provided with the distribution.
19
* 3. Neither the name of authors nor the names of its contributors
20
* may be used to endorse or promote products derived from this software
21
* without specific prior written permission.
23
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
24
* IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
27
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
===========================================================================*/
37
* - This file is intended to be portable. Don't depend on SigScheme.
38
* - To isolate and hide implementation-dependent things, don't merge this file
49
#include "scmport-config.h"
52
/*=======================================
53
File Local Macro Definitions
54
=======================================*/
56
/*=======================================
57
File Local Type Definitions
58
=======================================*/
60
/*=======================================
61
File Local Function Declarations
62
=======================================*/
63
static ScmCharPort *basecport_dyn_cast(ScmCharPort *cport,
64
const ScmCharPortVTbl *dst_vptr);
65
static void basecport_close(ScmBaseCharPort *port);
66
static ScmCharCodec *basecport_codec(ScmBaseCharPort *port);
67
static char *basecport_inspect(ScmBaseCharPort *port);
68
static scm_ichar_t basecport_get_char(ScmBaseCharPort *port);
69
static scm_ichar_t basecport_peek_char(ScmBaseCharPort *port);
70
static scm_bool basecport_char_readyp(ScmBaseCharPort *port);
71
static void basecport_puts(ScmBaseCharPort *port, const char *str);
72
static void basecport_put_char(ScmBaseCharPort *port, scm_ichar_t ch);
73
static void basecport_flush(ScmBaseCharPort *port);
75
/*=======================================
77
=======================================*/
78
static const ScmCharPortVTbl ScmBaseCharPort_vtbl = {
79
(ScmCharPortMethod_dyn_cast) &basecport_dyn_cast,
80
(ScmCharPortMethod_close) &basecport_close,
81
(ScmCharPortMethod_codec) &basecport_codec,
82
(ScmCharPortMethod_inspect) &basecport_inspect,
83
(ScmCharPortMethod_get_char) &basecport_get_char,
84
(ScmCharPortMethod_peek_char) &basecport_peek_char,
85
(ScmCharPortMethod_char_readyp)&basecport_char_readyp,
86
(ScmCharPortMethod_puts) &basecport_puts,
87
(ScmCharPortMethod_put_char) &basecport_put_char,
88
(ScmCharPortMethod_flush) &basecport_flush
90
SCM_EXPORT const ScmCharPortVTbl *const ScmBaseCharPort_vptr
91
= &ScmBaseCharPort_vtbl;
93
/*=======================================
95
=======================================*/
97
ScmBaseCharPort_construct(ScmBaseCharPort *port, const ScmCharPortVTbl *vptr,
110
ScmBaseCharPort_inspect(ScmBaseCharPort *port, const char *header)
112
const char *encoding;
113
char *bport_part, *combined;
116
encoding = SCM_CHARPORT_ENCODING((ScmCharPort *)port);
117
bport_part = SCM_BYTEPORT_INSPECT((ScmBytePort *)port->bport);
118
size = strlen(header) + strlen(encoding) + strlen(bport_part)
120
combined = SCM_PORT_MALLOC(size);
121
sprintf(combined, "%s %s %s", header, encoding, bport_part);
128
ScmBaseCharPort_line_number(ScmBaseCharPort *port)
130
return port->linenum;
134
basecport_dyn_cast(ScmCharPort *cport, const ScmCharPortVTbl *dst_vptr)
136
return (dst_vptr == ScmBaseCharPort_vptr) ? cport : NULL;
140
basecport_close(ScmBaseCharPort *port)
142
SCM_BYTEPORT_CLOSE(port->bport);
146
static ScmCharCodec *
147
basecport_codec(ScmBaseCharPort *port)
149
SCM_PORT_ERROR_INVALID_OPERATION(CHAR, port, ScmBaseCharPort);
154
basecport_inspect(ScmBaseCharPort *port)
156
return ScmBaseCharPort_inspect(port, "unknown");
160
basecport_get_char(ScmBaseCharPort *port)
164
ch = SCM_BYTEPORT_GET_BYTE(port->bport);
166
if (ch == SCM_NEWLINE_STR[0])
174
basecport_peek_char(ScmBaseCharPort *port)
176
return SCM_BYTEPORT_PEEK_BYTE(port->bport);
180
basecport_char_readyp(ScmBaseCharPort *port)
182
return SCM_BYTEPORT_BYTE_READYP(port->bport);
186
basecport_puts(ScmBaseCharPort *port, const char *str)
188
SCM_BYTEPORT_PUTS(port->bport, str);
192
basecport_put_char(ScmBaseCharPort *port, scm_ichar_t ch)
194
SCM_PORT_ERROR_INVALID_OPERATION(CHAR, port, ScmBaseCharPort);
199
basecport_flush(ScmBaseCharPort *port)
201
SCM_BYTEPORT_FLUSH(port->bport);