]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/coll/coll.cpp
1 /********************************************************************
3 * Copyright (C) 2002-2006 IBM, Inc. All Rights Reserved.
5 ********************************************************************/
8 * This program demos string collation
11 const char gHelpString
[] =
12 "usage: coll [options*] -source source_string -target target_string\n"
13 "-help Display this message.\n"
14 "-locale name ICU locale to use. Default is en_US\n"
15 "-rules rule Collation rules file (overrides locale)\n"
16 "-french French accent ordering\n"
17 "-norm Normalizing mode on\n"
18 "-shifted Shifted mode\n"
19 "-lower Lower case first\n"
20 "-upper Upper case first\n"
21 "-case Enable separate case level\n"
22 "-level n Sort level, 1 to 5, for Primary, Secndary, Tertiary, Quaternary, Identical\n"
23 "-source string Source string for comparison\n"
24 "-target string Target string for comparison\n"
25 "Example coll -rules \\u0026b\\u003ca -source a -target b\n"
26 "The format \\uXXXX is supported for the rules and comparison strings\n"
33 #include <unicode/utypes.h>
34 #include <unicode/ucol.h>
35 #include <unicode/ustring.h>
38 * Command line option variables
39 * These global variables are set according to the options specified
40 * on the command line by the user.
42 char * opt_locale
= "en_US";
44 UBool opt_help
= FALSE
;
45 UBool opt_norm
= FALSE
;
46 UBool opt_french
= FALSE
;
47 UBool opt_shifted
= FALSE
;
48 UBool opt_lower
= FALSE
;
49 UBool opt_upper
= FALSE
;
50 UBool opt_case
= FALSE
;
52 char * opt_source
= "abc";
53 char * opt_target
= "abd";
54 UCollator
* collator
= 0;
57 * Definitions for the command line options
61 enum {FLAG
, NUM
, STRING
} type
;
66 {"-locale", OptSpec::STRING
, &opt_locale
},
67 {"-rules", OptSpec::STRING
, &opt_rules
},
68 {"-source", OptSpec::STRING
, &opt_source
},
69 {"-target", OptSpec::STRING
, &opt_target
},
70 {"-norm", OptSpec::FLAG
, &opt_norm
},
71 {"-french", OptSpec::FLAG
, &opt_french
},
72 {"-shifted", OptSpec::FLAG
, &opt_shifted
},
73 {"-lower", OptSpec::FLAG
, &opt_lower
},
74 {"-upper", OptSpec::FLAG
, &opt_upper
},
75 {"-case", OptSpec::FLAG
, &opt_case
},
76 {"-level", OptSpec::NUM
, &opt_level
},
77 {"-help", OptSpec::FLAG
, &opt_help
},
78 {"-?", OptSpec::FLAG
, &opt_help
},
83 * processOptions() Function to read the command line options.
85 UBool
processOptions(int argc
, const char **argv
, OptSpec opts
[])
87 for (int argNum
= 1; argNum
< argc
; argNum
++) {
88 const char *pArgName
= argv
[argNum
];
90 for (pOpt
= opts
; pOpt
->name
!= 0; pOpt
++) {
91 if (strcmp(pOpt
->name
, pArgName
) == 0) {
94 *(UBool
*)(pOpt
->pVar
) = TRUE
;
99 fprintf(stderr
, "value expected for \"%s\" option.\n",
103 *(const char **)(pOpt
->pVar
) = argv
[argNum
];
107 if (argNum
>= argc
) {
108 fprintf(stderr
, "value expected for \"%s\" option.\n",
113 int i
= strtol(argv
[argNum
], &endp
, 0);
114 if (endp
== argv
[argNum
]) {
116 "integer value expected for \"%s\" option.\n",
120 *(int *)(pOpt
->pVar
) = i
;
127 fprintf(stderr
, "Unrecognized option \"%s\"\n", pArgName
);
135 * ICU string comparison
141 u_unescape(opt_source
, source
, 100);
142 u_unescape(opt_target
, target
, 100);
143 UCollationResult result
= ucol_strcoll(collator
, source
, -1, target
, -1);
144 if (result
== UCOL_LESS
) {
147 else if (result
== UCOL_GREATER
) {
156 UBool
processCollator()
158 // Set up an ICU collator
159 UErrorCode status
= U_ZERO_ERROR
;
162 if (opt_rules
!= 0) {
163 u_unescape(opt_rules
, rules
, 100);
164 collator
= ucol_openRules(rules
, -1, UCOL_OFF
, UCOL_TERTIARY
,
168 collator
= ucol_open(opt_locale
, &status
);
170 if (U_FAILURE(status
)) {
171 fprintf(stderr
, "Collator creation failed.: %d\n", status
);
174 if (status
== U_USING_DEFAULT_WARNING
) {
175 fprintf(stderr
, "Warning, U_USING_DEFAULT_WARNING for %s\n",
178 if (status
== U_USING_FALLBACK_WARNING
) {
179 fprintf(stderr
, "Warning, U_USING_FALLBACK_ERROR for %s\n",
183 ucol_setAttribute(collator
, UCOL_NORMALIZATION_MODE
, UCOL_ON
, &status
);
186 ucol_setAttribute(collator
, UCOL_FRENCH_COLLATION
, UCOL_ON
, &status
);
189 ucol_setAttribute(collator
, UCOL_CASE_FIRST
, UCOL_LOWER_FIRST
,
193 ucol_setAttribute(collator
, UCOL_CASE_FIRST
, UCOL_UPPER_FIRST
,
197 ucol_setAttribute(collator
, UCOL_CASE_LEVEL
, UCOL_ON
, &status
);
200 ucol_setAttribute(collator
, UCOL_ALTERNATE_HANDLING
, UCOL_SHIFTED
,
203 if (opt_level
!= 0) {
206 ucol_setAttribute(collator
, UCOL_STRENGTH
, UCOL_PRIMARY
, &status
);
209 ucol_setAttribute(collator
, UCOL_STRENGTH
, UCOL_SECONDARY
,
213 ucol_setAttribute(collator
, UCOL_STRENGTH
, UCOL_TERTIARY
, &status
);
216 ucol_setAttribute(collator
, UCOL_STRENGTH
, UCOL_QUATERNARY
,
220 ucol_setAttribute(collator
, UCOL_STRENGTH
, UCOL_IDENTICAL
,
224 fprintf(stderr
, "-level param must be between 1 and 5\n");
228 if (U_FAILURE(status
)) {
229 fprintf(stderr
, "Collator attribute setting failed.: %d\n", status
);
236 * Main -- process command line, read in and pre-process the test file,
237 * call other functions to do the actual tests.
239 int main(int argc
, const char** argv
)
241 if (processOptions(argc
, argv
, opts
) != TRUE
|| opt_help
) {
246 if (processCollator() != TRUE
) {
247 fprintf(stderr
, "Error creating collator for comparison\n");
251 fprintf(stdout
, "Comparing source=%s and target=%s\n", opt_source
,
253 int result
= strcmp();
255 fprintf(stdout
, "source is equals to target\n");
257 else if (result
< 0) {
258 fprintf(stdout
, "source is less than target\n");
261 fprintf(stdout
, "source is greater than target\n");
264 ucol_close(collator
);