1 /* Find matching transformation algorithms and initialize steps.
2 Copyright (C) 1997,1998,1999,2000,2001,2004,2005
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 #include "../locale/localeinfo.h"
28 #include <gconv_int.h>
33 __gconv_open (const char *toset
, const char *fromset
, __gconv_t
*handle
,
36 struct __gconv_step
*steps
;
38 __gconv_t result
= NULL
;
44 struct trans_struct
*trans
= NULL
;
46 /* Find out whether any error handling method is specified. */
47 errhand
= strchr (toset
, '/');
49 errhand
= strchr (errhand
+ 1, '/');
50 if (__builtin_expect (errhand
!= NULL
, 1))
52 if (*++errhand
== '\0')
56 /* Make copy without the error handling description. */
57 char *newtoset
= (char *) alloca (errhand
- toset
+ 1);
61 newtoset
[errhand
- toset
] = '\0';
62 toset
= memcpy (newtoset
, toset
, errhand
- toset
);
64 /* Find the appropriate transliteration handlers. */
65 tok
= strdupa (errhand
);
67 tok
= __strtok_r (tok
, ",", &ptr
);
70 if (__strcasecmp_l (tok
, "TRANSLIT", &_nl_C_locobj
) == 0)
72 /* It's the builtin transliteration handling. We only
73 support it for working on the internal encoding. */
74 static const char *internal_trans_names
[1] = { "INTERNAL" };
75 struct trans_struct
*lastp
= NULL
;
76 struct trans_struct
*runp
;
78 for (runp
= trans
; runp
!= NULL
; runp
= runp
->next
)
79 if (runp
->trans_fct
== __gconv_transliterate
)
86 struct trans_struct
*newp
;
88 newp
= (struct trans_struct
*) alloca (sizeof (*newp
));
89 memset (newp
, '\0', sizeof (*newp
));
91 /* We leave the `name' field zero to signal that
92 this is an internal transliteration step. */
93 newp
->csnames
= internal_trans_names
;
95 newp
->trans_fct
= __gconv_transliterate
;
103 else if (__strcasecmp_l (tok
, "IGNORE", &_nl_C_locobj
) == 0)
104 /* Set the flag to ignore all errors. */
105 conv_flags
|= __GCONV_IGNORE_ERRORS
;
108 /* `tok' is possibly a module name. We'll see later
109 whether we can find it. But first see that we do
110 not already a module of this name. */
111 struct trans_struct
*lastp
= NULL
;
112 struct trans_struct
*runp
;
114 for (runp
= trans
; runp
!= NULL
; runp
= runp
->next
)
115 if (runp
->name
!= NULL
116 && __strcasecmp_l (tok
, runp
->name
,
124 struct trans_struct
*newp
;
126 newp
= (struct trans_struct
*) alloca (sizeof (*newp
));
127 memset (newp
, '\0', sizeof (*newp
));
137 tok
= __strtok_r (NULL
, ",", &ptr
);
142 /* For the source character set we ignore the error handler specification.
143 XXX Is this really always the best? */
144 ignore
= strchr (fromset
, '/');
145 if (ignore
!= NULL
&& (ignore
= strchr (ignore
+ 1, '/')) != NULL
146 && *++ignore
!= '\0')
148 char *newfromset
= (char *) alloca (ignore
- fromset
+ 1);
150 newfromset
[ignore
- fromset
] = '\0';
151 fromset
= memcpy (newfromset
, fromset
, ignore
- fromset
);
154 /* If the string is empty define this to mean the charset of the
155 currently selected locale. */
156 if (strcmp (toset
, "//") == 0)
158 const char *codeset
= _NL_CURRENT (LC_CTYPE
, CODESET
);
159 size_t len
= strlen (codeset
);
161 toset
= dest
= (char *) alloca (len
+ 3);
162 memcpy (__mempcpy (dest
, codeset
, len
), "//", 3);
164 if (strcmp (fromset
, "//") == 0)
166 const char *codeset
= _NL_CURRENT (LC_CTYPE
, CODESET
);
167 size_t len
= strlen (codeset
);
169 fromset
= dest
= (char *) alloca (len
+ 3);
170 memcpy (__mempcpy (dest
, codeset
, len
), "//", 3);
173 res
= __gconv_find_transform (toset
, fromset
, &steps
, &nsteps
, flags
);
174 if (res
== __GCONV_OK
)
176 /* Find the modules. */
177 struct trans_struct
*lastp
= NULL
;
178 struct trans_struct
*runp
;
180 for (runp
= trans
; runp
!= NULL
; runp
= runp
->next
)
182 if (runp
->name
== NULL
183 || __builtin_expect (__gconv_translit_find (runp
), 0) == 0)
187 /* This means we haven't found the module. Remove it. */
191 lastp
->next
= runp
->next
;
195 /* Allocate room for handle. */
196 result
= (__gconv_t
) malloc (sizeof (struct __gconv_info
)
198 * sizeof (struct __gconv_step_data
)));
205 /* Remember the list of steps. */
206 result
->__steps
= steps
;
207 result
->__nsteps
= nsteps
;
209 /* Clear the array for the step data. */
210 memset (result
->__data
, '\0',
211 nsteps
* sizeof (struct __gconv_step_data
));
213 /* Call all initialization functions for the transformation
214 step implementations. */
215 for (cnt
= 0; cnt
< nsteps
; ++cnt
)
219 /* Would have to be done if we would not clear the whole
222 /* Reset the counter. */
223 result
->__data
[cnt
].__invocation_counter
= 0;
225 /* It's a regular use. */
226 result
->__data
[cnt
].__internal_use
= 0;
229 /* We use the `mbstate_t' member in DATA. */
230 result
->__data
[cnt
].__statep
= &result
->__data
[cnt
].__state
;
232 /* Now see whether we can use any of the transliteration
233 modules for this step. */
234 for (runp
= trans
; runp
!= NULL
; runp
= runp
->next
)
235 for (n
= 0; n
< runp
->ncsnames
; ++n
)
236 if (__strcasecmp_l (steps
[cnt
].__from_name
,
237 runp
->csnames
[n
], &_nl_C_locobj
) == 0)
241 /* Match! Now try the initializer. */
242 if (runp
->trans_init_fct
== NULL
243 || (runp
->trans_init_fct (&data
,
244 steps
[cnt
].__to_name
)
247 /* Append at the end of the list. */
248 struct __gconv_trans_data
*newp
;
249 struct __gconv_trans_data
**lastp
;
251 newp
= (struct __gconv_trans_data
*)
252 malloc (sizeof (struct __gconv_trans_data
));
259 newp
->__trans_fct
= runp
->trans_fct
;
260 newp
->__trans_context_fct
= runp
->trans_context_fct
;
261 newp
->__trans_end_fct
= runp
->trans_end_fct
;
265 lastp
= &result
->__data
[cnt
].__trans
;
266 while (*lastp
!= NULL
)
267 lastp
= &(*lastp
)->__next
;
274 /* If this is the last step we must not allocate an
276 if (cnt
< nsteps
- 1)
278 result
->__data
[cnt
].__flags
= conv_flags
;
280 /* Allocate the buffer. */
281 size
= (GCONV_NCHAR_GOAL
* steps
[cnt
].__max_needed_to
);
283 result
->__data
[cnt
].__outbuf
= malloc (size
);
284 if (result
->__data
[cnt
].__outbuf
== NULL
)
290 result
->__data
[cnt
].__outbufend
=
291 result
->__data
[cnt
].__outbuf
+ size
;
295 /* Handle the last entry. */
296 result
->__data
[cnt
].__flags
= conv_flags
| __GCONV_IS_LAST
;
303 if (res
!= __GCONV_OK
)
305 /* Something went wrong. Free all the resources. */
314 struct __gconv_trans_data
*transp
;
316 transp
= result
->__data
[cnt
].__trans
;
317 while (transp
!= NULL
)
319 struct __gconv_trans_data
*curp
= transp
;
320 transp
= transp
->__next
;
322 if (__builtin_expect (curp
->__trans_end_fct
!= NULL
, 0))
323 curp
->__trans_end_fct (curp
->__data
);
328 free (result
->__data
[cnt
].__outbuf
);
335 __gconv_close_transform (steps
, nsteps
);
337 __set_errno (serrno
);