]> git.saurik.com Git - apple/icu.git/blob - icuSources/samples/coll/coll.cpp
ICU-6.2.8.tar.gz
[apple/icu.git] / icuSources / samples / coll / coll.cpp
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (C) 2002-2003 IBM, Inc. All Rights Reserved.
4 *
5 ********************************************************************/
6
7 /**
8 * This program demos string collation
9 */
10
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"
27 ;
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32
33 #include <unicode/utypes.h>
34 #include <unicode/ucol.h>
35 #include <unicode/ustring.h>
36
37 /**
38 * Command line option variables
39 * These global variables are set according to the options specified
40 * on the command line by the user.
41 */
42 char * opt_locale = "en_US";
43 char * opt_rules = 0;
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;
51 int opt_level = 0;
52 char * opt_source = "abc";
53 char * opt_target = "abd";
54 UCollator * collator = 0;
55
56 /**
57 * Definitions for the command line options
58 */
59 struct OptSpec {
60 const char *name;
61 enum {FLAG, NUM, STRING} type;
62 void *pVar;
63 };
64
65 OptSpec opts[] = {
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},
79 {0, OptSpec::FLAG, 0}
80 };
81
82 /**
83 * processOptions() Function to read the command line options.
84 */
85 UBool processOptions(int argc, const char **argv, OptSpec opts[])
86 {
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) {
91 switch (pOpt->type) {
92 case OptSpec::FLAG:
93 *(UBool *)(pOpt->pVar) = TRUE;
94 break;
95 case OptSpec::STRING:
96 argNum ++;
97 if (argNum >= argc) {
98 fprintf(stderr, "value expected for \"%s\" option.\n",
99 pOpt->name);
100 return FALSE;
101 }
102 *(const char **)(pOpt->pVar) = argv[argNum];
103 break;
104 case OptSpec::NUM:
105 argNum ++;
106 if (argNum >= argc) {
107 fprintf(stderr, "value expected for \"%s\" option.\n",
108 pOpt->name);
109 return FALSE;
110 }
111 char *endp;
112 int i = strtol(argv[argNum], &endp, 0);
113 if (endp == argv[argNum]) {
114 fprintf(stderr,
115 "integer value expected for \"%s\" option.\n",
116 pOpt->name);
117 return FALSE;
118 }
119 *(int *)(pOpt->pVar) = i;
120 }
121 break;
122 }
123 }
124 if (pOpt->name == 0)
125 {
126 fprintf(stderr, "Unrecognized option \"%s\"\n", pArgName);
127 return FALSE;
128 }
129 }
130 return TRUE;
131 }
132
133 /**
134 * ICU string comparison
135 */
136 int strcmp()
137 {
138 UChar source[100];
139 UChar target[100];
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) {
144 return -1;
145 }
146 else if (result == UCOL_GREATER) {
147 return 1;
148 }
149 return 0;
150 }
151
152 /**
153 * Creates a collator
154 */
155 UBool processCollator()
156 {
157 // Set up an ICU collator
158 UErrorCode status = U_ZERO_ERROR;
159 UChar rules[100];
160
161 if (opt_rules != 0) {
162 u_unescape(opt_rules, rules, 100);
163 collator = ucol_openRules(rules, -1, UCOL_OFF, UCOL_TERTIARY,
164 NULL, &status);
165 }
166 else {
167 collator = ucol_open(opt_locale, &status);
168 }
169 if (U_FAILURE(status)) {
170 fprintf(stderr, "Collator creation failed.: %d\n", status);
171 return FALSE;
172 }
173 if (status == U_USING_DEFAULT_WARNING) {
174 fprintf(stderr, "Warning, U_USING_DEFAULT_WARNING for %s\n",
175 opt_locale);
176 }
177 if (status == U_USING_FALLBACK_WARNING) {
178 fprintf(stderr, "Warning, U_USING_FALLBACK_ERROR for %s\n",
179 opt_locale);
180 }
181 if (opt_norm) {
182 ucol_setAttribute(collator, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
183 }
184 if (opt_french) {
185 ucol_setAttribute(collator, UCOL_FRENCH_COLLATION, UCOL_ON, &status);
186 }
187 if (opt_lower) {
188 ucol_setAttribute(collator, UCOL_CASE_FIRST, UCOL_LOWER_FIRST,
189 &status);
190 }
191 if (opt_upper) {
192 ucol_setAttribute(collator, UCOL_CASE_FIRST, UCOL_UPPER_FIRST,
193 &status);
194 }
195 if (opt_case) {
196 ucol_setAttribute(collator, UCOL_CASE_LEVEL, UCOL_ON, &status);
197 }
198 if (opt_shifted) {
199 ucol_setAttribute(collator, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED,
200 &status);
201 }
202 if (opt_level != 0) {
203 switch (opt_level) {
204 case 1:
205 ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_PRIMARY, &status);
206 break;
207 case 2:
208 ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_SECONDARY,
209 &status);
210 break;
211 case 3:
212 ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_TERTIARY, &status);
213 break;
214 case 4:
215 ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_QUATERNARY,
216 &status);
217 break;
218 case 5:
219 ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_IDENTICAL,
220 &status);
221 break;
222 default:
223 fprintf(stderr, "-level param must be between 1 and 5\n");
224 return FALSE;
225 }
226 }
227 if (U_FAILURE(status)) {
228 fprintf(stderr, "Collator attribute setting failed.: %d\n", status);
229 return FALSE;
230 }
231 return TRUE;
232 }
233
234 /**
235 * Main -- process command line, read in and pre-process the test file,
236 * call other functions to do the actual tests.
237 */
238 int main(int argc, const char** argv)
239 {
240 if (processOptions(argc, argv, opts) != TRUE || opt_help) {
241 printf(gHelpString);
242 return -1;
243 }
244
245 if (processCollator() != TRUE) {
246 fprintf(stderr, "Error creating collator for comparison\n");
247 return -1;
248 }
249
250 fprintf(stdout, "Comparing source=%s and target=%s\n", opt_source,
251 opt_target);
252 int result = strcmp();
253 if (result == 0) {
254 fprintf(stdout, "source is equals to target\n");
255 }
256 else if (result < 0) {
257 fprintf(stdout, "source is less than target\n");
258 }
259 else {
260 fprintf(stdout, "source is greater than target\n");
261 }
262
263 ucol_close(collator);
264 return 0;
265 }