OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [newlib-1.18.0/] [newlib/] [libc/] [iconv/] [lib/] [aliasesi.c] - Blame information for rev 841

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 207 jeremybenn
/*
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
 * strnstr - locate a substring in a fixed-size string.
38
 *
39
 * PARAMETERS:
40
 *   _CONST char *haystack - the string in which to search.
41
 *   _CONST char *needle   - the string which to search.
42
 *   int length            - the maximum 'haystack' string length.
43
 *
44
 * DESCRIPTION:
45
 *   The  strstr() function finds the first occurrence of the substring
46
 *   'needle' in the string 'haystack'. At most 'length' bytes are searched.
47
 *
48
 * RETURN:
49
 *   Returns a pointer to the beginning of substring, or NULL if substring
50
 *   was not found.
51
 */
52
static char *
53
_DEFUN(strnstr, (haystack, needle, length),
54
                _CONST char *haystack _AND
55
                _CONST char *needle   _AND
56
                int length)
57
{
58
  _CONST char *max = haystack + length;
59
 
60
  if (*haystack == '\0')
61
    return *needle == '\0' ? (char *)haystack : (char *)NULL;
62
 
63
  while (haystack < max)
64
    {
65
      int i = 0;
66
      while (1)
67
        {
68
          if (needle[i] == '\0')
69
            return (char *)haystack;
70
          if (needle[i] != haystack[i])
71
            break;
72
          i += 1;
73
        }
74
      haystack += 1;
75
    }
76
  return (char *)NULL;
77
}
78
 
79
/*
80
 * canonical_form - canonize 'str'.
81
 *
82
 * PARAMETERS:
83
 *   struct _reent *rptr - reent structure of current thread/process.
84
 *   _CONST char *str    - string to canonize.
85
 *
86
 * DESCRIPTION:
87
 *   Converts all letters to small and substitute all '-' characters by '_'
88
 *   characters.
89
 *
90
 * RETURN:
91
 *   Returns canonical form of 'str' if success, NULL if failure.
92
 */
93
static _CONST char *
94
_DEFUN(canonical_form, (rptr, str),
95
                       struct _reent *rptr _AND
96
                       _CONST char *str)
97
{
98
  char *p, *p1;
99
 
100
  if (str == NULL || (p = p1 = _strdup_r (rptr, str)) == NULL)
101
    return (_CONST char *)NULL;
102
 
103
  for (; *str; str++, p++)
104
    {
105
      if (*str == '-')
106
        *p = '_';
107
      else
108
        *p = tolower (*str);
109
    }
110
 
111
  return (_CONST char *)p1;
112
}
113
 
114
/*
115
 * find_alias - find encoding name name by it's alias.
116
 *
117
 * PARAMETERS:
118
 *   struct _reent *rptr - reent structure of current thread/process.
119
 *   _CONST char *alias  - alias by which "official" name should be found.
120
 *   _CONST char *table  - aliases table.
121
 *   int len             - aliases table length.
122
 *
123
 * DESCRIPTION:
124
 *   'table' contains the list of encoding names and aliases.
125
 *    Names go first, e.g.:
126
 *
127
 *   name1 alias11 alias12 alias1N
128
 *   name2 alias21 alias22 alias2N
129
 *   nameM aliasM1 aliasM2 aliasMN
130
 *
131
 *   If line begins with backspace it is considered as the continuation of
132
 *   previous line.
133
 *
134
 * RETURN:
135
 *   Returns pointer to name found if success. In case of error returns NULL
136
 *   and sets current thread's/process's errno.
137
 */
138
static char *
139
_DEFUN(find_alias, (rptr, alias, table, len),
140
                   struct _reent *rptr _AND
141
                   _CONST char *alias  _AND
142
                   _CONST char *table  _AND
143
                   int len)
144
{
145
  _CONST char *end;
146
  _CONST char *p;
147
  int l = strlen (alias);
148
  _CONST char *ptable = table;
149
  _CONST char *table_end = table + len;
150
 
151
  if (table == NULL || alias == NULL || *table == '\0' || *alias == '\0')
152
    return NULL;
153
 
154
search_again:
155
  if (len < l || (p = strnstr (ptable, alias, len)) == NULL)
156
    return NULL;
157
 
158
  /* Check that substring is segregated by '\n', '\t' or ' ' */
159
  if (!((p == table || isspace (*(p-1)) || *(p-1) == '\n')
160
     && (p+l == table_end || isspace (*(p+l)) || *(p+l) == '\n')))
161
    {
162
      ptable = p + l;
163
      len -= table - p;
164
      goto search_again;
165
    }
166
 
167
  while(--p > table && *p != '\n');
168
 
169
  if (*(++p) == '#')
170
    return NULL;
171
 
172
  for (end = p + 1; !isspace (*end) && *end != '\n' && *end != '\0'; end++);
173
 
174
  return _strndup_r (rptr, p, (size_t)(end - p));
175
}
176
 
177
/*
178
 * _iconv_resolve_encoding_name - resolves encoding's name by given alias.
179
 *
180
 * PARAMETERS:
181
 *   struct _reent *rptr - reent structure of current thread/process.
182
 *   _CONST char *ca     - encoding alias to resolve.
183
 *
184
 * DESCRIPTION:
185
 *   First, tries to find 'ca' among built-in aliases. If not found, tries to
186
 *   find it external file.
187
 *
188
 * RETURN:
189
 *   Encoding name if found. In case of error returns NULL
190
 *   and sets current thread's/process's errno.
191
 */
192
char *
193
_DEFUN(_iconv_resolve_encoding_name, (rptr, cname, path),
194
                                     struct _reent *rptr _AND
195
                                     _CONST char *ca)
196
{
197
  char *p = (char *)ca;
198
 
199
  /* Alias shouldn't contain white spaces, '\n' and '\r' symbols */
200
  while (*p)
201
    if (*p == ' ' || *p == '\r' || *p++ == '\n')
202
      return NULL;
203
 
204
  if ((ca = canonical_form (rptr, ca)) == NULL)
205
    return NULL;
206
 
207
  p = find_alias (rptr, ca, _iconv_aliases, strlen (_iconv_aliases));
208
 
209
  _free_r (rptr, (_VOID_PTR)ca);
210
  return p;
211
}
212
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.