Ticket #2041: mbrtowc_test.c

File mbrtowc_test.c, 2.7 KB (added by mjw, 16 years ago)

A test case that can be made to fail or succeed.

Line 
1/*
2$File: LEGAL.NOTICE,v 1.15 2006/05/03 18:48:33 christos Exp $
3Copyright (c) Ian F. Darwin 1986, 1987, 1989, 1990, 1991, 1992, 1994, 1995.
4Software written by Ian F. Darwin and others;
5maintained 1994- Christos Zoulas.
6
7This software is not subject to any export provision of the United States
8Department of Commerce, and may be exported to any country or planet.
9
10Redistribution and use in source and binary forms, with or without
11modification, are permitted provided that the following conditions
12are met:
131. Redistributions of source code must retain the above copyright
14 notice immediately at the beginning of the file, without modification,
15 this list of conditions, and the following disclaimer.
162. Redistributions in binary form must reproduce the above copyright
17 notice, this list of conditions and the following disclaimer in the
18 documentation and/or other materials provided with the distribution.
19
20THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
24ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30SUCH DAMAGE.
31*/
32
33
34
35// Uncomment these lines to make the test work
36
37// #ifdef __WCHAR_TYPE__
38// #undef __WCHAR_TYPE__
39// #define __WCHAR_TYPE__ unsigned int
40// #endif
41#include <stdio.h>
42#include <wchar.h>
43
44size_t
45file_mbswidth(const char *s)
46{
47 size_t bytesconsumed, old_n, n, width = 0;
48 mbstate_t state;
49 wchar_t nextchar;
50 (void)memset(&state, 0, sizeof(mbstate_t));
51 old_n = n = strlen(s);
52
53 while (n > 0) {
54 bytesconsumed = mbrtowc(&nextchar, s, n, &state);
55 printf("mjw... in while %d\n", bytesconsumed);
56 if (bytesconsumed == (size_t)(-1) ||
57 bytesconsumed == (size_t)(-2)) {
58 /* Something went wrong, return something reasonable */
59 return old_n;
60 }
61 if (s[0] == '\n') {
62 /*
63 * do what strlen() would do, so that caller
64 * is always right
65 */
66 width++;
67 } else
68 width += wcwidth(nextchar);
69
70 s += bytesconsumed, n -= bytesconsumed;
71 }
72 return width;
73}
74
75int
76main(int argc, char *argv[])
77{
78 wchar_t t;
79 printf("%d, %d\n", sizeof(wchar_t), sizeof(t));
80 if (sizeof(wchar_t) != 4)
81 {
82 printf("Error: glibc expects sizeof(wchar_t) to be 4\n");
83 }
84 printf("%d\n", file_mbswidth("etc/magic"));
85
86 return 0;
87}
88