* __scm.h, stackchk.h, stackchk.c: Guile now performs stack
[bpt/guile.git] / libguile / extchrs.c
1 /* Copyright (C) 1995,1996 Free Software Foundation, Inc.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
19 *
20 * The exception is that, if you link the GUILE library with other files
21 * to produce an executable, this does not by itself cause the
22 * resulting executable to be covered by the GNU General Public License.
23 * Your use of that executable is in no way restricted on account of
24 * linking the GUILE library code into it.
25 *
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
28 *
29 * This exception applies only to the code released by the
30 * Free Software Foundation under the name GUILE. If you copy
31 * code from other Free Software Foundation releases into a copy of
32 * GUILE, as the General Public License permits, the exception does
33 * not apply to the code that you add in this way. To avoid misleading
34 * anyone as to the status of such modified files, you must delete
35 * this exception notice from them.
36 *
37 * If you write modifications of your own for GUILE, it is your choice
38 * whether to permit this exception to apply to your modifications.
39 * If you do not wish that, delete this exception notice.
40 */
41 \f
42
43 #include "extchrs.h"
44
45 \f
46
47 #ifdef FAKE_EXT_SCM_CHARS
48
49 #ifdef __STDC__
50 int
51 xmblen (const char * str, size_t size)
52 #else
53 int
54 xmblen (str, size)
55 const char * str;
56 size_t size;
57 #endif
58 {
59 if (!str)
60 return 0;
61
62 if (*(unsigned char *)str > 127)
63 return ((size < 4)
64 ? -1
65 : 4);
66 else if (!*str)
67 return 0;
68 else
69 return 1;
70 }
71
72 #ifdef __STDC__
73 int
74 xwctomb (char * _str, int c)
75 #else
76 int
77 xwctomb (_str, c)
78 char * _str;
79 int c;
80 #endif
81 {
82 unsigned char * str;
83 str = (unsigned char *)_str;
84 if (!str)
85 return 0;
86
87 if (!c)
88 {
89 *str = 0;
90 return 0;
91 }
92
93
94 if (c < 127)
95 {
96 *str = c;
97 return 1;
98 }
99
100 str[0] = 255;
101 str[1] = 0x80 | ((c >> 10) & 0x3f);
102 str[2] = 0x80 | ((c >> 4) & 0x3f);
103 str[3] = 0x80 | (c & 0xf);
104 return 4;
105 }
106
107 #ifdef __STDC__
108 int
109 xmbtowc (xwchar_t * result, const unsigned char * _str, size_t size)
110 #else
111 int
112 xmbtowc (result, str, size)
113 xwchar_t * result;
114 const unsigned char * _str;
115 size_t size;
116 #endif
117 {
118 const unsigned char * str;
119 str = (const unsigned char *)_str;
120 if (!str)
121 return 0;
122
123 if ((size == 0) || !*str)
124 {
125 *result = 0;
126 return 0;
127 }
128
129 if (*str < 128)
130 {
131 *result = *str;
132 return 1;
133 }
134
135 if ( (*str != 255)
136 || (size < 4))
137 return -1;
138
139 *result = ( ((str[1] & 0x3f) << 10)
140 | ((str[2] & 0x3f) << 4)
141 | (str[3] & 0xf));
142 return 4;
143 }
144
145 #endif /* FAKE_EXT_SCM_CHARS */
146