]>
git.saurik.com Git - apple/libc.git/blob - locale.subproj/rune.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
24 * The Regents of the University of California. All rights reserved.
26 * This code is derived from software contributed to Berkeley by
27 * Paul Borman at Krystal Technologies.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #include <sys/types.h>
69 extern int _none_init
__P((_RuneLocale
*));
70 extern int _UTF2_init
__P((_RuneLocale
*));
71 extern int _EUC_init
__P((_RuneLocale
*));
72 static _RuneLocale
*_Read_RuneMagi
__P((FILE *));
74 static char *PathLocale
= 0;
77 setrunelocale(encoding
)
88 * The "C" and "POSIX" locale are always here.
90 if (!strcmp(encoding
, "C") || !strcmp(encoding
, "POSIX")) {
91 _CurrentRuneLocale
= &_DefaultRuneLocale
;
96 PathLocale
= _PATH_LOCALE
;
98 sprintf(name
, "%s/%s/LC_CTYPE", PathLocale
, encoding
);
100 if ((fp
= fopen(name
, "r")) == NULL
)
103 if ((rl
= _Read_RuneMagi(fp
)) == 0) {
108 if (!rl
->encoding
[0] || !strcmp(rl
->encoding
, "UTF2")) {
109 return(_UTF2_init(rl
));
110 } else if (!strcmp(rl
->encoding
, "NONE")) {
111 return(_none_init(rl
));
112 } else if (!strcmp(rl
->encoding
, "EUC")) {
113 return(_EUC_init(rl
));
137 if (fstat(fileno(fp
), &sb
) < 0)
140 if (sb
.st_size
< sizeof(_RuneLocale
))
143 if ((data
= malloc(sb
.st_size
)) == NULL
)
146 rewind(fp
); /* Someone might have read the magic number once already */
148 if (fread(data
, sb
.st_size
, 1, fp
) != 1) {
153 rl
= (_RuneLocale
*)data
;
154 lastp
= data
+ sb
.st_size
;
156 rl
->variable
= rl
+ 1;
158 if (memcmp(rl
->magic
, _RUNE_MAGIC_1
, sizeof(rl
->magic
))) {
163 rl
->invalid_rune
= ntohl(rl
->invalid_rune
);
164 rl
->variable_len
= ntohl(rl
->variable_len
);
165 rl
->runetype_ext
.nranges
= ntohl(rl
->runetype_ext
.nranges
);
166 rl
->maplower_ext
.nranges
= ntohl(rl
->maplower_ext
.nranges
);
167 rl
->mapupper_ext
.nranges
= ntohl(rl
->mapupper_ext
.nranges
);
169 for (x
= 0; x
< _CACHED_RUNES
; ++x
) {
170 rl
->runetype
[x
] = ntohl(rl
->runetype
[x
]);
171 rl
->maplower
[x
] = ntohl(rl
->maplower
[x
]);
172 rl
->mapupper
[x
] = ntohl(rl
->mapupper
[x
]);
175 rl
->runetype_ext
.ranges
= (_RuneEntry
*)rl
->variable
;
176 rl
->variable
= rl
->runetype_ext
.ranges
+ rl
->runetype_ext
.nranges
;
177 if (rl
->variable
> lastp
) {
182 rl
->maplower_ext
.ranges
= (_RuneEntry
*)rl
->variable
;
183 rl
->variable
= rl
->maplower_ext
.ranges
+ rl
->maplower_ext
.nranges
;
184 if (rl
->variable
> lastp
) {
189 rl
->mapupper_ext
.ranges
= (_RuneEntry
*)rl
->variable
;
190 rl
->variable
= rl
->mapupper_ext
.ranges
+ rl
->mapupper_ext
.nranges
;
191 if (rl
->variable
> lastp
) {
196 for (x
= 0; x
< rl
->runetype_ext
.nranges
; ++x
) {
197 rr
= rl
->runetype_ext
.ranges
;
199 rr
[x
].min
= ntohl(rr
[x
].min
);
200 rr
[x
].max
= ntohl(rr
[x
].max
);
201 if ((rr
[x
].map
= ntohl(rr
[x
].map
)) == 0) {
202 int len
= rr
[x
].max
- rr
[x
].min
+ 1;
203 rr
[x
].types
= rl
->variable
;
204 rl
->variable
= rr
[x
].types
+ len
;
205 if (rl
->variable
> lastp
) {
210 rr
[x
].types
[len
] = ntohl(rr
[x
].types
[len
]);
215 for (x
= 0; x
< rl
->maplower_ext
.nranges
; ++x
) {
216 rr
= rl
->maplower_ext
.ranges
;
218 rr
[x
].min
= ntohl(rr
[x
].min
);
219 rr
[x
].max
= ntohl(rr
[x
].max
);
220 rr
[x
].map
= ntohl(rr
[x
].map
);
223 for (x
= 0; x
< rl
->mapupper_ext
.nranges
; ++x
) {
224 rr
= rl
->mapupper_ext
.ranges
;
226 rr
[x
].min
= ntohl(rr
[x
].min
);
227 rr
[x
].max
= ntohl(rr
[x
].max
);
228 rr
[x
].map
= ntohl(rr
[x
].map
);
230 if (((char *)rl
->variable
) + rl
->variable_len
> (char *)lastp
) {
236 * Go out and zero pointers that should be zero.
238 if (!rl
->variable_len
)
241 if (!rl
->runetype_ext
.nranges
)
242 rl
->runetype_ext
.ranges
= 0;
244 if (!rl
->maplower_ext
.nranges
)
245 rl
->maplower_ext
.ranges
= 0;
247 if (!rl
->mapupper_ext
.nranges
)
248 rl
->mapupper_ext
.ranges
= 0;
258 _RuneRange
*rr
= &_CurrentRuneLocale
->runetype_ext
;
259 _RuneEntry
*re
= rr
->ranges
;
263 for (x
= 0; x
< rr
->nranges
; ++x
, ++re
) {
268 return(re
->types
[c
- re
->min
]);
281 _RuneRange
*rr
= &_CurrentRuneLocale
->mapupper_ext
;
282 _RuneEntry
*re
= rr
->ranges
;
286 for (x
= 0; x
< rr
->nranges
; ++x
, ++re
) {
290 return(re
->map
+ c
- re
->min
);
300 _RuneRange
*rr
= &_CurrentRuneLocale
->maplower_ext
;
301 _RuneEntry
*re
= rr
->ranges
;
305 for (x
= 0; x
< rr
->nranges
; ++x
, ++re
) {
309 return(re
->map
+ c
- re
->min
);
315 #if !defined(_USE_CTYPE_INLINE_) && !defined(_USE_CTYPE_MACROS_)
317 * See comments in <machine/ansi.h>
324 return ((((c
& _CRMASK
) ? ___runetype(c
)
325 : _CurrentRuneLocale
->runetype
[c
]) & f
) ? 1 : 0);
329 __isctype(_BSD_RUNE_T_ c
, unsigned long f
)
333 return ((((c
& _CRMASK
) ? 0
334 : _DefaultRuneLocale
.runetype
[c
]) & f
) ? 1 : 0);
341 return ((c
& _CRMASK
) ?
342 ___toupper(c
) : _CurrentRuneLocale
->mapupper
[c
]);
349 return ((c
& _CRMASK
) ?
350 ___tolower(c
) : _CurrentRuneLocale
->maplower
[c
]);