1 /* codec.h                                         -*- mode:c; coding:utf-8; -*-
2  *
3  *   Copyright (c) 2010-2021  Takashi Kato <ktakashi@ymail.com>
4  *
5  *   Redistribution and use in source and binary forms, with or without
6  *   modification, are permitted provided that the following conditions
7  *   are met:
8  *
9  *   1. Redistributions of source code must retain the above copyright
10  *      notice, this list of conditions and the following disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above copyright
13  *      notice, this list of conditions and the following disclaimer in the
14  *      documentation and/or other materials provided with the distribution.
15  *
16  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22  *   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  *   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24  *   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  *   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  *  $Id: $
29  */
30 #ifndef SAGITTARIUS_PRIVATE_CODEC_H_
31 #define SAGITTARIUS_PRIVATE_CODEC_H_
32 
33 #include "sagittariusdefs.h"
34 #include "clos.h"
35 
36 typedef enum {
37   UTF_16BE,
38   UTF_16LE,
39   UTF_16CHECK_BOM,
40   UTF_32BE,
41   UTF_32LE,
42   UTF_32USE_NATIVE_ENDIAN,
43   NO_BOM,
44 } SgEndianness;
45 
46 typedef enum {
47   SG_BUILTIN_CODEC,
48   SG_CUSTOM_CODEC
49 } SgCodecType;
50 
51 struct SgCodecRec
52 {
53   SG_HEADER;
54   SgString  *name;
55   SgCodecType type;
56   union {
57     struct {
58       /* TODO read and write; */
59       int     (*putc)(SgObject, SgPort*, SgChar, SgErrorHandlingMode);
60       SgChar  (*getc)(SgObject, SgPort*, SgErrorHandlingMode, int);
61       int64_t (*readc)(SgObject, SgPort*, SgChar*, int64_t,
62 		       SgErrorHandlingMode, int);
63       int64_t (*writec)(SgObject, SgPort*, SgChar *, int64_t,
64 			SgErrorHandlingMode);
65       SgEndianness endian;
66       /* only for utf16 and utf32 */
67       int     littlep;
68     } builtin;
69     struct {
70       /* TODO read and write */
71       SgObject getc;
72       SgObject putc;
73       SgObject readc;
74       SgObject writec;
75       SgObject data;
76     } custom;
77   } impl;
78 };
79 
80 SG_CLASS_DECL(Sg_CodecClass);
81 #define SG_CLASS_CODEC      (&Sg_CodecClass)
82 
83 #define SG_CODECP(obj) SG_XTYPEP(obj, SG_CLASS_CODEC)
84 #define SG_CODEC(obj)  ((SgCodec*)obj)
85 /* accessor */
86 #define SG_CODEC_NAME(obj)   (SG_CODEC(obj)->name)
87 
88 #define SG_CODEC_BUILTIN(obj) (&(SG_CODEC(obj)->impl.builtin))
89 #define SG_CODEC_CUSTOM(obj)  (&(SG_CODEC(obj)->impl.custom))
90 
91 #define SG_CODEC_ENDIAN(obj) (SG_CODEC_BUILTIN(obj)->endian)
92 
93 SG_CDECL_BEGIN
94 
95 SG_EXTERN SgObject Sg_MakeUtf8Codec();
96 SG_EXTERN SgObject Sg_MakeUtf16Codec(SgEndianness endian);
97 SG_EXTERN SgObject Sg_MakeUtf32Codec(SgEndianness endian);
98 SG_EXTERN SgObject Sg_MakeLatin1Codec();
99 
100 /* check BOM */
101 SG_EXTERN SgEndianness Sg_Utf16CheckBOM(SgByteVector *bv);
102 SG_EXTERN SgEndianness Sg_Utf32CheckBOM(SgByteVector *bv);
103 
104 /* Scheme interface */
105 SG_EXTERN SgObject Sg_MakeCustomCodecSimple(SgObject name, SgObject getc,
106 					    SgObject putc, SgObject data);
107 
108 SG_CDECL_END
109 
110 #endif /* SAGITTARIUS_CODEC_H_ */
111 
112 /*
113   end of file
114   Local Variables:
115   coding: utf-8-unix
116   End:
117 */
118