]> git.ipfire.org Git - thirdparty/glibc.git/blob - locale/programs/charset.c
Update to 2.1.x development version
[thirdparty/glibc.git] / locale / programs / charset.c
1 /* Copyright (C) 1996, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1996.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <alloca.h>
25 #include <ctype.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "error.h"
31 #include "charset.h"
32
33
34 static void
35 insert_char (struct linereader *lr, struct charset_t *cs, int bytes,
36 unsigned int value, const char *from, const char *to);
37
38
39 void
40 charset_new_char (struct linereader *lr, struct charset_t *cs, int bytes,
41 unsigned int value, const char *from, const char *to)
42 {
43 if (bytes < cs->mb_cur_min)
44 lr_error (lr, _("too few bytes in character encoding"));
45 else if (bytes > cs->mb_cur_max)
46 lr_error (lr, _("too many bytes in character encoding"));
47 else
48 insert_char (lr, cs, bytes, value, from, to);
49 }
50
51
52 void
53 charset_new_unicode (struct linereader *lr, struct charset_t *cs, int bytes,
54 unsigned int value, const char *from, const char *to)
55 {
56 /* For now: perhaps <Uxxxx> support will be removed again... */
57 insert_char (lr, cs, bytes, value, from, to);
58 }
59
60
61 unsigned int
62 charset_find_value (const struct charset_t *cs, const char *name, size_t len)
63 {
64 void *result;
65
66 if (find_entry ((hash_table *) &cs->char_table, name, len, &result) < 0)
67 return ILLEGAL_CHAR_VALUE;
68
69 return (unsigned int) ((unsigned long int) result);
70 }
71
72
73 static void
74 insert_char (struct linereader *lr, struct charset_t *cs, int bytes,
75 unsigned int value, const char *from, const char *to)
76 {
77 const char *cp;
78 char *buf;
79 int prefix_len, len1, len2;
80 unsigned int from_nr, to_nr, cnt;
81
82 if (to == NULL)
83 {
84 if (insert_entry (&cs->char_table, from, strlen (from),
85 (void *) (unsigned long int) value)
86 < 0)
87 lr_error (lr, _("duplicate character name `%s'"), from);
88
89 return;
90 }
91
92 /* We have a range: the names must have names with equal prefixes
93 and an equal number of digits, where the second number is greater
94 or equal than the first. */
95 len1 = strlen (from);
96 len2 = strlen (to);
97
98 if (len1 != len2)
99 {
100 illegal_range:
101 lr_error (lr, _("illegal names for character range"));
102 return;
103 }
104
105 cp = &from[len1 - 1];
106 while (isdigit (*cp) && cp >= from)
107 --cp;
108
109 prefix_len = (cp - from) + 1;
110
111 if (cp == &from[len1 - 1] || strncmp (from, to, prefix_len) != 0)
112 goto illegal_range;
113
114 from_nr = strtoul (&from[prefix_len], NULL, 10);
115 to_nr = strtoul (&to[prefix_len], NULL, 10);
116
117 if (from_nr > to_nr)
118 {
119 lr_error (lr, _("upper limit in range is not smaller then lower limit"));
120 return;
121 }
122
123 buf = alloca (len1 + 1);
124 memcpy (buf, from, prefix_len);
125
126 for (cnt = from_nr; cnt <= to_nr; ++cnt)
127 {
128 sprintf (&buf[prefix_len], "%0d", cnt);
129
130 if (insert_entry (&cs->char_table, buf, len1,
131 (void *) (unsigned long int) cnt) < 0)
132 lr_error (lr, _("duplicate character name `%s'"), buf);
133 }
134 }