Update.
[glibc.git] / iconv / gconv_open.c
blobfb5f88b9a336a0ca98a24d2678e303423f6e3858
1 /* Find matching transformation algorithms and initialize steps.
2 Copyright (C) 1997, 1998 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. */
21 #include <errno.h>
22 #include <stdlib.h>
24 #include <gconv_int.h>
27 int
28 internal_function
29 __gconv_open (const char *toset, const char *fromset, gconv_t *handle)
31 struct gconv_step *steps;
32 size_t nsteps;
33 gconv_t result = NULL;
34 size_t cnt = 0;
35 int res;
37 res = __gconv_find_transform (toset, fromset, &steps, &nsteps);
38 if (res == GCONV_OK)
40 /* Allocate room for handle. */
41 result = (gconv_t) malloc (sizeof (struct gconv_info));
42 if (result == NULL)
43 res = GCONV_NOMEM;
44 else
46 /* Remember the list of steps. */
47 result->steps = steps;
48 result->nsteps = nsteps;
50 /* Allocate array for the step data. */
51 result->data = (struct gconv_step_data *)
52 calloc (nsteps, sizeof (struct gconv_step_data));
54 if (result->data == NULL)
55 res = GCONV_NOMEM;
56 else
58 /* Call all initialization functions for the transformation
59 step implemenations. */
60 struct gconv_step_data *data = result->data;
62 for (cnt = 0; cnt < nsteps; ++cnt)
64 /* If this is the last step we must not allocate an output
65 buffer. */
66 data[cnt].is_last = cnt == nsteps - 1;
68 /* Reset the counter. */
69 data[cnt].invocation_counter = 0;
71 /* It's a regular use. */
72 data[cnt].internal_use = 0;
74 /* We use the `mbstate_t' member in DATA. */
75 data[cnt].statep = &data[cnt].__state;
77 /* Allocate the buffer. */
78 if (!data[cnt].is_last)
80 size_t size = (GCONV_NCHAR_GOAL
81 * steps[cnt].max_needed_to);
83 data[cnt].outbuf = (char *) malloc (size);
84 if (data[cnt].outbuf == NULL)
86 res = GCONV_NOMEM;
87 break;
89 data[cnt].outbufend = data[cnt].outbuf + size;
96 if (res != GCONV_OK)
98 /* Something went wrong. Free all the resources. */
99 int serrno = errno;
101 if (result != NULL)
103 if (result->data != NULL)
105 while (cnt-- > 0)
106 free (result->data[cnt].outbuf);
108 free (result->data);
111 free (result);
112 result = NULL;
115 __gconv_close_transform (steps, nsteps);
117 __set_errno (serrno);
120 *handle = result;
121 return res;