1 /* Find matching transformation algorithms and initialize steps.
2 Copyright (C) 1997, 1998, 1999 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. */
24 #include <gconv_int.h>
29 __gconv_open (const char *toset, const char *fromset, gconv_t *handle)
31 struct gconv_step *steps;
33 gconv_t result = NULL;
37 res = __gconv_find_transform (toset, fromset, &steps, &nsteps);
40 /* Allocate room for handle. */
41 result = (gconv_t) malloc (sizeof (struct gconv_info)
42 + nsteps * sizeof (struct gconv_step_data));
47 /* Remember the list of steps. */
48 result->steps = steps;
49 result->nsteps = nsteps;
51 /* Clear the array for the step data. */
52 memset (result->data, '\0',
53 nsteps * sizeof (struct gconv_step_data));
55 /* Call all initialization functions for the transformation
56 step implemenations. */
57 for (cnt = 0; cnt < nsteps; ++cnt)
59 /* If this is the last step we must not allocate an
61 result->data[cnt].is_last = cnt == nsteps - 1;
63 /* Reset the counter. */
64 result->data[cnt].invocation_counter = 0;
66 /* It's a regular use. */
67 result->data[cnt].internal_use = 0;
69 /* We use the `mbstate_t' member in DATA. */
70 result->data[cnt].statep = &result->data[cnt].__state;
72 /* Allocate the buffer. */
73 if (!result->data[cnt].is_last)
75 size_t size = (GCONV_NCHAR_GOAL
76 * steps[cnt].max_needed_to);
78 result->data[cnt].outbuf = (char *) malloc (size);
79 if (result->data[cnt].outbuf == NULL)
84 result->data[cnt].outbufend = (result->data[cnt].outbuf
93 /* Something went wrong. Free all the resources. */
99 free (result->data[cnt].outbuf);
105 __gconv_close_transform (steps, nsteps);
107 __set_errno (serrno);