~ubuntu-branches/ubuntu/hardy/sigscheme/hardy-proposed

« back to all changes in this revision

Viewing changes to src/sbcport.c

  • Committer: Bazaar Package Importer
  • Author(s): NIIBE Yutaka
  • Date: 2006-05-23 21:46:41 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060523214641-6ix4gz34wpiehub8
Tags: 0.5.0-2
* debian/control (Build-Depends): Added ruby.
  Thanks to Frederik Schueler.  Closes: #368571
* debian/rules (clean): invoke 'distclean' instead of 'clean'.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*===========================================================================
 
2
 *  FileName : sbcport.c
 
3
 *  About    : A ScmCharPort implementation for singlebyte character stream
 
4
 *
 
5
 *  Copyright (C) 2005-2006 YamaKen <yamaken AT bp.iij4u.or.jp>
 
6
 *
 
7
 *  All rights reserved.
 
8
 *
 
9
 *  Redistribution and use in source and binary forms, with or without
 
10
 *  modification, are permitted provided that the following conditions
 
11
 *  are met:
 
12
 *
 
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.
 
18
 *  3. Neither the name of authors nor the names of its contributors
 
19
 *     may be used to endorse or promote products derived from this software
 
20
 *     without specific prior written permission.
 
21
 *
 
22
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
 
23
 *  IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 
24
 *  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
25
 *  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
 
26
 *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
27
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
28
 *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 
29
 *  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 
30
 *  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 
31
 *  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 
32
 *  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
33
===========================================================================*/
 
34
 
 
35
/*
 
36
 * - This file is intended to be portable. Don't depend on SigScheme.
 
37
 * - To isolate and hide implementation-dependent things, don't merge this file
 
38
 *   into another
 
39
 */
 
40
 
 
41
#include "config.h"
 
42
 
 
43
/*=======================================
 
44
  System Include
 
45
=======================================*/
 
46
#include <stdlib.h>
 
47
#include <stdarg.h>
 
48
 
 
49
/*=======================================
 
50
  Local Include
 
51
=======================================*/
 
52
/* To override SCM_{CHAR,BYTE}PORT_ERROR() and SCM_PORT_*ALLOC(). Don't depend
 
53
 * on SigScheme-specific things */
 
54
#include "sigscheme.h"
 
55
#include "sigschemeinternal.h"
 
56
 
 
57
#include "baseport.h"
 
58
#include "sbcport.h"
 
59
 
 
60
/*=======================================
 
61
  File Local Macro Definitions
 
62
=======================================*/
 
63
 
 
64
/*=======================================
 
65
  File Local Type Definitions
 
66
=======================================*/
 
67
struct ScmSingleByteCharPort_ {  /* inherits ScmBaseCharPort */
 
68
    const ScmCharPortVTbl *vptr;
 
69
 
 
70
    ScmBytePort *bport;  /* protected */
 
71
};
 
72
 
 
73
/*=======================================
 
74
  File Local Function Declarations
 
75
=======================================*/
 
76
static ScmCharPort *sbcport_dyn_cast(ScmCharPort *cport,
 
77
                                     const ScmCharPortVTbl *dst_vptr);
 
78
static ScmCharCodec *sbcport_codec(ScmSingleByteCharPort *port);
 
79
static char *sbcport_inspect(ScmSingleByteCharPort *port);
 
80
static int sbcport_put_char(ScmSingleByteCharPort *port, scm_ichar_t ch);
 
81
 
 
82
/*=======================================
 
83
  Variable Declarations
 
84
=======================================*/
 
85
static ScmCharPortVTbl ScmSingleByteCharPort_vtbl;
 
86
const ScmCharPortVTbl *ScmSingleByteCharPort_vptr = &ScmSingleByteCharPort_vtbl;
 
87
 
 
88
static ScmCharCodec *codec;
 
89
 
 
90
/*=======================================
 
91
  Function Implementations
 
92
=======================================*/
 
93
void
 
94
scm_sbcport_init(void)
 
95
{
 
96
    ScmCharPortVTbl *vptr;
 
97
 
 
98
    ScmSingleByteCharPort_vtbl = *ScmBaseCharPort_vptr;
 
99
 
 
100
    vptr = &ScmSingleByteCharPort_vtbl;
 
101
    vptr->dyn_cast = (ScmCharPortMethod_dyn_cast)&sbcport_dyn_cast;
 
102
    vptr->codec    = (ScmCharPortMethod_codec)&sbcport_codec;
 
103
    vptr->inspect  = (ScmCharPortMethod_inspect)&sbcport_inspect;
 
104
    vptr->put_char = (ScmCharPortMethod_put_char)&sbcport_put_char;
 
105
 
 
106
    codec = scm_mb_find_codec("ISO-8859-1");
 
107
}
 
108
 
 
109
void
 
110
ScmSingleByteCharPort_construct(ScmSingleByteCharPort *port,
 
111
                                const ScmCharPortVTbl *vptr,
 
112
                                ScmBytePort *bport)
 
113
{
 
114
    ScmBaseCharPort_construct((ScmBaseCharPort *)port, vptr, bport);
 
115
}
 
116
 
 
117
ScmCharPort *
 
118
ScmSingleByteCharPort_new(ScmBytePort *bport)
 
119
{
 
120
    ScmSingleByteCharPort *cport;
 
121
 
 
122
    cport = SCM_PORT_MALLOC(sizeof(ScmSingleByteCharPort));
 
123
    ScmSingleByteCharPort_construct(cport, ScmSingleByteCharPort_vptr, bport);
 
124
 
 
125
    return (ScmCharPort *)cport;
 
126
}
 
127
 
 
128
static ScmCharPort *
 
129
sbcport_dyn_cast(ScmCharPort *cport, const ScmCharPortVTbl *dst_vptr)
 
130
{
 
131
    return (dst_vptr == ScmBaseCharPort_vptr
 
132
            || dst_vptr == ScmSingleByteCharPort_vptr) ? cport : NULL;
 
133
}
 
134
 
 
135
static ScmCharCodec *
 
136
sbcport_codec(ScmSingleByteCharPort *port)
 
137
{
 
138
    return codec;
 
139
}
 
140
 
 
141
static char *
 
142
sbcport_inspect(ScmSingleByteCharPort *port)
 
143
{
 
144
    return ScmBaseCharPort_inspect((ScmBaseCharPort *)port, "sb");
 
145
}
 
146
 
 
147
static int
 
148
sbcport_put_char(ScmSingleByteCharPort *port, scm_ichar_t ch)
 
149
{
 
150
    char buf[1];
 
151
 
 
152
    buf[0] = ch;
 
153
    return SCM_BYTEPORT_WRITE(port->bport, sizeof(char), buf);
 
154
}