1 /*===========================================================================
2  *  Filename : scmport-null.c
3  *  About    : A ScmBytePort implementation for null read/write
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 
47 #include "scmint.h"
48 #include "scmport-config.h"
49 #include "scmport.h"
50 #include "scmport-null.h"
51 
52 /*=======================================
53   File Local Macro Definitions
54 =======================================*/
55 
56 /*=======================================
57   File Local Type Definitions
58 =======================================*/
59 typedef ScmBytePort ScmNullPort;
60 
61 /*=======================================
62   File Local Function Declarations
63 =======================================*/
64 static ScmBytePort *nullport_dyn_cast(ScmBytePort *bport,
65                                       const ScmBytePortVTbl *dest_vptr);
66 static void nullport_close(ScmNullPort *bport);
67 static char *nullport_inspect(ScmNullPort *port);
68 static scm_ichar_t nullport_get_byte(ScmNullPort *bport);
69 static scm_bool nullport_byte_readyp(ScmNullPort *bport);
70 static void nullport_puts(ScmNullPort *bport, const char *str);
71 static void nullport_write(ScmNullPort *bport, size_t nbytes, const char *buf);
72 static void nullport_flush(ScmNullPort *bport);
73 
74 /*=======================================
75   Variable Definitions
76 =======================================*/
77 static const ScmBytePortVTbl ScmNullPort_vtbl = {
78     (ScmBytePortMethod_dyn_cast)   &nullport_dyn_cast,
79     (ScmBytePortMethod_close)      &nullport_close,
80     (ScmBytePortMethod_inspect)    &nullport_inspect,
81     (ScmBytePortMethod_get_byte)   &nullport_get_byte,
82     (ScmBytePortMethod_peek_byte)  &nullport_get_byte,
83     (ScmBytePortMethod_byte_readyp)&nullport_byte_readyp,
84     (ScmBytePortMethod_puts)       &nullport_puts,
85     (ScmBytePortMethod_write)      &nullport_write,
86     (ScmBytePortMethod_flush)      &nullport_flush
87 };
88 SCM_EXPORT const ScmBytePortVTbl *const ScmNullPort_vptr = &ScmNullPort_vtbl;
89 
90 /*=======================================
91   Function Definitions
92 =======================================*/
93 
94 /*
95  * Client code must call this first even if current implementation does not
96  * contain actual code.
97  */
98 SCM_EXPORT void
scm_nullport_init(void)99 scm_nullport_init(void)
100 {
101 }
102 
103 SCM_EXPORT ScmBytePort *
ScmNullPort_new(void)104 ScmNullPort_new(void)
105 {
106     ScmNullPort *port;
107 
108     port = SCM_PORT_MALLOC(sizeof(ScmNullPort));
109 
110     port->vptr = ScmNullPort_vptr;
111 
112     return (ScmBytePort *)port;
113 }
114 
115 static ScmBytePort *
nullport_dyn_cast(ScmBytePort * bport,const ScmBytePortVTbl * dst_vptr)116 nullport_dyn_cast(ScmBytePort *bport, const ScmBytePortVTbl *dst_vptr)
117 {
118     return (dst_vptr == ScmNullPort_vptr) ? bport : NULL;
119 }
120 
121 static void
nullport_close(ScmNullPort * port)122 nullport_close(ScmNullPort *port)
123 {
124     free(port);
125 }
126 
127 static char *
nullport_inspect(ScmNullPort * port)128 nullport_inspect(ScmNullPort *port)
129 {
130     return SCM_PORT_STRDUP("null");
131 }
132 
133 static scm_ichar_t
nullport_get_byte(ScmNullPort * port)134 nullport_get_byte(ScmNullPort *port)
135 {
136     return SCM_ICHAR_EOF;
137 }
138 
139 static scm_bool
nullport_byte_readyp(ScmNullPort * port)140 nullport_byte_readyp(ScmNullPort *port)
141 {
142     return scm_true;
143 }
144 
145 static void
nullport_puts(ScmNullPort * port,const char * str)146 nullport_puts(ScmNullPort *port, const char *str)
147 {
148 }
149 
150 static void
nullport_write(ScmNullPort * port,size_t nbytes,const char * buf)151 nullport_write(ScmNullPort *port, size_t nbytes, const char *buf)
152 {
153 }
154 
155 static void
nullport_flush(ScmNullPort * port)156 nullport_flush(ScmNullPort *port)
157 {
158 }
159