annotate lib/mbsrtowcs-impl.h @ 14379:2330aac2ae54

maint: adjust cpp indentation to reflect nesting depth I.e., in a block of code that begins with an unnested "#if", put one space between the "#" in column 1 and following token. For example, -#include <sys/vfs.h> +# include <sys/vfs.h> Do this only in .c files that are part of a module I maintain. * lib/linkat.c: Filter through cppi. * lib/nanosleep.c: Likewise. * lib/openat.c: Likewise. * lib/openat-die.c: Likewise. * lib/dup3.c: Likewise. * lib/fchownat.c: Likewise. * lib/flock.c: Likewise. * lib/fsync.c: Likewise. * lib/fts.c: Likewise. * lib/getpass.c: Likewise. * lib/gettimeofday.c: Likewise. * lib/userspec.c: Likewise. * Makefile (sc_cpp_indent_check): New rule, to check this.
author Jim Meyering <meyering@redhat.com>
date Sun, 20 Feb 2011 23:02:43 +0100
parents 758fb75c3064
children 8250f2777afc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14261
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Convert string to wide string.
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
2 Copyright (C) 2008-2011 Free Software Foundation, Inc.
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <bruno@clisp.org>, 2008.
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 This program is free software: you can redistribute it and/or modify
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 (at your option) any later version.
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 size_t
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 mbsrtowcs (wchar_t *dest, const char **srcp, size_t len, mbstate_t *ps)
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 {
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 if (ps == NULL)
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 ps = &_gl_mbsrtowcs_state;
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 {
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 const char *src = *srcp;
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 if (dest != NULL)
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 {
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 wchar_t *destptr = dest;
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 for (; len > 0; destptr++, len--)
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 {
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 size_t src_avail;
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 size_t ret;
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 /* An optimized variant of
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 src_avail = strnlen1 (src, MB_LEN_MAX); */
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 if (src[0] == '\0')
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 src_avail = 1;
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 else if (src[1] == '\0')
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 src_avail = 2;
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 else if (src[2] == '\0')
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 src_avail = 3;
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 else if (MB_LEN_MAX <= 4 || src[3] == '\0')
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 src_avail = 4;
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 else
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 src_avail = 4 + strnlen1 (src + 4, MB_LEN_MAX - 4);
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 /* Parse the next multibyte character. */
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 ret = mbrtowc (destptr, src, src_avail, ps);
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 if (ret == (size_t)(-2))
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 /* Encountered a multibyte character that extends past a '\0' byte
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 or that is longer than MB_LEN_MAX bytes. Cannot happen. */
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 abort ();
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 if (ret == (size_t)(-1))
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 goto bad_input;
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 if (ret == 0)
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 {
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 src = NULL;
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 /* Here mbsinit (ps). */
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 break;
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 }
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 src += ret;
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 }
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 *srcp = src;
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 return destptr - dest;
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 }
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 else
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 {
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 /* Ignore dest and len, don't store *srcp at the end, and
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 don't clobber *ps. */
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 mbstate_t state = *ps;
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 size_t totalcount = 0;
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 for (;; totalcount++)
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 {
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 size_t src_avail;
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 size_t ret;
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 /* An optimized variant of
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 src_avail = strnlen1 (src, MB_LEN_MAX); */
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 if (src[0] == '\0')
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 src_avail = 1;
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 else if (src[1] == '\0')
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 src_avail = 2;
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 else if (src[2] == '\0')
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 src_avail = 3;
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 else if (MB_LEN_MAX <= 4 || src[3] == '\0')
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 src_avail = 4;
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 else
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 src_avail = 4 + strnlen1 (src + 4, MB_LEN_MAX - 4);
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 /* Parse the next multibyte character. */
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 ret = mbrtowc (NULL, src, src_avail, &state);
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 if (ret == (size_t)(-2))
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 /* Encountered a multibyte character that extends past a '\0' byte
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 or that is longer than MB_LEN_MAX bytes. Cannot happen. */
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 abort ();
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 if (ret == (size_t)(-1))
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 goto bad_input2;
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 if (ret == 0)
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 {
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 /* Here mbsinit (&state). */
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108 break;
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 }
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 src += ret;
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 }
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 return totalcount;
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 }
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116 bad_input:
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 *srcp = src;
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
118 bad_input2:
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119 errno = EILSEQ;
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120 return (size_t)(-1);
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 }
758fb75c3064 mbsrtowcs: Prepare for new module mbsrtowwcs.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122 }