annotate lib/wcsxfrm-impl.h @ 17480:f40b3156a43e

selinux-at: omit unnecessary include * lib/selinux-at.c: Don't include dosname.h; not needed, since this source file doesn't use its macros, and subsidiary files that use the macros already include it.
author Paul Eggert <eggert@cs.ucla.edu>
date Fri, 23 Aug 2013 13:53:46 -0700
parents e542fd46ad6f
children 344018b6e5d7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14294
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Transform wide string for comparison using the current locale.
17249
e542fd46ad6f maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents: 16201
diff changeset
2 Copyright (C) 2011-2013 Free Software Foundation, Inc.
14294
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <bruno@clisp.org>, 2011.
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 This program is free software: you can redistribute it and/or modify
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 (at your option) any later version.
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 size_t
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 wcsxfrm (wchar_t *s1, const wchar_t *s2, size_t n)
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 {
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 char mbbuf2[1024];
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 char *mbs2;
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 {
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 int saved_errno = errno;
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 size_t result;
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 /* Convert s2 to a multibyte string, trying to avoid malloc(). */
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 {
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 size_t ret;
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 ret = wcstombs (mbbuf2, s2, sizeof (mbbuf2));
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 if (ret == (size_t)-1)
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 goto failed;
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 if (ret < sizeof (mbbuf2))
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 mbs2 = mbbuf2;
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 else
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 {
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 size_t need = wcstombs (NULL, s2, 0);
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 if (need == (size_t)-1)
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 goto failed;
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 mbs2 = (char *) malloc (need + 1);
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 if (mbs2 == NULL)
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 goto out_of_memory;
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 ret = wcstombs (mbs2, s2, need + 1);
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 if (ret != need)
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 abort ();
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 }
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 }
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 /* Transform the multibyte string. */
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 errno = 0;
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 result = strxfrm ((char *)s1, mbs2, n);
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 if (errno != 0)
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 {
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 /* An error occurred. */
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 if (mbs2 != mbbuf2)
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 {
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 saved_errno = errno;
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 free (mbs2);
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 errno = saved_errno;
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 }
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 return 0;
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 }
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 if (result < n)
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 {
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 /* Convert the result by mapping char[] -> wchar_t[].
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 Since strcmp() compares the elements as 'unsigned char' values,
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 whereas wcscmp() compares the elements as 'wchar_t' values, we need
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 to map 1 'unsigned char' to 1 'wchar_t'. (We could also map 2
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 consecutive 'unsigned char' values to 1 'wchar_t' value, but this is
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 not needed. */
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 wchar_t *wcp = s1 + n;
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 char *cp = (char *)s1 + n;
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 while (wcp > s1)
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 *--wcp = (wchar_t) (unsigned char) *--cp;
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 }
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 if (mbs2 != mbbuf2)
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 free (mbs2);
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 /* No error. */
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 errno = saved_errno;
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 return result;
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 }
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 out_of_memory:
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 errno = ENOMEM;
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 return 0;
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 failed:
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 errno = EILSEQ;
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 return 0;
b19db9397852 New module 'wcsxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 }