Update to 2.1.x development version
[glibc/history.git] / locale / programs / charset.c
blob00233abf8f98ffeff018b372280e5faa0c0efb95
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.
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.
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.
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. */
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <alloca.h>
25 #include <ctype.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #include "error.h"
31 #include "charset.h"
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);
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)
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);
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)
56 /* For now: perhaps <Uxxxx> support will be removed again... */
57 insert_char (lr, cs, bytes, value, from, to);
61 unsigned int
62 charset_find_value (const struct charset_t *cs, const char *name, size_t len)
64 void *result;
66 if (find_entry ((hash_table *) &cs->char_table, name, len, &result) < 0)
67 return ILLEGAL_CHAR_VALUE;
69 return (unsigned int) ((unsigned long int) result);
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)
77 const char *cp;
78 char *buf;
79 int prefix_len, len1, len2;
80 unsigned int from_nr, to_nr, cnt;
82 if (to == NULL)
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);
89 return;
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);
98 if (len1 != len2)
100 illegal_range:
101 lr_error (lr, _("illegal names for character range"));
102 return;
105 cp = &from[len1 - 1];
106 while (isdigit (*cp) && cp >= from)
107 --cp;
109 prefix_len = (cp - from) + 1;
111 if (cp == &from[len1 - 1] || strncmp (from, to, prefix_len) != 0)
112 goto illegal_range;
114 from_nr = strtoul (&from[prefix_len], NULL, 10);
115 to_nr = strtoul (&to[prefix_len], NULL, 10);
117 if (from_nr > to_nr)
119 lr_error (lr, _("upper limit in range is not smaller then lower limit"));
120 return;
123 buf = alloca (len1 + 1);
124 memcpy (buf, from, prefix_len);
126 for (cnt = from_nr; cnt <= to_nr; ++cnt)
128 sprintf (&buf[prefix_len], "%0d", cnt);
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);