Imported Upstream version 58.1
[platform/upstream/icu.git] / source / samples / case / case.cpp
1 /*
2 *******************************************************************************
3 *
4 *   Copyright (C) 2016 and later: Unicode, Inc. and others.
5 *   License & terms of use: http://www.unicode.org/copyright.html#License
6 *
7 *******************************************************************************
8 *******************************************************************************
9 *
10 *   Copyright (C) 2003-2004, International Business Machines
11 *   Corporation and others.  All Rights Reserved.
12 *
13 *******************************************************************************
14 */
15
16 #include <unicode/unistr.h>
17 #include <unicode/ustdio.h>
18 #include <unicode/brkiter.h>
19 #include <stdlib.h>
20
21 U_CFUNC int c_main(UFILE *out);
22
23 void printUnicodeString(UFILE *out, const UnicodeString &s) {
24     UnicodeString other = s;
25     u_fprintf(out, "\"%S\"", other.getTerminatedBuffer());
26 }
27
28
29 int main( void )
30 {
31     UFILE *out;
32     UErrorCode status  = U_ZERO_ERROR;
33     out = u_finit(stdout, NULL, NULL);
34     if(!out) {
35         fprintf(stderr, "Could not initialize (finit()) over stdout! \n");
36         return 1;
37     }
38     ucnv_setFromUCallBack(u_fgetConverter(out), UCNV_FROM_U_CALLBACK_ESCAPE,
39         NULL, NULL, NULL, &status);
40     if(U_FAILURE(status)) {
41         u_fprintf(out, "Warning- couldn't set the substitute callback - err %s\n", u_errorName(status));
42     }
43
44     /* End Demo boilerplate */
45
46     u_fprintf(out,"ICU Case Mapping Sample Program\n\n");
47     u_fprintf(out, "C++ Case Mapping\n\n");
48
49     UnicodeString string("This is a test");
50     /* lowercase = "istanbul" */ 
51     UChar lowercase[] = {0x69, 0x73, 0x74, 0x61, 0x6e, 0x62, 0x75, 0x6c, 0};
52     /* uppercase = "LATIN CAPITAL I WITH DOT ABOVE STANBUL" */  
53     UChar uppercase[] = {0x0130, 0x53, 0x54, 0x41, 0x4e, 0x42, 0x55, 0x4C, 0};
54
55     UnicodeString upper(uppercase);
56     UnicodeString lower(lowercase);
57
58     u_fprintf(out, "\nstring: ");
59     printUnicodeString(out, string);
60     string.toUpper(); /* string = "THIS IS A TEST" */
61     u_fprintf(out, "\ntoUpper(): ");
62     printUnicodeString(out, string);
63     string.toLower(); /* string = "this is a test" */
64     u_fprintf(out, "\ntoLower(): ");
65     printUnicodeString(out, string);
66
67     u_fprintf(out, "\n\nlowercase=%S, uppercase=%S\n", lowercase, uppercase);
68
69
70     string = upper; 
71     string.toLower(Locale("tr", "TR")); /* Turkish lower case map string =
72                                         lowercase */
73     u_fprintf(out, "\nupper.toLower: ");
74     printUnicodeString(out, string);
75
76     string = lower;
77     string.toUpper(Locale("tr", "TR")); /* Turkish upper case map string =
78                                         uppercase */
79     u_fprintf(out, "\nlower.toUpper: ");
80     printUnicodeString(out, string);
81
82
83     u_fprintf(out, "\nEnd C++ sample\n\n");
84
85     // Call the C version
86     int rc = c_main(out);
87     u_fclose(out);
88     return rc;
89 }
90