update.
[chise/xemacs-chise.git.1] / src / mb-multibyte.h
1 /* Header for generic multibyte string representation.
2    Copyright (C) 1999 Electrotechnical Laboratory, JAPAN.
3    Licensed to the Free Software Foundation.
4
5 This file is part of XEmacs.
6
7 XEmacs is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
11
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with XEmacs; see the file COPYING.  If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 /* Rewritten by MORIOKA Tomohiko <tomo@m17n.org>. */
23
24 #ifndef _XEMACS_MB_MULTIBYTE_H
25 #define _XEMACS_MB_MULTIBYTE_H
26
27 /************************************************************************/
28 /*                     Operations on individual bytes                   */
29 /*                             of any format                            */
30 /************************************************************************/
31
32 /* These are carefully designed to work if BYTE is signed or unsigned. */
33 /* Note that SPC and DEL are considered ASCII, not control. */
34
35 #define BYTE_ASCII_P(byte) (((byte) & ~0x7f) == 0)
36 #define BYTE_C0_P(byte)    (((byte) & ~0x1f) == 0)
37 #define BYTE_C1_P(byte)    (((byte) & ~0x1f) == 0x80)
38
39
40 /************************************************************************/
41 /*                        Dealing with characters                       */
42 /************************************************************************/
43
44 /* Is this character represented by more than one byte in a string? */
45
46 #define CHAR_MULTIBYTE_P(c) ((c) >= 0x80)
47
48
49 /* ---------------------------------------------------------------------- */
50 /* (A) For working with charptr's (pointers to internally-formatted text) */
51 /* ---------------------------------------------------------------------- */
52
53 # define VALID_CHARPTR_P(ptr) BUFBYTE_FIRST_BYTE_P (* (unsigned char *) ptr)
54
55 #define VALIDATE_CHARPTR_BACKWARD(ptr) do {     \
56   while (!VALID_CHARPTR_P (ptr)) ptr--;         \
57 } while (0)
58
59 /* This needs to be trickier to avoid the possibility of running off
60    the end of the string. */
61
62 #define VALIDATE_CHARPTR_FORWARD(ptr) do {      \
63   Bufbyte *vcf_ptr = (ptr);                     \
64   VALIDATE_CHARPTR_BACKWARD (vcf_ptr);          \
65   if (vcf_ptr != (ptr))                         \
66     {                                           \
67       (ptr) = vcf_ptr;                          \
68       INC_CHARPTR (ptr);                        \
69     }                                           \
70 } while (0)
71
72 /* -------------------------------------------------------------------- */
73 /* (C) For retrieving or changing the character pointed to by a charptr */
74 /* -------------------------------------------------------------------- */
75
76 #define simple_charptr_emchar(ptr)              ((Emchar) (ptr)[0])
77 #define simple_set_charptr_emchar(ptr, x)       ((ptr)[0] = (Bufbyte) (x), 1)
78 #define simple_charptr_copy_char(ptr, ptr2)     ((ptr2)[0] = *(ptr), 1)
79
80 Emchar non_ascii_charptr_emchar (const Bufbyte *ptr);
81 Bytecount non_ascii_set_charptr_emchar (Bufbyte *ptr, Emchar c);
82 Bytecount non_ascii_charptr_copy_char (const Bufbyte *src, Bufbyte *dst);
83
84 INLINE_HEADER Emchar charptr_emchar (const Bufbyte *ptr);
85 INLINE_HEADER Emchar
86 charptr_emchar (const Bufbyte *ptr)
87 {
88   return BYTE_ASCII_P (*ptr) ?
89     simple_charptr_emchar (ptr) :
90     non_ascii_charptr_emchar (ptr);
91 }
92
93 INLINE_HEADER Bytecount set_charptr_emchar (Bufbyte *ptr, Emchar x);
94 INLINE_HEADER Bytecount
95 set_charptr_emchar (Bufbyte *ptr, Emchar x)
96 {
97   return !CHAR_MULTIBYTE_P (x) ?
98     simple_set_charptr_emchar (ptr, x) :
99     non_ascii_set_charptr_emchar (ptr, x);
100 }
101
102 /* Copy the character pointed to by SRC into DST.
103    Return the number of bytes copied.  */
104 INLINE_HEADER Bytecount
105 charptr_copy_char (const Bufbyte *src, Bufbyte *dst);
106 INLINE_HEADER Bytecount
107 charptr_copy_char (const Bufbyte *src, Bufbyte *dst)
108 {
109   return BYTE_ASCII_P (*src) ?
110     simple_charptr_copy_char (src, dst) :
111     non_ascii_charptr_copy_char (src, dst);
112 }
113
114
115 /************************************************************************/
116 /*                            Exported functions                        */
117 /************************************************************************/
118
119 Emchar Lstream_get_emchar_1 (Lstream *stream, int first_char);
120 int Lstream_fput_emchar (Lstream *stream, Emchar ch);
121 void Lstream_funget_emchar (Lstream *stream, Emchar ch);
122
123 int copy_internal_to_external (const Bufbyte *internal, Bytecount len,
124                                unsigned char *external);
125 Bytecount copy_external_to_internal (const unsigned char *external,
126                                      int len, Bufbyte *internal);
127
128 #endif /* _XEMACS_MB_MULTIBYTE_H */