1 /*===========================================================================
2  *  Filename : scmport-basechar.c
3  *  About    : ScmBaseCharPort implementation
4  *
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>
7  *
8  *  All rights reserved.
9  *
10  *  Redistribution and use in source and binary forms, with or without
11  *  modification, are permitted provided that the following conditions
12  *  are met:
13  *
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.
22  *
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 ===========================================================================*/
35 
36 /*
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
39  *   into another
40  */
41 
42 #include <config.h>
43 
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include <string.h>
47 
48 #include "scmint.h"
49 #include "scmport-config.h"
50 #include "scmport.h"
51 
52 /*=======================================
53   File Local Macro Definitions
54 =======================================*/
55 
56 /*=======================================
57   File Local Type Definitions
58 =======================================*/
59 
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);
74 
75 /*=======================================
76   Variable Definitions
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
89 };
90 SCM_EXPORT const ScmCharPortVTbl *const ScmBaseCharPort_vptr
91     = &ScmBaseCharPort_vtbl;
92 
93 /*=======================================
94   Function Definitions
95 =======================================*/
96 SCM_EXPORT void
ScmBaseCharPort_construct(ScmBaseCharPort * port,const ScmCharPortVTbl * vptr,ScmBytePort * bport)97 ScmBaseCharPort_construct(ScmBaseCharPort *port, const ScmCharPortVTbl *vptr,
98                           ScmBytePort *bport)
99 {
100     port->vptr = vptr;
101     port->bport = bport;
102 #if SCM_DEBUG
103     port->linenum = 1;
104 #else
105     port->linenum = 0;
106 #endif
107 }
108 
109 SCM_EXPORT char *
ScmBaseCharPort_inspect(ScmBaseCharPort * port,const char * header)110 ScmBaseCharPort_inspect(ScmBaseCharPort *port, const char *header)
111 {
112     const char *encoding;
113     char *bport_part, *combined;
114     size_t size;
115 
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)
119         + sizeof("  ");
120     combined = SCM_PORT_MALLOC(size);
121     sprintf(combined, "%s %s %s", header, encoding, bport_part);
122     free(bport_part);
123 
124     return combined;
125 }
126 
127 SCM_EXPORT size_t
ScmBaseCharPort_line_number(ScmBaseCharPort * port)128 ScmBaseCharPort_line_number(ScmBaseCharPort *port)
129 {
130     return port->linenum;
131 }
132 
133 static ScmCharPort *
basecport_dyn_cast(ScmCharPort * cport,const ScmCharPortVTbl * dst_vptr)134 basecport_dyn_cast(ScmCharPort *cport, const ScmCharPortVTbl *dst_vptr)
135 {
136     return (dst_vptr == ScmBaseCharPort_vptr) ? cport : NULL;
137 }
138 
139 static void
basecport_close(ScmBaseCharPort * port)140 basecport_close(ScmBaseCharPort *port)
141 {
142     SCM_BYTEPORT_CLOSE(port->bport);
143     free(port);
144 }
145 
146 static ScmCharCodec *
basecport_codec(ScmBaseCharPort * port)147 basecport_codec(ScmBaseCharPort *port)
148 {
149     SCM_PORT_ERROR_INVALID_OPERATION(CHAR, port, ScmBaseCharPort);
150     /* NOTREACHED */
151 }
152 
153 static char *
basecport_inspect(ScmBaseCharPort * port)154 basecport_inspect(ScmBaseCharPort *port)
155 {
156     return ScmBaseCharPort_inspect(port, "unknown");
157 }
158 
159 static scm_ichar_t
basecport_get_char(ScmBaseCharPort * port)160 basecport_get_char(ScmBaseCharPort *port)
161 {
162     scm_ichar_t ch;
163 
164     ch = SCM_BYTEPORT_GET_BYTE(port->bport);
165 #if SCM_DEBUG
166     if (ch == SCM_NEWLINE_STR[0])
167         port->linenum++;
168 #endif
169 
170     return ch;
171 }
172 
173 static scm_ichar_t
basecport_peek_char(ScmBaseCharPort * port)174 basecport_peek_char(ScmBaseCharPort *port)
175 {
176     return SCM_BYTEPORT_PEEK_BYTE(port->bport);
177 }
178 
179 static scm_bool
basecport_char_readyp(ScmBaseCharPort * port)180 basecport_char_readyp(ScmBaseCharPort *port)
181 {
182     return SCM_BYTEPORT_BYTE_READYP(port->bport);
183 }
184 
185 static void
basecport_puts(ScmBaseCharPort * port,const char * str)186 basecport_puts(ScmBaseCharPort *port, const char *str)
187 {
188     SCM_BYTEPORT_PUTS(port->bport, str);
189 }
190 
191 static void
basecport_put_char(ScmBaseCharPort * port,scm_ichar_t ch)192 basecport_put_char(ScmBaseCharPort *port, scm_ichar_t ch)
193 {
194     SCM_PORT_ERROR_INVALID_OPERATION(CHAR, port, ScmBaseCharPort);
195     /* NOTREACHED */
196 }
197 
198 static void
basecport_flush(ScmBaseCharPort * port)199 basecport_flush(ScmBaseCharPort *port)
200 {
201     SCM_BYTEPORT_FLUSH(port->bport);
202 }
203