[uim-commit] r1875 - branches/r5rs/sigscheme
yamaken at freedesktop.org
yamaken at freedesktop.org
Sun Oct 23 05:24:33 PDT 2005
Author: yamaken
Date: 2005-10-23 05:24:27 -0700 (Sun, 23 Oct 2005)
New Revision: 1875
Added:
branches/r5rs/sigscheme/baseport.h
branches/r5rs/sigscheme/fileport.c
branches/r5rs/sigscheme/fileport.h
Log:
* This commit starts the abstract port implementation
* sigscheme/baseport.h
- New file
- (SCM_CHARPORT_ERROR, SCM_BYTEPORT_ERROR, SCM_PORT_DYNAMIC_CAST,
SCM_CHARPORT_CLOSE, SCM_CHARPORT_ENCODING, SCM_CHARPORT_GET_CHAR,
SCM_CHARPORT_PEEK_CHAR, SCM_CHARPORT_CHAR_READYP,
SCM_CHARPORT_VPRINTF, SCM_CHARPORT_PUT_CHAR, SCM_CHARPORT_FLUSH,
SCM_BYTEPORT_CLOSE, SCM_BYTEPORT_GET_BYTE, SCM_BYTEPORT_PEEK_BYTE,
SCM_BYTEPORT_BYTE_READYP, SCM_BYTEPORT_VPRINTF, SCM_BYTEPORT_PUTS,
SCM_BYTEPORT_WRITE, SCM_BYTEPORT_FLUSH): New macro
- (struct ScmCharPortVTbl_, ScmCharPortVTbl, struct ScmCharPort_,
ScmCharPort, struct ScmBaseCharPort_, ScmBaseCharPort, struct
ScmBytePortVTbl_, ScmBytePortVTbl, struct ScmBytePort_,
ScmBytePort): New type
* sigscheme/fileport.h
- New file
- (ScmFilePort_vtbl): New variable decl
- (ScmFilePort_new): New function decl
* sigscheme/fileport.c
- New file
- (struct ScmFilePort_, ScmFilePort): New type
- (ScmFilePort_vtbl): New variable
- (ScmFilePort_new): New function
- (fileport_dyn_cast, fileport_close, fileport_get_byte,
fileport_peek_byte, fileport_byte_readyp, fileport_vprintf,
fileport_puts, fileport_write, fileport_flush): New static function
Added: branches/r5rs/sigscheme/baseport.h
===================================================================
--- branches/r5rs/sigscheme/baseport.h 2005-10-23 06:04:50 UTC (rev 1874)
+++ branches/r5rs/sigscheme/baseport.h 2005-10-23 12:24:27 UTC (rev 1875)
@@ -0,0 +1,154 @@
+/*===========================================================================
+ * FileName : baseport.h
+ * About : Abstract base of port implementation
+ *
+ * Copyright (C) 2005 by YamaKen (yamaken AT bp.iij4u.or.jp)
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of authors nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
+ * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+===========================================================================*/
+
+/*
+ * This file is intended to be portable. Don't depend on SigScheme and don't
+ * merge into another file.
+ */
+
+#ifndef __SCM_BASEPORT_H
+#define __SCM_BASEPORT_H
+
+/*=======================================
+ System Include
+=======================================*/
+#include <stdlib.h>
+#include <stdarg.h>
+
+/*=======================================
+ Local Include
+=======================================*/
+
+/*=======================================
+ Macro Definitions
+=======================================*/
+/* define appropriate error handling such as exception to override these */
+#ifndef SCM_CHARPORT_ERROR
+#define SCM_CHARPORT_ERROR(cport, msg) (exit(EXIT_FAILURE))
+#endif /* SCM_CHARPORT_ERROR */
+#ifndef SCM_BYTEPORT_ERROR
+#define SCM_BYTEPORT_ERROR(bport, msg) (exit(EXIT_FAILURE))
+#endif /* SCM_BYTEPORT_ERROR */
+
+#define SCM_PORT_DYNAMIC_CAST(type, obj) \
+ ((type *)(*obj->vptr->dyn_cast)(obj, &type##_vtbl))
+
+#define SCM_CHARPORT_CLOSE(cport) ((*cport->vptr->close)(cport))
+#define SCM_CHARPORT_ENCODING(cport) ((*cport->vptr->encoding)(cport))
+#define SCM_CHARPORT_GET_CHAR(cport) ((*cport->vptr->get_char)(cport))
+#define SCM_CHARPORT_PEEK_CHAR(cport) ((*cport->vptr->peek_char)(cport))
+#define SCM_CHARPORT_CHAR_READYP(cport) ((*cport->vptr->char_readyp)(cport))
+#define SCM_CHARPORT_VPRINTF(cport, str, args) \
+ ((*cport->vptr->vprintf)(cport, str, args))
+#define SCM_CHARPORT_PUT_CHAR(cport, ch) ((*cport->vptr->put_char)(cport, ch))
+#define SCM_CHARPORT_FLUSH(cport) ((*cport->vptr->flush)(cport))
+
+#define SCM_BYTEPORT_CLOSE(bport) ((*bport->vptr->close)(bport))
+#define SCM_BYTEPORT_GET_BYTE(bport) ((*bport->vptr->get_byte)(bport))
+#define SCM_BYTEPORT_PEEK_BYTE(bport) ((*bport->vptr->peek_byte)(bport))
+#define SCM_BYTEPORT_BYTE_READYP(bport) ((*bport->vptr->byte_readyp)(bport))
+#define SCM_BYTEPORT_VPRINTF(bport, str, args) \
+ ((*bport->vptr->vprintf)(bport, str, args))
+#define SCM_BYTEPORT_PUTS(bport, str) ((*bport->vptr->puts)(bport, str))
+#define SCM_BYTEPORT_WRITE(bport, nbytes, buf) \
+ ((*bport->vptr->write)(bport, nbytes, buf))
+#define SCM_BYTEPORT_FLUSH(bport) ((*bport->vptr->flush)(bport))
+
+/*=======================================
+ Type Definitions
+=======================================*/
+typedef struct ScmCharPortVTbl_ ScmCharPortVTbl;
+typedef struct ScmCharPort_ ScmCharPort;
+typedef struct ScmBaseCharPort_ ScmBaseCharPort;
+typedef struct ScmBytePortVTbl_ ScmBytePortVTbl;
+typedef struct ScmBytePort_ ScmBytePort;
+
+
+struct ScmCharPortVTbl_ {
+ ScmCharPort *(*dyn_cast)(ScmCharPort *cport, const ScmCharPortVTbl *dst_vptr);
+ int (*close)(ScmCharPort *cport);
+ /* returns "UTF-8", "eucJP" and so on */
+ const char *(*encoding)(ScmCharPort *cport);
+
+ /* input */
+ int (*get_char)(ScmCharPort *cport);
+ int (*peek_char)(ScmCharPort *cport);
+ int (*char_readyp)(ScmCharPort *cport);
+
+ /* output */
+ int (*vprintf)(ScmCharPort *cport, const char *str, va_list args); /* tmp */
+ int (*put_char)(ScmCharPort *cport, int ch);
+ int (*flush)(ScmCharPort *cport);
+};
+
+struct ScmCharPort_ {
+ const ScmCharPortVTbl *vptr;
+};
+
+struct ScmBaseCharPort_ {
+ const ScmCharPortVTbl *vptr;
+
+ ScmBytePort *bport;
+};
+
+struct ScmBytePortVTbl_ {
+ ScmBytePort *(*dyn_cast)(ScmBytePort *bport, const ScmBytePortVTbl *dst_vptr);
+ int (*close)(ScmBytePort *bport);
+
+ /* input */
+ int (*get_byte)(ScmBytePort *bport);
+ int (*peek_byte)(ScmBytePort *bport);
+ int (*byte_readyp)(ScmBytePort *bport);
+
+ /* output */
+ int (*vprintf)(ScmBytePort *bport, const char *str, va_list args); /* tmp */
+ int (*puts)(ScmBytePort *bport, const char *str);
+ size_t (*write)(ScmBytePort *bport, size_t nbytes, const char *buf);
+ int (*flush)(ScmBytePort *bport);
+};
+
+struct ScmBytePort_ {
+ const ScmBytePortVTbl *vptr;
+};
+
+/*=======================================
+ Variable Declarations
+=======================================*/
+
+/*=======================================
+ Function Declarations
+=======================================*/
+
+
+#endif /* __SCM_BASEPORT_H */
Added: branches/r5rs/sigscheme/fileport.c
===================================================================
--- branches/r5rs/sigscheme/fileport.c 2005-10-23 06:04:50 UTC (rev 1874)
+++ branches/r5rs/sigscheme/fileport.c 2005-10-23 12:24:27 UTC (rev 1875)
@@ -0,0 +1,209 @@
+/*===========================================================================
+ * FileName : fileport.c
+ * About : A ScmBytePort implementation for file stream of the standard C
+ * library
+ *
+ * Copyright (C) 2005 by YamaKen (yamaken AT bp.iij4u.or.jp)
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of authors nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
+ * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+===========================================================================*/
+
+/*
+ * - This file is intended to be portable. Don't depend on SigScheme.
+ * - To isolate and hide implementation-dependent things, don't merge this file
+ * into another
+ */
+
+/*=======================================
+ System Include
+=======================================*/
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+
+/*=======================================
+ Local Include
+=======================================*/
+#include "baseport.h"
+#include "fileport.h"
+
+/*=======================================
+ File Local Macro Definitions
+=======================================*/
+
+/*=======================================
+ File Local Type Definitions
+=======================================*/
+typedef struct ScmFilePort_ ScmFilePort;
+
+/* inherits ScmBytePort */
+struct ScmFilePort_ {
+ const ScmBytePortVTbl *vptr;
+
+ FILE *file;
+};
+
+/*=======================================
+ File Local Function Declarations
+=======================================*/
+static ScmBytePort *fileport_dyn_cast(ScmBytePort *bport,
+ const ScmBytePortVTbl *dest_vptr);
+static int fileport_close(ScmBytePort *bport);
+static int fileport_get_byte(ScmBytePort *bport);
+static int fileport_peek_byte(ScmBytePort *bport);
+static int fileport_byte_readyp(ScmBytePort *bport);
+static int fileport_vprintf(ScmBytePort *bport, const char *str, va_list args);
+static int fileport_puts(ScmBytePort *bport, const char *str);
+static size_t fileport_write(ScmBytePort *bport, size_t nbytes, const char *buf);
+static int fileport_flush(ScmBytePort *bport);
+
+/*=======================================
+ Variable Declarations
+=======================================*/
+const ScmBytePortVTbl ScmFilePort_vtbl = {
+ &fileport_dyn_cast,
+ &fileport_close,
+ &fileport_get_byte,
+ &fileport_peek_byte,
+ &fileport_byte_readyp,
+ &fileport_vprintf,
+ &fileport_puts,
+ &fileport_write,
+ &fileport_flush
+};
+
+/*=======================================
+ Function Implementations
+=======================================*/
+
+ScmBytePort *
+ScmFilePort_new(FILE *file)
+{
+ ScmFilePort *port;
+
+ port = malloc(sizeof(ScmFilePort));
+ if (port) {
+ port->vptr = &ScmFilePort_vtbl;
+ port->file = file;
+ }
+
+ return (ScmBytePort *)port;
+}
+
+/*
+ * To allow method invocation from subclasses, all method must call
+ * SCM_PORT_DYNAMIC_CAST() explicitly.
+ */
+static ScmBytePort *
+fileport_dyn_cast(ScmBytePort *bport, const ScmBytePortVTbl *dst_vptr)
+{
+ ScmBytePort *cast;
+
+ cast = (dst_vptr == &ScmFilePort_vtbl) ? bport : NULL;
+ if (!cast)
+ SCM_BYTEPORT_ERROR(bport, "invalid object is passed to a ScmFilePort method");
+
+ return cast;
+}
+
+static int
+fileport_close(ScmBytePort *bport)
+{
+ ScmFilePort *fport;
+ int err;
+
+ fport = SCM_PORT_DYNAMIC_CAST(ScmFilePort, bport);
+ err = fclose(fport->file);
+ free(fport);
+
+ return err;
+}
+
+static int
+fileport_get_byte(ScmBytePort *bport)
+{
+ ScmFilePort *fport;
+
+ fport = SCM_PORT_DYNAMIC_CAST(ScmFilePort, bport);
+ return getc(fport->file);
+}
+
+static int
+fileport_peek_byte(ScmBytePort *bport)
+{
+ ScmFilePort *fport;
+ int ch;
+
+ fport = SCM_PORT_DYNAMIC_CAST(ScmFilePort, bport);
+ ch = getc(fport->file);
+ return ungetc(ch, fport->file);
+}
+
+static int
+fileport_byte_readyp(ScmBytePort *bport)
+{
+ /* does not support a FILE based on a pipe, or opened by fdopen(3) */
+ /* FIXME: support stdin properly */
+ return 1;
+}
+
+static int
+fileport_vprintf(ScmBytePort *bport, const char *str, va_list args)
+{
+ ScmFilePort *fport;
+
+ fport = SCM_PORT_DYNAMIC_CAST(ScmFilePort, bport);
+ return vfprintf(fport->file, str, args);
+}
+
+static int
+fileport_puts(ScmBytePort *bport, const char *str)
+{
+ ScmFilePort *fport;
+
+ fport = SCM_PORT_DYNAMIC_CAST(ScmFilePort, bport);
+ return fputs(str, fport->file);
+}
+
+static size_t
+fileport_write(ScmBytePort *bport, size_t nbytes, const char *buf)
+{
+ ScmFilePort *fport;
+
+ fport = SCM_PORT_DYNAMIC_CAST(ScmFilePort, bport);
+ return fwrite(buf, 1, nbytes, fport->file);
+}
+
+static int
+fileport_flush(ScmBytePort *bport)
+{
+ ScmFilePort *fport;
+
+ fport = SCM_PORT_DYNAMIC_CAST(ScmFilePort, bport);
+ return fflush(fport->file);
+}
Added: branches/r5rs/sigscheme/fileport.h
===================================================================
--- branches/r5rs/sigscheme/fileport.h 2005-10-23 06:04:50 UTC (rev 1874)
+++ branches/r5rs/sigscheme/fileport.h 2005-10-23 12:24:27 UTC (rev 1875)
@@ -0,0 +1,74 @@
+/*===========================================================================
+ * FileName : fileport.h
+ * About : A ScmBytePort implementation for file stream of the standard C
+ * library
+ *
+ * Copyright (C) 2005 by YamaKen (yamaken AT bp.iij4u.or.jp)
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of authors nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
+ * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+===========================================================================*/
+
+/*
+ * This file is intended to be portable. Don't depend on SigScheme and don't
+ * merge into another file.
+ */
+
+#ifndef __SCM_FILEPORT_H
+#define __SCM_FILEPORT_H
+
+/*=======================================
+ System Include
+=======================================*/
+#include <stdio.h>
+
+/*=======================================
+ Local Include
+=======================================*/
+#include "baseport.h"
+
+/*=======================================
+ Macro Definitions
+=======================================*/
+
+/*=======================================
+ Type Definitions
+=======================================*/
+
+/*=======================================
+ Variable Declarations
+=======================================*/
+extern const ScmBytePortVTbl ScmFilePort_vtbl;
+
+/*=======================================
+ Function Declarations
+=======================================*/
+/* FIXME: add a creator takes pathname */
+ScmBytePort *ScmFilePort_new(FILE *file);
+
+
+#endif /* __SCM_FILEPORT_H */
More information about the uim-commit
mailing list