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_ptr
) == 0)
72 /* It's the builtin transliteration handling. We only
73 support it for working on the internal encoding. */
74 static const char *const internal_trans_names
[1]
76 struct trans_struct
*lastp
= NULL
;
77 struct trans_struct
*runp
;
79 for (runp
= trans
; runp
!= NULL
; runp
= runp
->next
)
80 if (runp
->trans_fct
== __gconv_transliterate
)
87 struct trans_struct
*newp
;
89 newp
= (struct trans_struct
*) alloca (sizeof (*newp
));
90 memset (newp
, '\0', sizeof (*newp
));
92 /* We leave the `name' field zero to signal that
93 this is an internal transliteration step. */
94 newp
->csnames
= (const char **) internal_trans_names
;
96 newp
->trans_fct
= __gconv_transliterate
;
104 else if (__strcasecmp_l (tok
, "IGNORE", _nl_C_locobj_ptr
) == 0)
105 /* Set the flag to ignore all errors. */
106 conv_flags
|= __GCONV_IGNORE_ERRORS
;
109 /* `tok' is possibly a module name. We'll see later
110 whether we can find it. But first see that we do
111 not already a module of this name. */
112 struct trans_struct
*lastp
= NULL
;
113 struct trans_struct
*runp
;
115 for (runp
= trans
; runp
!= NULL
; runp
= runp
->next
)
116 if (runp
->name
!= NULL
117 && __strcasecmp_l (tok
, runp
->name
,
118 _nl_C_locobj_ptr
) == 0)
125 struct trans_struct
*newp
;
127 newp
= (struct trans_struct
*) alloca (sizeof (*newp
));
128 memset (newp
, '\0', sizeof (*newp
));
138 tok
= __strtok_r (NULL
, ",", &ptr
);
143 /* For the source character set we ignore the error handler specification.
144 XXX Is this really always the best? */
145 ignore
= strchr (fromset
, '/');
146 if (ignore
!= NULL
&& (ignore
= strchr (ignore
+ 1, '/')) != NULL
147 && *++ignore
!= '\0')
149 char *newfromset
= (char *) alloca (ignore
- fromset
+ 1);
151 newfromset
[ignore
- fromset
] = '\0';
152 fromset
= memcpy (newfromset
, fromset
, ignore
- fromset
);
155 /* If the string is empty define this to mean the charset of the
156 currently selected locale. */
157 if (strcmp (toset
, "//") == 0)
159 const char *codeset
= _NL_CURRENT (LC_CTYPE
, CODESET
);
160 size_t len
= strlen (codeset
);
162 toset
= dest
= (char *) alloca (len
+ 3);
163 memcpy (__mempcpy (dest
, codeset
, len
), "//", 3);
165 if (strcmp (fromset
, "//") == 0)
167 const char *codeset
= _NL_CURRENT (LC_CTYPE
, CODESET
);
168 size_t len
= strlen (codeset
);
170 fromset
= dest
= (char *) alloca (len
+ 3);
171 memcpy (__mempcpy (dest
, codeset
, len
), "//", 3);
174 res
= __gconv_find_transform (toset
, fromset
, &steps
, &nsteps
, flags
);
175 if (res
== __GCONV_OK
)
177 /* Find the modules. */
178 struct trans_struct
*lastp
= NULL
;
179 struct trans_struct
*runp
;
181 for (runp
= trans
; runp
!= NULL
; runp
= runp
->next
)
183 if (runp
->name
== NULL
184 || __builtin_expect (__gconv_translit_find (runp
), 0) == 0)
188 /* This means we haven't found the module. Remove it. */
192 lastp
->next
= runp
->next
;
196 /* Allocate room for handle. */
197 result
= (__gconv_t
) malloc (sizeof (struct __gconv_info
)
199 * sizeof (struct __gconv_step_data
)));
206 /* Remember the list of steps. */
207 result
->__steps
= steps
;
208 result
->__nsteps
= nsteps
;
210 /* Clear the array for the step data. */
211 memset (result
->__data
, '\0',
212 nsteps
* sizeof (struct __gconv_step_data
));
214 /* Call all initialization functions for the transformation
215 step implementations. */
216 for (cnt
= 0; cnt
< nsteps
; ++cnt
)
220 /* Would have to be done if we would not clear the whole
223 /* Reset the counter. */
224 result
->__data
[cnt
].__invocation_counter
= 0;
226 /* It's a regular use. */
227 result
->__data
[cnt
].__internal_use
= 0;
230 /* We use the `mbstate_t' member in DATA. */
231 result
->__data
[cnt
].__statep
= &result
->__data
[cnt
].__state
;
233 /* Now see whether we can use any of the transliteration
234 modules for this step. */
235 for (runp
= trans
; runp
!= NULL
; runp
= runp
->next
)
236 for (n
= 0; n
< runp
->ncsnames
; ++n
)
237 if (__strcasecmp_l (steps
[cnt
].__from_name
,
238 runp
->csnames
[n
], _nl_C_locobj_ptr
) == 0)
242 /* Match! Now try the initializer. */
243 if (runp
->trans_init_fct
== NULL
244 || (runp
->trans_init_fct (&data
,
245 steps
[cnt
].__to_name
)
248 /* Append at the end of the list. */
249 struct __gconv_trans_data
*newp
;
250 struct __gconv_trans_data
**lastp
;
252 newp
= (struct __gconv_trans_data
*)
253 malloc (sizeof (struct __gconv_trans_data
));
260 newp
->__trans_fct
= runp
->trans_fct
;
261 newp
->__trans_context_fct
= runp
->trans_context_fct
;
262 newp
->__trans_end_fct
= runp
->trans_end_fct
;
266 lastp
= &result
->__data
[cnt
].__trans
;
267 while (*lastp
!= NULL
)
268 lastp
= &(*lastp
)->__next
;
275 /* If this is the last step we must not allocate an
277 if (cnt
< nsteps
- 1)
279 result
->__data
[cnt
].__flags
= conv_flags
;
281 /* Allocate the buffer. */
282 size
= (GCONV_NCHAR_GOAL
* steps
[cnt
].__max_needed_to
);
284 result
->__data
[cnt
].__outbuf
= malloc (size
);
285 if (result
->__data
[cnt
].__outbuf
== NULL
)
291 result
->__data
[cnt
].__outbufend
=
292 result
->__data
[cnt
].__outbuf
+ size
;
296 /* Handle the last entry. */
297 result
->__data
[cnt
].__flags
= conv_flags
| __GCONV_IS_LAST
;
304 if (res
!= __GCONV_OK
)
306 /* Something went wrong. Free all the resources. */
315 struct __gconv_trans_data
*transp
;
317 transp
= result
->__data
[cnt
].__trans
;
318 while (transp
!= NULL
)
320 struct __gconv_trans_data
*curp
= transp
;
321 transp
= transp
->__next
;
323 if (__builtin_expect (curp
->__trans_end_fct
!= NULL
, 0))
324 curp
->__trans_end_fct (curp
->__data
);
329 free (result
->__data
[cnt
].__outbuf
);
336 __gconv_close_transform (steps
, nsteps
);
338 __set_errno (serrno
);