]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/coll/coll.cpp
1 /********************************************************************
3 * Copyright (C) 2002-2003 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
];
89 for (OptSpec
*pOpt
= opts
; pOpt
->name
!= 0; pOpt
++) {
90 if (strcmp(pOpt
->name
, pArgName
) == 0) {
93 *(UBool
*)(pOpt
->pVar
) = TRUE
;
98 fprintf(stderr
, "value expected for \"%s\" option.\n",
102 *(const char **)(pOpt
->pVar
) = argv
[argNum
];
106 if (argNum
>= argc
) {
107 fprintf(stderr
, "value expected for \"%s\" option.\n",
112 int i
= strtol(argv
[argNum
], &endp
, 0);
113 if (endp
== argv
[argNum
]) {
115 "integer value expected for \"%s\" option.\n",
119 *(int *)(pOpt
->pVar
) = i
;
126 fprintf(stderr
, "Unrecognized option \"%s\"\n", pArgName
);
134 * ICU string comparison
140 u_unescape(opt_source
, source
, 100);
141 u_unescape(opt_target
, target
, 100);
142 UCollationResult result
= ucol_strcoll(collator
, source
, -1, target
, -1);
143 if (result
== UCOL_LESS
) {
146 else if (result
== UCOL_GREATER
) {
155 UBool
processCollator()
157 // Set up an ICU collator
158 UErrorCode status
= U_ZERO_ERROR
;
161 if (opt_rules
!= 0) {
162 u_unescape(opt_rules
, rules
, 100);
163 collator
= ucol_openRules(rules
, -1, UCOL_OFF
, UCOL_TERTIARY
,
167 collator
= ucol_open(opt_locale
, &status
);
169 if (U_FAILURE(status
)) {
170 fprintf(stderr
, "Collator creation failed.: %d\n", status
);
173 if (status
== U_USING_DEFAULT_WARNING
) {
174 fprintf(stderr
, "Warning, U_USING_DEFAULT_WARNING for %s\n",
177 if (status
== U_USING_FALLBACK_WARNING
) {
178 fprintf(stderr
, "Warning, U_USING_FALLBACK_ERROR for %s\n",
182 ucol_setAttribute(collator
, UCOL_NORMALIZATION_MODE
, UCOL_ON
, &status
);
185 ucol_setAttribute(collator
, UCOL_FRENCH_COLLATION
, UCOL_ON
, &status
);
188 ucol_setAttribute(collator
, UCOL_CASE_FIRST
, UCOL_LOWER_FIRST
,
192 ucol_setAttribute(collator
, UCOL_CASE_FIRST
, UCOL_UPPER_FIRST
,
196 ucol_setAttribute(collator
, UCOL_CASE_LEVEL
, UCOL_ON
, &status
);
199 ucol_setAttribute(collator
, UCOL_ALTERNATE_HANDLING
, UCOL_SHIFTED
,
202 if (opt_level
!= 0) {
205 ucol_setAttribute(collator
, UCOL_STRENGTH
, UCOL_PRIMARY
, &status
);
208 ucol_setAttribute(collator
, UCOL_STRENGTH
, UCOL_SECONDARY
,
212 ucol_setAttribute(collator
, UCOL_STRENGTH
, UCOL_TERTIARY
, &status
);
215 ucol_setAttribute(collator
, UCOL_STRENGTH
, UCOL_QUATERNARY
,
219 ucol_setAttribute(collator
, UCOL_STRENGTH
, UCOL_IDENTICAL
,
223 fprintf(stderr
, "-level param must be between 1 and 5\n");
227 if (U_FAILURE(status
)) {
228 fprintf(stderr
, "Collator attribute setting failed.: %d\n", status
);
235 * Main -- process command line, read in and pre-process the test file,
236 * call other functions to do the actual tests.
238 int main(int argc
, const char** argv
)
240 if (processOptions(argc
, argv
, opts
) != TRUE
|| opt_help
) {
245 if (processCollator() != TRUE
) {
246 fprintf(stderr
, "Error creating collator for comparison\n");
250 fprintf(stdout
, "Comparing source=%s and target=%s\n", opt_source
,
252 int result
= strcmp();
254 fprintf(stdout
, "source is equals to target\n");
256 else if (result
< 0) {
257 fprintf(stdout
, "source is less than target\n");
260 fprintf(stdout
, "source is greater than target\n");
263 ucol_close(collator
);