1 /*
2 *******************************************************************************
3 *
4 *   Copyright (C) 2003-2004, International Business Machines
5 *   Corporation and others.  All Rights Reserved.
6 *
7 ****
8 *
9 *   Case folding examples, in C
10 *
11 *******************************************************************************
12 */
13 
14 #include "unicode/uchar.h"
15 #include "unicode/ustring.h"
16 #include "unicode/utypes.h"
17 #include "unicode/ustdio.h"
18 
19 /* Note: don't use 'k' or 'K' because we might be on EBCDIC */
20 
c_main(UFILE * out)21 int c_main(UFILE *out)
22 {
23   UChar32 ch;
24   UErrorCode errorCode = U_ZERO_ERROR;
25   static const UChar upper[] = {0x61, 0x42, 0x49, 0}; /* upper = "aBI" */
26   static const UChar lower[] = {0x61, 0x42, 0x69, 0}; /* lower = "abi" */
27   /* unfold = "aB LATIN SMALL LETTER DOTLESS I" */
28   static const UChar unfold[] = {0x61, 0x42, 0x131, 0} ;
29   UChar buffer[32];
30   const UChar char_k = 0x006b; /* 'k' */
31   const UChar char_K = 0x004b; /* 'K' */
32 
33   int length;
34 
35   printf("** C Case Mapping Sample\n");
36 
37   /* uchar.h APIs, single character case mapping */
38   ch = u_toupper(char_k); /* ch = 'K' */
39   u_fprintf(out, "toupper(%C) = %C\n", char_k, ch);
40   ch = u_tolower(ch); /* ch = 'k' */
41   u_fprintf(out, "tolower() = %C\n", ch);
42   ch = u_totitle(char_k); /* ch = 'K' */
43   u_fprintf(out, "totitle(%C) = %C\n", char_k, ch);
44   ch = u_foldCase(char_K, U_FOLD_CASE_DEFAULT); /* ch = 'k' */
45   u_fprintf(out, "u_foldCase(%C, U_FOLD_CASE_DEFAULT) = %C\n", char_K, (UChar) ch);
46 
47   /* ustring.h APIs, UChar * string case mapping with a Turkish locale */
48   /* result buffer = "ab?" latin small letter a, latin small letter b, latin
49      small letter dotless i */
50   length = u_strToLower(buffer, sizeof(buffer)/sizeof(buffer[0]), upper,
51                         sizeof(upper)/sizeof(upper[0]), "tr", &errorCode);
52   if(U_FAILURE(errorCode) || buffer[length]!=0) {
53     u_fprintf(out, "error in u_strToLower(Turkish locale)=%ld error=%s\n", length,
54            u_errorName(errorCode));
55   }
56 
57   u_fprintf(out, "u_strToLower(%S, turkish) -> %S\n", upper, buffer);
58 
59 
60   /* ustring.h APIs, UChar * string case mapping with a Engish locale */
61   /* result buffer = "ABI" latin CAPITAL letter A, latin capital letter B,
62      latin capital letter I */
63   length = u_strToUpper(buffer, sizeof(buffer)/sizeof(buffer[0]), upper,
64                         sizeof(upper)/sizeof(upper[0]), "en", &errorCode);
65   if(U_FAILURE(errorCode) || buffer[length]!=0) {
66     u_fprintf(out, "error in u_strToLower(English locale)=%ld error=%s\n", length,
67               u_errorName(errorCode));
68   }
69   u_fprintf(out, "u_strToUpper(%S, english) -> %S\n", lower, buffer);
70 
71 
72   /* ustring.h APIs, UChar * string case folding */
73   /* result buffer = "abi" */
74   length = u_strFoldCase(buffer, sizeof(buffer)/sizeof(buffer[0]), unfold,
75                          sizeof(unfold)/sizeof(unfold[0]), U_FOLD_CASE_DEFAULT,
76                          &errorCode);
77   if(U_FAILURE(errorCode) || buffer[length]!=0) {
78     u_fprintf(out, "error in u_strFoldCase()=%ld error=%s\n", length,
79            u_errorName(errorCode));
80   }
81   u_fprintf(out, "u_strFoldCase(%S, U_FOLD_CASE_DEFAULT) -> %S\n", unfold, buffer);
82   u_fprintf(out, "\n** end of C sample\n");
83 
84   return 0;
85 }
86