1 /* Find matching transformation algorithms and initialize steps.
2 Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
25 #include <gconv_int.h>
30 __gconv_open (const char *toset, const char *fromset, __gconv_t *handle,
33 struct __gconv_step *steps;
35 __gconv_t result = NULL;
41 /* Find out whether any error handling method is specified. */
42 errhand = strchr (toset, '/');
44 errhand = strchr (errhand + 1, '/');
45 if (__builtin_expect (errhand != NULL, 1))
47 if (errhand[1] == '\0')
51 /* Make copy without the error handling description. */
52 char *newtoset = (char *) alloca (errhand - toset + 1);
54 newtoset[errhand - toset] = '\0';
55 toset = memcpy (newtoset, toset, errhand - toset);
57 flags = __GCONV_IGNORE_ERRORS;
59 if (strcasecmp (errhand, "IGNORE") == 0)
61 /* Found it. This means we should ignore conversion errors. */
62 flags = __GCONV_IGNORE_ERRORS;
68 res = __gconv_find_transform (toset, fromset, &steps, &nsteps, flags);
69 if (res == __GCONV_OK)
71 const char **csnames = NULL;
73 __gconv_trans_fct trans_fct = NULL;
74 __gconv_trans_context_fct trans_context_fct = NULL;
75 __gconv_trans_init_fct trans_init_fct = NULL;
76 __gconv_trans_end_fct trans_end_fct = NULL;
80 /* Find the appropriate transliteration handling. */
81 if (strcasecmp (errhand, "TRANSLIT") == 0)
83 /* It's the builtin transliteration handling. We only
84 suport for it working on the internal encoding. */
85 static const char *internal_trans_names[1] = { "INTERNAL" };
87 csnames = internal_trans_names;
89 trans_fct = gconv_transliterate;
90 /* No context, init, or end function. */
92 else if (strcasecmp (errhand, "WORK AROUND A GCC BUG") == 0)
94 trans_init_fct = (__gconv_trans_init_fct) 1;
98 /* Allocate room for handle. */
99 result = (__gconv_t) malloc (sizeof (struct __gconv_info)
101 * sizeof (struct __gconv_step_data)));
108 /* Remember the list of steps. */
109 result->__steps = steps;
110 result->__nsteps = nsteps;
112 /* Clear the array for the step data. */
113 memset (result->__data, '\0',
114 nsteps * sizeof (struct __gconv_step_data));
116 /* Call all initialization functions for the transformation
117 step implementations. */
118 for (cnt = 0; cnt < nsteps - 1; ++cnt)
122 /* Would have to be done if we would not clear the whole
124 /* If this is the last step we must not allocate an
126 result->__data[cnt].__flags = conv_flags;
129 /* Reset the counter. */
130 result->__data[cnt].__invocation_counter = 0;
132 /* It's a regular use. */
133 result->__data[cnt].__internal_use = 0;
136 /* We use the `mbstate_t' member in DATA. */
137 result->__data[cnt].__statep = &result->__data[cnt].__state;
139 /* Allocate the buffer. */
140 size = (GCONV_NCHAR_GOAL * steps[cnt].__max_needed_to);
142 result->__data[cnt].__outbuf = (char *) malloc (size);
143 if (result->__data[cnt].__outbuf == NULL)
148 result->__data[cnt].__outbufend =
149 result->__data[cnt].__outbuf + size;
151 /* Now see whether we can use the transliteration module
153 for (n = 0; n < ncsnames; ++n)
154 if (strcasecmp (steps[cnt].__from_name, csnames[n]) == 0)
156 /* Match! Now try the initializer. */
157 if (trans_init_fct == NULL
158 || (trans_init_fct (&result->__data[cnt].__trans.__data,
159 steps[cnt].__to_name)
162 result->__data[cnt].__trans.__trans_fct = trans_fct;
163 result->__data[cnt].__trans.__trans_context_fct =
165 result->__data[cnt].__trans.__trans_end_fct =
172 /* Now handle the last entry. */
173 result->__data[cnt].__flags = conv_flags | __GCONV_IS_LAST;
174 /* Would have to be done if we would not clear the whole
177 result->__data[cnt].__invocation_counter = 0;
178 result->__data[cnt].__internal_use = 0;
180 result->__data[cnt].__statep = &result->__data[cnt].__state;
182 /* Now see whether we can use the transliteration module
184 for (n = 0; n < ncsnames; ++n)
185 if (strcasecmp (steps[cnt].__from_name, csnames[n]) == 0)
187 /* Match! Now try the initializer. */
188 if (trans_init_fct == NULL
189 || trans_init_fct (&result->__data[cnt].__trans.__data,
190 steps[cnt].__to_name)
193 result->__data[cnt].__trans.__trans_fct = trans_fct;
194 result->__data[cnt].__trans.__trans_context_fct =
196 result->__data[cnt].__trans.__trans_end_fct =
203 if (res != __GCONV_OK)
205 /* Something went wrong. Free all the resources. */
211 free (result->__data[cnt].__outbuf);
217 __gconv_close_transform (steps, nsteps);
219 __set_errno (serrno);