source: trunk/libs/newlib/src/newlib/libc/iconv/lib/aliasesi.c @ 444

Last change on this file since 444 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 4.8 KB
Line 
1/*
2 * Copyright (c) 2003-2004, Artem B. Bityuckiy
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25#include <_ansi.h>
26#include <reent.h>
27#include <newlib.h>
28#include <sys/types.h>
29#include <string.h>
30#include <stdlib.h>
31#include <errno.h>
32#include <ctype.h>
33#include <sys/iconvnls.h>
34#include "local.h"
35
36/*
37 * canonical_form - canonize 'str'.
38 *
39 * PARAMETERS:
40 *   struct _reent *rptr - reent structure of current thread/process.
41 *   const char *str    - string to canonize.
42 *
43 * DESCRIPTION:
44 *   Converts all letters to small and substitute all '-' characters by '_'
45 *   characters.
46 *
47 * RETURN:
48 *   Returns canonical form of 'str' if success, NULL if failure.
49 */
50static const char *
51canonical_form (struct _reent *rptr,
52                       const char *str)
53{
54  char *p, *p1;
55
56  if (str == NULL || (p = p1 = _strdup_r (rptr, str)) == NULL)
57    return (const char *)NULL;
58
59  for (; *str; str++, p++)
60    {
61      if (*str == '-')
62        *p = '_';
63      else
64        *p = tolower (*str);
65    }
66
67  return (const char *)p1;
68}
69
70/*
71 * find_alias - find encoding name name by it's alias.
72 *
73 * PARAMETERS:
74 *   struct _reent *rptr - reent structure of current thread/process.
75 *   const char *alias  - alias by which "official" name should be found.
76 *   const char *table  - aliases table.
77 *   int len             - aliases table length.
78 *
79 * DESCRIPTION:
80 *   'table' contains the list of encoding names and aliases.
81 *    Names go first, e.g.:
82 *
83 *   name1 alias11 alias12 alias1N
84 *   name2 alias21 alias22 alias2N
85 *   nameM aliasM1 aliasM2 aliasMN
86 *
87 *   If line begins with backspace it is considered as the continuation of
88 *   previous line.
89 *
90 * RETURN:
91 *   Returns pointer to name found if success. In case of error returns NULL
92 *   and sets current thread's/process's errno.
93 */
94static char *
95find_alias (struct _reent *rptr,
96                   const char *alias,
97                   const char *table,
98                   int len)
99{
100  const char *end;
101  const char *p;
102  int l = strlen (alias);
103  const char *ptable = table;
104  const char *table_end = table + len;
105
106  if (table == NULL || alias == NULL || *table == '\0' || *alias == '\0')
107    return NULL;
108
109search_again:
110  if (len < l || (p = strnstr (ptable, alias, len)) == NULL)
111    return NULL;
112 
113  /* Check that substring is segregated by '\n', '\t' or ' ' */
114  if (!((p == table || isspace (*(p-1)) || *(p-1) == '\n')
115     && (p+l == table_end || isspace (*(p+l)) || *(p+l) == '\n')))
116    {
117      ptable = p + l;
118      len -= table - p;
119      goto search_again;
120    }
121
122  while(--p > table && *p != '\n');
123
124  if (*(++p) == '#')
125    return NULL;
126
127  for (end = p + 1; !isspace (*end) && *end != '\n' && *end != '\0'; end++);
128
129  return _strndup_r (rptr, p, (size_t)(end - p));
130}
131
132/*
133 * _iconv_resolve_encoding_name - resolves encoding's name by given alias.
134 *
135 * PARAMETERS:
136 *   struct _reent *rptr - reent structure of current thread/process.
137 *   const char *ca     - encoding alias to resolve.
138 *
139 * DESCRIPTION:
140 *   First, tries to find 'ca' among built-in aliases. If not found, tries to
141 *   find it external file.
142 *
143 * RETURN:
144 *   Encoding name if found. In case of error returns NULL
145 *   and sets current thread's/process's errno.
146 */
147char *
148_iconv_resolve_encoding_name (struct _reent *rptr,
149                                     const char *ca)
150{
151  char *p = (char *)ca;
152
153  /* Alias shouldn't contain white spaces, '\n' and '\r' symbols */ 
154  while (*p)
155    if (*p == ' ' || *p == '\r' || *p++ == '\n')
156      return NULL;
157   
158  if ((ca = canonical_form (rptr, ca)) == NULL)
159    return NULL;
160
161  p = find_alias (rptr, ca, _iconv_aliases, strlen (_iconv_aliases));
162 
163  _free_r (rptr, (void *)ca);
164  return p;
165}
166
Note: See TracBrowser for help on using the repository browser.