(char_code_table): New type.
[chise/xemacs-chise.git] / 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 /* Argument `c' should be (unsigned int) or (unsigned char). */
33 /* Note that SP and DEL are not included. */
34
35 #define BYTE_ASCII_P(c) ((c) < 0x80)
36 #define BYTE_C0_P(c) ((c) < 0x20)
37 /* Do some forced casting just to make *sure* things are gotten right. */
38 #define BYTE_C1_P(c) ((unsigned int) ((unsigned int) (c) - 0x80) < 0x20)
39
40
41 /************************************************************************/
42 /*                        Dealing with characters                       */
43 /************************************************************************/
44
45 /* Is this character represented by more than one byte in a string? */
46
47 #define CHAR_MULTIBYTE_P(c) ((c) >= 0x80)
48
49
50 /* ---------------------------------------------------------------------- */
51 /* (A) For working with charptr's (pointers to internally-formatted text) */
52 /* ---------------------------------------------------------------------- */
53
54 # define VALID_CHARPTR_P(ptr) BUFBYTE_FIRST_BYTE_P (* (unsigned char *) ptr)
55
56 #define VALIDATE_CHARPTR_BACKWARD(ptr) do {     \
57   while (!VALID_CHARPTR_P (ptr)) ptr--;         \
58 } while (0)
59
60 /* This needs to be trickier to avoid the possibility of running off
61    the end of the string. */
62
63 #define VALIDATE_CHARPTR_FORWARD(ptr) do {      \
64   Bufbyte *vcf_ptr = (ptr);                     \
65   VALIDATE_CHARPTR_BACKWARD (vcf_ptr);          \
66   if (vcf_ptr != (ptr))                         \
67     {                                           \
68       (ptr) = vcf_ptr;                          \
69       INC_CHARPTR (ptr);                        \
70     }                                           \
71 } while (0)
72
73 /* -------------------------------------------------------------------- */
74 /* (C) For retrieving or changing the character pointed to by a charptr */
75 /* -------------------------------------------------------------------- */
76
77 #define simple_charptr_emchar(ptr)              ((Emchar) (ptr)[0])
78 #define simple_set_charptr_emchar(ptr, x)       ((ptr)[0] = (Bufbyte) (x), 1)
79 #define simple_charptr_copy_char(ptr, ptr2)     ((ptr2)[0] = *(ptr), 1)
80
81 Emchar non_ascii_charptr_emchar (CONST Bufbyte *ptr);
82 Bytecount non_ascii_set_charptr_emchar (Bufbyte *ptr, Emchar c);
83 Bytecount non_ascii_charptr_copy_char (CONST Bufbyte *ptr, Bufbyte *ptr2);
84
85 INLINE Emchar charptr_emchar (CONST Bufbyte *ptr);
86 INLINE Emchar
87 charptr_emchar (CONST Bufbyte *ptr)
88 {
89   return BYTE_ASCII_P (*ptr) ?
90     simple_charptr_emchar (ptr) :
91     non_ascii_charptr_emchar (ptr);
92 }
93
94 INLINE Bytecount set_charptr_emchar (Bufbyte *ptr, Emchar x);
95 INLINE Bytecount
96 set_charptr_emchar (Bufbyte *ptr, Emchar x)
97 {
98   return !CHAR_MULTIBYTE_P (x) ?
99     simple_set_charptr_emchar (ptr, x) :
100     non_ascii_set_charptr_emchar (ptr, x);
101 }
102
103 INLINE Bytecount charptr_copy_char (CONST Bufbyte *ptr, Bufbyte *ptr2);
104 INLINE Bytecount
105 charptr_copy_char (CONST Bufbyte *ptr, Bufbyte *ptr2)
106 {
107   return BYTE_ASCII_P (*ptr) ?
108     simple_charptr_copy_char (ptr, ptr2) :
109     non_ascii_charptr_copy_char (ptr, ptr2);
110 }
111
112
113 /************************************************************************/
114 /*                            Exported functions                        */
115 /************************************************************************/
116
117 Emchar Lstream_get_emchar_1 (Lstream *stream, int first_char);
118 int Lstream_fput_emchar (Lstream *stream, Emchar ch);
119 void Lstream_funget_emchar (Lstream *stream, Emchar ch);
120
121 int copy_internal_to_external (CONST Bufbyte *internal, Bytecount len,
122                                unsigned char *external);
123 Bytecount copy_external_to_internal (CONST unsigned char *external,
124                                      int len, Bufbyte *internal);
125
126 #endif /* _XEMACS_MB_MULTIBYTE_H */