1 /* Header for generic multibyte string representation.
2 Copyright (C) 1999 Electrotechnical Laboratory, JAPAN.
3 Licensed to the Free Software Foundation.
5 This file is part of XEmacs.
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
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
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. */
22 /* Rewritten by MORIOKA Tomohiko <tomo@m17n.org>. */
24 #ifndef _XEMACS_MB_MULTIBYTE_H
25 #define _XEMACS_MB_MULTIBYTE_H
27 /************************************************************************/
28 /* Operations on individual bytes */
30 /************************************************************************/
32 /* These are carefully designed to work if BYTE is signed or unsigned. */
33 /* Note that SPC and DEL are considered ASCII, not control. */
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)
40 /************************************************************************/
41 /* Dealing with characters */
42 /************************************************************************/
44 /* Is this character represented by more than one byte in a string? */
46 #define CHAR_MULTIBYTE_P(c) ((c) >= 0x80)
49 /* ---------------------------------------------------------------------- */
50 /* (A) For working with charptr's (pointers to internally-formatted text) */
51 /* ---------------------------------------------------------------------- */
53 # define VALID_CHARPTR_P(ptr) BUFBYTE_FIRST_BYTE_P (* (unsigned char *) ptr)
55 #define VALIDATE_CHARPTR_BACKWARD(ptr) do { \
56 while (!VALID_CHARPTR_P (ptr)) ptr--; \
59 /* This needs to be trickier to avoid the possibility of running off
60 the end of the string. */
62 #define VALIDATE_CHARPTR_FORWARD(ptr) do { \
63 Bufbyte *vcf_ptr = (ptr); \
64 VALIDATE_CHARPTR_BACKWARD (vcf_ptr); \
65 if (vcf_ptr != (ptr)) \
72 /* -------------------------------------------------------------------- */
73 /* (C) For retrieving or changing the character pointed to by a charptr */
74 /* -------------------------------------------------------------------- */
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)
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);
84 INLINE_HEADER Emchar charptr_emchar (const Bufbyte *ptr);
86 charptr_emchar (const Bufbyte *ptr)
88 return BYTE_ASCII_P (*ptr) ?
89 simple_charptr_emchar (ptr) :
90 non_ascii_charptr_emchar (ptr);
93 INLINE_HEADER Bytecount set_charptr_emchar (Bufbyte *ptr, Emchar x);
94 INLINE_HEADER Bytecount
95 set_charptr_emchar (Bufbyte *ptr, Emchar x)
97 return !CHAR_MULTIBYTE_P (x) ?
98 simple_set_charptr_emchar (ptr, x) :
99 non_ascii_set_charptr_emchar (ptr, x);
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)
109 return BYTE_ASCII_P (*src) ?
110 simple_charptr_copy_char (src, dst) :
111 non_ascii_charptr_copy_char (src, dst);
115 /************************************************************************/
116 /* Exported functions */
117 /************************************************************************/
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);
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);
128 #endif /* _XEMACS_MB_MULTIBYTE_H */