]> git.saurik.com Git - apple/icu.git/blob - icuSources/tools/toolutil/udbgutil.cpp
ICU-66108.tar.gz
[apple/icu.git] / icuSources / tools / toolutil / udbgutil.cpp
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
4 * COPYRIGHT:
5 * Copyright (c) 2007-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
8
9 #include "udbgutil.h"
10 #include <string.h>
11 #include <stdio.h>
12 #include "ustr_imp.h"
13 #include "cmemory.h"
14 #include "cstring.h"
15 #include "putilimp.h"
16 #include "unicode/ulocdata.h"
17 #include "unicode/ucnv.h"
18 #include "unicode/unistr.h"
19 #include "unicode/ucol.h"
20 #include "cstr.h"
21
22 /*
23 To add a new enum type
24 (For example: UShoeSize with values USHOE_WIDE=0, USHOE_REGULAR, USHOE_NARROW, USHOE_COUNT)
25
26 0. Make sure that all lines you add are protected with appropriate uconfig guards,
27 such as '#if !UCONFIG_NO_SHOES'.
28 1. udbgutil.h: add UDBG_UShoeSize to the UDebugEnumType enum before UDBG_ENUM_COUNT
29 ( The subsequent steps involve this file, udbgutil.cpp )
30 2. Find the marker "Add new enum types above this line"
31 3. Before that marker, add a #include of any header file you need.
32 4. Each enum type has three things in this section: a #define, a count_, and an array of Fields.
33 It may help to copy and paste a previous definition.
34 5. In the case of the USHOE_... strings above, "USHOE_" is common to all values- six characters
35 " #define LEN_USHOE 6 "
36 6 characters will strip off "USHOE_" leaving enum values of WIDE, REGULAR, and NARROW.
37 6. Define the 'count_' variable, with the number of enum values. If the enum has a _MAX or _COUNT value,
38 that can be helpful for automatically defining the count. Otherwise define it manually.
39 " static const int32_t count_UShoeSize = USHOE_COUNT; "
40 7. Define the field names, in order.
41 " static const Field names_UShoeSize[] = {
42 " FIELD_NAME_STR( LEN_USHOE, USHOE_WIDE ),
43 " FIELD_NAME_STR( LEN_USHOE, USHOE_REGULAR ),
44 " FIELD_NAME_STR( LEN_USHOE, USHOE_NARROW ),
45 " };
46 ( The following command was usedfor converting ucol.h into partially correct entities )
47 grep "^[ ]*UCOL" < unicode/ucol.h |
48 sed -e 's%^[ ]*\([A-Z]*\)_\([A-Z_]*\).*% FIELD_NAME_STR( LEN_\1, \1_\2 ),%g'
49 8. Now, a bit farther down, add the name of the enum itself to the end of names_UDebugEnumType
50 ( UDebugEnumType is an enum, too!)
51 names_UDebugEnumType[] { ...
52 " FIELD_NAME_STR( LEN_UDBG, UDBG_UShoeSize ), "
53 9. Find the function _udbg_enumCount and add the count macro:
54 " COUNT_CASE(UShoeSize)
55 10. Find the function _udbg_enumFields and add the field macro:
56 " FIELD_CASE(UShoeSize)
57 11. verify that your test code, and Java data generation, works properly.
58 */
59
60 /**
61 * Structure representing an enum value
62 */
63 struct Field {
64 int32_t prefix; /**< how many characters to remove in the prefix - i.e. UCHAR_ = 5 */
65 const char *str; /**< The actual string value */
66 int32_t num; /**< The numeric value */
67 };
68
69 /**
70 * Define another field name. Used in an array of Field s
71 * @param y the common prefix length (i.e. 6 for "USHOE_" )
72 * @param x the actual enum value - it will be copied in both string and symbolic form.
73 * @see Field
74 */
75 #define FIELD_NAME_STR(y,x) { y, #x, x }
76
77
78 // TODO: Currently, this whole functionality goes away with UCONFIG_NO_FORMATTING. Should be split up.
79 #if !UCONFIG_NO_FORMATTING
80
81 // Calendar
82 #include "unicode/ucal.h"
83
84 // 'UCAL_' = 5
85 #define LEN_UCAL 5 /* UCAL_ */
86 static const int32_t count_UCalendarDateFields = UCAL_FIELD_COUNT;
87 static const Field names_UCalendarDateFields[] =
88 {
89 FIELD_NAME_STR( LEN_UCAL, UCAL_ERA ),
90 FIELD_NAME_STR( LEN_UCAL, UCAL_YEAR ),
91 FIELD_NAME_STR( LEN_UCAL, UCAL_MONTH ),
92 FIELD_NAME_STR( LEN_UCAL, UCAL_WEEK_OF_YEAR ),
93 FIELD_NAME_STR( LEN_UCAL, UCAL_WEEK_OF_MONTH ),
94 FIELD_NAME_STR( LEN_UCAL, UCAL_DATE ),
95 FIELD_NAME_STR( LEN_UCAL, UCAL_DAY_OF_YEAR ),
96 FIELD_NAME_STR( LEN_UCAL, UCAL_DAY_OF_WEEK ),
97 FIELD_NAME_STR( LEN_UCAL, UCAL_DAY_OF_WEEK_IN_MONTH ),
98 FIELD_NAME_STR( LEN_UCAL, UCAL_AM_PM ),
99 FIELD_NAME_STR( LEN_UCAL, UCAL_HOUR ),
100 FIELD_NAME_STR( LEN_UCAL, UCAL_HOUR_OF_DAY ),
101 FIELD_NAME_STR( LEN_UCAL, UCAL_MINUTE ),
102 FIELD_NAME_STR( LEN_UCAL, UCAL_SECOND ),
103 FIELD_NAME_STR( LEN_UCAL, UCAL_MILLISECOND ),
104 FIELD_NAME_STR( LEN_UCAL, UCAL_ZONE_OFFSET ),
105 FIELD_NAME_STR( LEN_UCAL, UCAL_DST_OFFSET ),
106 FIELD_NAME_STR( LEN_UCAL, UCAL_YEAR_WOY ),
107 FIELD_NAME_STR( LEN_UCAL, UCAL_DOW_LOCAL ),
108 FIELD_NAME_STR( LEN_UCAL, UCAL_EXTENDED_YEAR ),
109 FIELD_NAME_STR( LEN_UCAL, UCAL_JULIAN_DAY ),
110 FIELD_NAME_STR( LEN_UCAL, UCAL_MILLISECONDS_IN_DAY ),
111 FIELD_NAME_STR( LEN_UCAL, UCAL_IS_LEAP_MONTH ),
112 };
113
114
115 static const int32_t count_UCalendarMonths = UCAL_UNDECIMBER+1;
116 static const Field names_UCalendarMonths[] =
117 {
118 FIELD_NAME_STR( LEN_UCAL, UCAL_JANUARY ),
119 FIELD_NAME_STR( LEN_UCAL, UCAL_FEBRUARY ),
120 FIELD_NAME_STR( LEN_UCAL, UCAL_MARCH ),
121 FIELD_NAME_STR( LEN_UCAL, UCAL_APRIL ),
122 FIELD_NAME_STR( LEN_UCAL, UCAL_MAY ),
123 FIELD_NAME_STR( LEN_UCAL, UCAL_JUNE ),
124 FIELD_NAME_STR( LEN_UCAL, UCAL_JULY ),
125 FIELD_NAME_STR( LEN_UCAL, UCAL_AUGUST ),
126 FIELD_NAME_STR( LEN_UCAL, UCAL_SEPTEMBER ),
127 FIELD_NAME_STR( LEN_UCAL, UCAL_OCTOBER ),
128 FIELD_NAME_STR( LEN_UCAL, UCAL_NOVEMBER ),
129 FIELD_NAME_STR( LEN_UCAL, UCAL_DECEMBER ),
130 FIELD_NAME_STR( LEN_UCAL, UCAL_UNDECIMBER)
131 };
132
133 #include "unicode/udat.h"
134
135 #define LEN_UDAT 5 /* "UDAT_" */
136 static const int32_t count_UDateFormatStyle = UDAT_SHORT+1;
137 static const Field names_UDateFormatStyle[] =
138 {
139 FIELD_NAME_STR( LEN_UDAT, UDAT_FULL ),
140 FIELD_NAME_STR( LEN_UDAT, UDAT_LONG ),
141 FIELD_NAME_STR( LEN_UDAT, UDAT_MEDIUM ),
142 FIELD_NAME_STR( LEN_UDAT, UDAT_SHORT ),
143 /* end regular */
144 /*
145 * negative enums.. leave out for now.
146 FIELD_NAME_STR( LEN_UDAT, UDAT_NONE ),
147 FIELD_NAME_STR( LEN_UDAT, UDAT_PATTERN ),
148 */
149 };
150
151 #endif
152
153 #include "unicode/uloc.h"
154
155 #define LEN_UAR 12 /* "ULOC_ACCEPT_" */
156 static const int32_t count_UAcceptResult = 3;
157 static const Field names_UAcceptResult[] =
158 {
159 FIELD_NAME_STR( LEN_UAR, ULOC_ACCEPT_FAILED ),
160 FIELD_NAME_STR( LEN_UAR, ULOC_ACCEPT_VALID ),
161 FIELD_NAME_STR( LEN_UAR, ULOC_ACCEPT_FALLBACK ),
162 };
163
164 #if !UCONFIG_NO_COLLATION
165 #include "unicode/ucol.h"
166 #define LEN_UCOL 5 /* UCOL_ */
167 static const int32_t count_UColAttributeValue = UCOL_ATTRIBUTE_VALUE_COUNT;
168 static const Field names_UColAttributeValue[] = {
169 FIELD_NAME_STR( LEN_UCOL, UCOL_PRIMARY ),
170 FIELD_NAME_STR( LEN_UCOL, UCOL_SECONDARY ),
171 FIELD_NAME_STR( LEN_UCOL, UCOL_TERTIARY ),
172 // FIELD_NAME_STR( LEN_UCOL, UCOL_CE_STRENGTH_LIMIT ),
173 FIELD_NAME_STR( LEN_UCOL, UCOL_QUATERNARY ),
174 // gap
175 FIELD_NAME_STR( LEN_UCOL, UCOL_IDENTICAL ),
176 // FIELD_NAME_STR( LEN_UCOL, UCOL_STRENGTH_LIMIT ),
177 FIELD_NAME_STR( LEN_UCOL, UCOL_OFF ),
178 FIELD_NAME_STR( LEN_UCOL, UCOL_ON ),
179 // gap
180 FIELD_NAME_STR( LEN_UCOL, UCOL_SHIFTED ),
181 FIELD_NAME_STR( LEN_UCOL, UCOL_NON_IGNORABLE ),
182 // gap
183 FIELD_NAME_STR( LEN_UCOL, UCOL_LOWER_FIRST ),
184 FIELD_NAME_STR( LEN_UCOL, UCOL_UPPER_FIRST ),
185 };
186
187 #endif
188
189
190 #if UCONFIG_ENABLE_PLUGINS
191 #include "unicode/icuplug.h"
192
193 #define LEN_UPLUG_REASON 13 /* UPLUG_REASON_ */
194 static const int32_t count_UPlugReason = UPLUG_REASON_COUNT;
195 static const Field names_UPlugReason[] = {
196 FIELD_NAME_STR( LEN_UPLUG_REASON, UPLUG_REASON_QUERY ),
197 FIELD_NAME_STR( LEN_UPLUG_REASON, UPLUG_REASON_LOAD ),
198 FIELD_NAME_STR( LEN_UPLUG_REASON, UPLUG_REASON_UNLOAD ),
199 };
200
201 #define LEN_UPLUG_LEVEL 12 /* UPLUG_LEVEL_ */
202 static const int32_t count_UPlugLevel = UPLUG_LEVEL_COUNT;
203 static const Field names_UPlugLevel[] = {
204 FIELD_NAME_STR( LEN_UPLUG_LEVEL, UPLUG_LEVEL_INVALID ),
205 FIELD_NAME_STR( LEN_UPLUG_LEVEL, UPLUG_LEVEL_UNKNOWN ),
206 FIELD_NAME_STR( LEN_UPLUG_LEVEL, UPLUG_LEVEL_LOW ),
207 FIELD_NAME_STR( LEN_UPLUG_LEVEL, UPLUG_LEVEL_HIGH ),
208 };
209 #endif
210
211 #define LEN_UDBG 5 /* "UDBG_" */
212 static const int32_t count_UDebugEnumType = UDBG_ENUM_COUNT;
213 static const Field names_UDebugEnumType[] =
214 {
215 FIELD_NAME_STR( LEN_UDBG, UDBG_UDebugEnumType ),
216 #if !UCONFIG_NO_FORMATTING
217 FIELD_NAME_STR( LEN_UDBG, UDBG_UCalendarDateFields ),
218 FIELD_NAME_STR( LEN_UDBG, UDBG_UCalendarMonths ),
219 FIELD_NAME_STR( LEN_UDBG, UDBG_UDateFormatStyle ),
220 #endif
221 #if UCONFIG_ENABLE_PLUGINS
222 FIELD_NAME_STR( LEN_UDBG, UDBG_UPlugReason ),
223 FIELD_NAME_STR( LEN_UDBG, UDBG_UPlugLevel ),
224 #endif
225 FIELD_NAME_STR( LEN_UDBG, UDBG_UAcceptResult ),
226 #if !UCONFIG_NO_COLLATION
227 FIELD_NAME_STR( LEN_UDBG, UDBG_UColAttributeValue ),
228 #endif
229 };
230
231
232 // --- Add new enum types above this line ---
233
234 #define COUNT_CASE(x) case UDBG_##x: return (actual?count_##x:UPRV_LENGTHOF(names_##x));
235 #define COUNT_FAIL_CASE(x) case UDBG_##x: return -1;
236
237 #define FIELD_CASE(x) case UDBG_##x: return names_##x;
238 #define FIELD_FAIL_CASE(x) case UDBG_##x: return NULL;
239
240 // low level
241
242 /**
243 * @param type type of item
244 * @param actual TRUE: for the actual enum's type (UCAL_FIELD_COUNT, etc), or FALSE for the string count
245 */
246 static int32_t _udbg_enumCount(UDebugEnumType type, UBool actual) {
247 switch(type) {
248 COUNT_CASE(UDebugEnumType)
249 #if !UCONFIG_NO_FORMATTING
250 COUNT_CASE(UCalendarDateFields)
251 COUNT_CASE(UCalendarMonths)
252 COUNT_CASE(UDateFormatStyle)
253 #endif
254 #if UCONFIG_ENABLE_PLUGINS
255 COUNT_CASE(UPlugReason)
256 COUNT_CASE(UPlugLevel)
257 #endif
258 COUNT_CASE(UAcceptResult)
259 #if !UCONFIG_NO_COLLATION
260 COUNT_CASE(UColAttributeValue)
261 #endif
262 // COUNT_FAIL_CASE(UNonExistentEnum)
263 default:
264 return -1;
265 }
266 }
267
268 static const Field* _udbg_enumFields(UDebugEnumType type) {
269 switch(type) {
270 FIELD_CASE(UDebugEnumType)
271 #if !UCONFIG_NO_FORMATTING
272 FIELD_CASE(UCalendarDateFields)
273 FIELD_CASE(UCalendarMonths)
274 FIELD_CASE(UDateFormatStyle)
275 #endif
276 #if UCONFIG_ENABLE_PLUGINS
277 FIELD_CASE(UPlugReason)
278 FIELD_CASE(UPlugLevel)
279 #endif
280 FIELD_CASE(UAcceptResult)
281 // FIELD_FAIL_CASE(UNonExistentEnum)
282 #if !UCONFIG_NO_COLLATION
283 FIELD_CASE(UColAttributeValue)
284 #endif
285 default:
286 return NULL;
287 }
288 }
289
290 // implementation
291
292 int32_t udbg_enumCount(UDebugEnumType type) {
293 return _udbg_enumCount(type, FALSE);
294 }
295
296 int32_t udbg_enumExpectedCount(UDebugEnumType type) {
297 return _udbg_enumCount(type, TRUE);
298 }
299
300 const char * udbg_enumName(UDebugEnumType type, int32_t field) {
301 if(field<0 ||
302 field>=_udbg_enumCount(type,FALSE)) { // also will catch unsupported items
303 return NULL;
304 } else {
305 const Field *fields = _udbg_enumFields(type);
306 if(fields == NULL) {
307 return NULL;
308 } else {
309 return fields[field].str + fields[field].prefix;
310 }
311 }
312 }
313
314 int32_t udbg_enumArrayValue(UDebugEnumType type, int32_t field) {
315 if(field<0 ||
316 field>=_udbg_enumCount(type,FALSE)) { // also will catch unsupported items
317 return -1;
318 } else {
319 const Field *fields = _udbg_enumFields(type);
320 if(fields == NULL) {
321 return -1;
322 } else {
323 return fields[field].num;
324 }
325 }
326 }
327
328 int32_t udbg_enumByName(UDebugEnumType type, const char *value) {
329 if(type<0||type>=_udbg_enumCount(UDBG_UDebugEnumType, TRUE)) {
330 return -1; // type out of range
331 }
332 const Field *fields = _udbg_enumFields(type);
333 if (fields != NULL) {
334 for(int32_t field = 0;field<_udbg_enumCount(type, FALSE);field++) {
335 if(!strcmp(value, fields[field].str + fields[field].prefix)) {
336 return fields[field].num;
337 }
338 }
339 // try with the prefix
340 for(int32_t field = 0;field<_udbg_enumCount(type, FALSE);field++) {
341 if(!strcmp(value, fields[field].str)) {
342 return fields[field].num;
343 }
344 }
345 }
346 // fail
347 return -1;
348 }
349
350 /* platform info */
351 /**
352 * Print the current platform
353 */
354 U_CAPI const char *udbg_getPlatform(void)
355 {
356 #if U_PLATFORM_USES_ONLY_WIN32_API
357 return "Windows";
358 #elif U_PLATFORM == U_PF_CYGWIN
359 return "Cygwin";
360 #elif U_PLATFORM == U_PF_UNKNOWN
361 return "unknown";
362 #elif U_PLATFORM == U_PF_DARWIN
363 return "Darwin";
364 #elif U_PLATFORM == U_PF_BSD
365 return "BSD";
366 #elif U_PLATFORM == U_PF_QNX
367 return "QNX";
368 #elif U_PLATFORM == U_PF_LINUX
369 return "Linux";
370 #elif U_PLATFORM == U_PF_ANDROID
371 return "Android";
372 #elif U_PLATFORM == U_PF_CLASSIC_MACOS
373 return "MacOS (Classic)";
374 #elif U_PLATFORM == U_PF_OS390
375 return "IBM z";
376 #elif U_PLATFORM == U_PF_OS400
377 return "IBM i";
378 #else
379 return "Other (POSIX-like)";
380 #endif
381 }
382
383 struct USystemParams;
384
385 typedef int32_t U_CALLCONV USystemParameterCallback(const USystemParams *param, char *target, int32_t targetCapacity, UErrorCode *status);
386
387 struct USystemParams {
388 const char *paramName;
389 USystemParameterCallback *paramFunction;
390 const char *paramStr;
391 int32_t paramInt;
392 };
393
394 /* parameter types */
395 U_CAPI int32_t
396 paramEmpty(const USystemParams * /* param */, char *target, int32_t targetCapacity, UErrorCode *status) {
397 if(U_FAILURE(*status))return 0;
398 return u_terminateChars(target, targetCapacity, 0, status);
399 }
400
401 U_CAPI int32_t
402 paramStatic(const USystemParams *param, char *target, int32_t targetCapacity, UErrorCode *status) {
403 if(param->paramStr==NULL) return paramEmpty(param,target,targetCapacity,status);
404 if(U_FAILURE(*status))return 0;
405 int32_t len = static_cast<int32_t>(uprv_strlen(param->paramStr));
406 if(target!=NULL) {
407 uprv_strncpy(target,param->paramStr,uprv_min(len,targetCapacity));
408 }
409 return u_terminateChars(target, targetCapacity, len, status);
410 }
411
412 static const char *nullString = "(null)";
413
414 static int32_t stringToStringBuffer(char *target, int32_t targetCapacity, const char *str, UErrorCode *status) {
415 if(str==NULL) str=nullString;
416
417 int32_t len = static_cast<int32_t>(uprv_strlen(str));
418 if (U_SUCCESS(*status)) {
419 if(target!=NULL) {
420 uprv_strncpy(target,str,uprv_min(len,targetCapacity));
421 }
422 } else {
423 const char *s = u_errorName(*status);
424 len = static_cast<int32_t>(uprv_strlen(s));
425 if(target!=NULL) {
426 uprv_strncpy(target,s,uprv_min(len,targetCapacity));
427 }
428 }
429 return u_terminateChars(target, targetCapacity, len, status);
430 }
431
432 static int32_t integerToStringBuffer(char *target, int32_t targetCapacity, int32_t n, int32_t radix, UErrorCode *status) {
433 if(U_FAILURE(*status)) return 0;
434 char str[300];
435 T_CString_integerToString(str,n,radix);
436 return stringToStringBuffer(target,targetCapacity,str,status);
437 }
438
439 U_CAPI int32_t
440 paramInteger(const USystemParams *param, char *target, int32_t targetCapacity, UErrorCode *status) {
441 if(U_FAILURE(*status))return 0;
442 if(param->paramStr==NULL || param->paramStr[0]=='d') {
443 return integerToStringBuffer(target,targetCapacity,param->paramInt, 10,status);
444 } else if(param->paramStr[0]=='x') {
445 return integerToStringBuffer(target,targetCapacity,param->paramInt, 16,status);
446 } else if(param->paramStr[0]=='o') {
447 return integerToStringBuffer(target,targetCapacity,param->paramInt, 8,status);
448 } else if(param->paramStr[0]=='b') {
449 return integerToStringBuffer(target,targetCapacity,param->paramInt, 2,status);
450 } else {
451 *status = U_INTERNAL_PROGRAM_ERROR;
452 return 0;
453 }
454 }
455
456
457 U_CAPI int32_t
458 paramCldrVersion(const USystemParams * /* param */, char *target, int32_t targetCapacity, UErrorCode *status) {
459 if(U_FAILURE(*status))return 0;
460 char str[200]="";
461 UVersionInfo icu;
462
463 ulocdata_getCLDRVersion(icu, status);
464 if(U_SUCCESS(*status)) {
465 u_versionToString(icu, str);
466 return stringToStringBuffer(target,targetCapacity,str,status);
467 } else {
468 return 0;
469 }
470 }
471
472 // Apple addition
473 static void versionBinToString(UVersionInfo icu, char* str) {
474 sprintf(str, "%d.%d.%d.%d", icu[0], icu[1], icu[2], icu[3]);
475 }
476
477 // Apple addition
478 U_CAPI int32_t
479 paramCollUCAVersion(const USystemParams * /* param */, char *target, int32_t targetCapacity, UErrorCode *status) {
480 if(U_FAILURE(*status))return 0;
481 char str[200]="";
482 UVersionInfo icu;
483
484 UCollator * ucol = ucol_open("root", status);
485 if(U_SUCCESS(*status)) {
486 ucol_getUCAVersion(ucol, icu);
487 versionBinToString(icu, str);
488 ucol_close(ucol);
489 return stringToStringBuffer(target,targetCapacity,str,status);
490 } else {
491 return 0;
492 }
493 }
494
495 // Apple addition
496 U_CAPI int32_t
497 paramCollRootVersion(const USystemParams * /* param */, char *target, int32_t targetCapacity, UErrorCode *status) {
498 if(U_FAILURE(*status))return 0;
499 char str[200]="";
500 UVersionInfo icu;
501
502 UCollator * ucol = ucol_open("root", status);
503 if(U_SUCCESS(*status)) {
504 ucol_getVersion(ucol, icu);
505 versionBinToString(icu, str);
506 ucol_close(ucol);
507 return stringToStringBuffer(target,targetCapacity,str,status);
508 } else {
509 return 0;
510 }
511 }
512
513 // Apple addition
514 U_CAPI int32_t
515 paramCollEnVersion(const USystemParams * /* param */, char *target, int32_t targetCapacity, UErrorCode *status) {
516 if(U_FAILURE(*status))return 0;
517 char str[200]="";
518 UVersionInfo icu;
519
520 UCollator * ucol = ucol_open("en", status);
521 if(U_SUCCESS(*status)) {
522 ucol_getVersion(ucol, icu);
523 versionBinToString(icu, str);
524 ucol_close(ucol);
525 return stringToStringBuffer(target,targetCapacity,str,status);
526 } else {
527 return 0;
528 }
529 }
530
531 // Apple addition
532 U_CAPI int32_t
533 paramCollZhVersion(const USystemParams * /* param */, char *target, int32_t targetCapacity, UErrorCode *status) {
534 if(U_FAILURE(*status))return 0;
535 char str[200]="";
536 UVersionInfo icu;
537
538 UCollator * ucol = ucol_open("zh", status);
539 if(U_SUCCESS(*status)) {
540 ucol_getVersion(ucol, icu);
541 versionBinToString(icu, str);
542 ucol_close(ucol);
543 return stringToStringBuffer(target,targetCapacity,str,status);
544 } else {
545 return 0;
546 }
547 }
548
549
550 #if !UCONFIG_NO_FORMATTING
551 U_CAPI int32_t
552 paramTimezoneDefault(const USystemParams * /* param */, char *target, int32_t targetCapacity, UErrorCode *status) {
553 if(U_FAILURE(*status))return 0;
554 UChar buf[100];
555 char buf2[100];
556 int32_t len;
557
558 len = ucal_getDefaultTimeZone(buf, 100, status);
559 if(U_SUCCESS(*status)&&len>0) {
560 u_UCharsToChars(buf, buf2, len+1);
561 return stringToStringBuffer(target,targetCapacity, buf2,status);
562 } else {
563 return 0;
564 }
565 }
566 #endif
567
568 U_CAPI int32_t
569 paramLocaleDefaultBcp47(const USystemParams * /* param */, char *target, int32_t targetCapacity, UErrorCode *status) {
570 if(U_FAILURE(*status))return 0;
571 const char *def = uloc_getDefault();
572 return uloc_toLanguageTag(def,target,targetCapacity,FALSE,status);
573 }
574
575
576 /* simple 1-liner param functions */
577 #define STRING_PARAM(func, str) U_CAPI int32_t \
578 func(const USystemParams *, char *target, int32_t targetCapacity, UErrorCode *status) \
579 { return stringToStringBuffer(target,targetCapacity,(str),status); }
580
581 STRING_PARAM(paramIcudataPath, u_getDataDirectory())
582 STRING_PARAM(paramPlatform, udbg_getPlatform())
583 STRING_PARAM(paramLocaleDefault, uloc_getDefault())
584 #if !UCONFIG_NO_CONVERSION
585 STRING_PARAM(paramConverterDefault, ucnv_getDefaultName())
586 #endif
587
588 #if !UCONFIG_NO_FORMATTING
589 STRING_PARAM(paramTimezoneVersion, ucal_getTZDataVersion(status))
590 #endif
591
592 static const USystemParams systemParams[] = {
593 { "copyright", paramStatic, U_COPYRIGHT_STRING,0 },
594 { "product", paramStatic, "icu4c",0 },
595 { "product.full", paramStatic, "International Components for Unicode for C/C++",0 },
596 { "version", paramStatic, U_ICU_VERSION,0 },
597 { "version.unicode", paramStatic, U_UNICODE_VERSION,0 },
598 { "platform.number", paramInteger, "d",U_PLATFORM},
599 { "platform.type", paramPlatform, NULL ,0},
600 { "locale.default", paramLocaleDefault, NULL, 0},
601 { "locale.default.bcp47", paramLocaleDefaultBcp47, NULL, 0},
602 #if !UCONFIG_NO_CONVERSION
603 { "converter.default", paramConverterDefault, NULL, 0},
604 #endif
605 { "icudata.name", paramStatic, U_ICUDATA_NAME, 0},
606 { "icudata.path", paramIcudataPath, NULL, 0},
607
608 { "cldr.version", paramCldrVersion, NULL, 0},
609 { "coll-uca.version", paramCollUCAVersion, NULL, 0},
610 { "coll-root.version", paramCollRootVersion, NULL, 0},
611 { "coll-en.version", paramCollEnVersion, NULL, 0},
612 { "coll-zh.version", paramCollZhVersion, NULL, 0},
613
614 #if !UCONFIG_NO_FORMATTING
615 { "tz.version", paramTimezoneVersion, NULL, 0},
616 { "tz.default", paramTimezoneDefault, NULL, 0},
617 #endif
618
619 { "cpu.bits", paramInteger, "d", (sizeof(void*))*8},
620 { "cpu.big_endian", paramInteger, "b", U_IS_BIG_ENDIAN},
621 { "os.wchar_width", paramInteger, "d", U_SIZEOF_WCHAR_T},
622 { "os.charset_family", paramInteger, "d", U_CHARSET_FAMILY},
623 #if defined (U_HOST)
624 { "os.host", paramStatic, U_HOST, 0},
625 #endif
626 #if defined (U_BUILD)
627 { "build.build", paramStatic, U_BUILD, 0},
628 #endif
629 #if defined (U_CC)
630 { "build.cc", paramStatic, U_CC, 0},
631 #endif
632 #if defined (U_CXX)
633 { "build.cxx", paramStatic, U_CXX, 0},
634 #endif
635 #if defined (CYGWINMSVC)
636 { "build.cygwinmsvc", paramInteger, "b", 1},
637 #endif
638 { "uconfig.internal_digitlist", paramInteger, "b", 1}, /* always 1 */
639 { "uconfig.have_parseallinput", paramInteger, "b", UCONFIG_HAVE_PARSEALLINPUT},
640
641
642 };
643
644 #define U_SYSPARAM_COUNT UPRV_LENGTHOF(systemParams)
645
646 U_CAPI const char *udbg_getSystemParameterNameByIndex(int32_t i) {
647 if(i>=0 && i < (int32_t)U_SYSPARAM_COUNT) {
648 return systemParams[i].paramName;
649 } else {
650 return NULL;
651 }
652 }
653
654
655 U_CAPI int32_t udbg_getSystemParameterValueByIndex(int32_t i, char *buffer, int32_t bufferCapacity, UErrorCode *status) {
656 if(i>=0 && i< (int32_t)U_SYSPARAM_COUNT) {
657 return systemParams[i].paramFunction(&(systemParams[i]),buffer,bufferCapacity,status);
658 } else {
659 return 0;
660 }
661 }
662
663 U_CAPI void udbg_writeIcuInfo(FILE *out) {
664 char str[2000];
665 /* todo: API for writing DTD? */
666 fprintf(out, " <icuSystemParams type=\"icu4c\">\n");
667 const char *paramName;
668 for(int32_t i=0;(paramName=udbg_getSystemParameterNameByIndex(i))!=NULL;i++) {
669 UErrorCode status2 = U_ZERO_ERROR;
670 udbg_getSystemParameterValueByIndex(i, str,2000,&status2);
671 if(U_SUCCESS(status2)) {
672 fprintf(out," <param name=\"%s\">%s</param>\n", paramName,str);
673 } else {
674 fprintf(out," <!-- n=\"%s\" ERROR: %s -->\n", paramName, u_errorName(status2));
675 }
676 }
677 fprintf(out, " </icuSystemParams>\n");
678 }
679
680 #define ICU_TRAC_URL "http://bugs.icu-project.org/trac/ticket/"
681 #define CLDR_TRAC_URL "http://unicode.org/cldr/trac/ticket/"
682 #define CLDR_TICKET_PREFIX "cldrbug:"
683
684 U_CAPI char *udbg_knownIssueURLFrom(const char *ticket, char *buf) {
685 if( ticket==NULL ) {
686 return NULL;
687 }
688
689 if( !strncmp(ticket, CLDR_TICKET_PREFIX, strlen(CLDR_TICKET_PREFIX)) ) {
690 strcpy( buf, CLDR_TRAC_URL );
691 strcat( buf, ticket+strlen(CLDR_TICKET_PREFIX) );
692 } else {
693 strcpy( buf, ICU_TRAC_URL );
694 strcat( buf, ticket );
695 }
696 return buf;
697 }
698
699
700 #include <set>
701 #include <map>
702 #include <string>
703 #include <ostream>
704 #include <iostream>
705
706 class KnownIssues {
707 public:
708 KnownIssues();
709 ~KnownIssues();
710 void add(const char *ticket, const char *where, const UChar *msg, UBool *firstForTicket, UBool *firstForWhere);
711 void add(const char *ticket, const char *where, const char *msg, UBool *firstForTicket, UBool *firstForWhere);
712 UBool print();
713 private:
714 std::map< std::string,
715 std::map < std::string, std::set < std::string > > > fTable;
716 };
717
718 KnownIssues::KnownIssues()
719 : fTable()
720 {
721 }
722
723 KnownIssues::~KnownIssues()
724 {
725 }
726
727 void KnownIssues::add(const char *ticket, const char *where, const UChar *msg, UBool *firstForTicket, UBool *firstForWhere)
728 {
729 if(fTable.find(ticket) == fTable.end()) {
730 if(firstForTicket!=NULL) *firstForTicket = TRUE;
731 fTable[ticket] = std::map < std::string, std::set < std::string > >();
732 } else {
733 if(firstForTicket!=NULL) *firstForTicket = FALSE;
734 }
735 if(where==NULL) return;
736
737 if(fTable[ticket].find(where) == fTable[ticket].end()) {
738 if(firstForWhere!=NULL) *firstForWhere = TRUE;
739 fTable[ticket][where] = std::set < std::string >();
740 } else {
741 if(firstForWhere!=NULL) *firstForWhere = FALSE;
742 }
743 if(msg==NULL || !*msg) return;
744
745 const icu::UnicodeString ustr(msg);
746
747 fTable[ticket][where].insert(std::string(icu::CStr(ustr)()));
748 }
749
750 void KnownIssues::add(const char *ticket, const char *where, const char *msg, UBool *firstForTicket, UBool *firstForWhere)
751 {
752 if(fTable.find(ticket) == fTable.end()) {
753 if(firstForTicket!=NULL) *firstForTicket = TRUE;
754 fTable[ticket] = std::map < std::string, std::set < std::string > >();
755 } else {
756 if(firstForTicket!=NULL) *firstForTicket = FALSE;
757 }
758 if(where==NULL) return;
759
760 if(fTable[ticket].find(where) == fTable[ticket].end()) {
761 if(firstForWhere!=NULL) *firstForWhere = TRUE;
762 fTable[ticket][where] = std::set < std::string >();
763 } else {
764 if(firstForWhere!=NULL) *firstForWhere = FALSE;
765 }
766 if(msg==NULL || !*msg) return;
767
768 std::string str(msg);
769 fTable[ticket][where].insert(str);
770 }
771
772 UBool KnownIssues::print()
773 {
774 if(fTable.empty()) {
775 return FALSE;
776 }
777
778 std::cout << "KNOWN ISSUES" << std::endl;
779 for( std::map< std::string,
780 std::map < std::string, std::set < std::string > > >::iterator i = fTable.begin();
781 i != fTable.end();
782 i++ ) {
783 char URL[1024];
784 std::cout << '#' << (*i).first << " <" << udbg_knownIssueURLFrom( (*i).first.c_str(), URL ) << ">" << std::endl;
785
786 for( std::map< std::string, std::set < std::string > >::iterator ii = (*i).second.begin();
787 ii != (*i).second.end();
788 ii++ ) {
789 std::cout << " " << (*ii).first << std::endl;
790 for ( std::set < std::string >::iterator iii = (*ii).second.begin();
791 iii != (*ii).second.end();
792 iii++ ) {
793 std::cout << " " << '"' << (*iii) << '"' << std::endl;
794 }
795 }
796 }
797 return TRUE;
798 }
799
800 U_CAPI void *udbg_knownIssue_openU(void *ptr, const char *ticket, char *where, const UChar *msg, UBool *firstForTicket,
801 UBool *firstForWhere) {
802 KnownIssues *t = static_cast<KnownIssues*>(ptr);
803 if(t==NULL) {
804 t = new KnownIssues();
805 }
806
807 t->add(ticket, where, msg, firstForTicket, firstForWhere);
808
809 return static_cast<void*>(t);
810 }
811
812 U_CAPI void *udbg_knownIssue_open(void *ptr, const char *ticket, char *where, const char *msg, UBool *firstForTicket,
813 UBool *firstForWhere) {
814 KnownIssues *t = static_cast<KnownIssues*>(ptr);
815 if(t==NULL) {
816 t = new KnownIssues();
817 }
818
819 t->add(ticket, where, msg, firstForTicket, firstForWhere);
820
821 return static_cast<void*>(t);
822 }
823
824 U_CAPI UBool udbg_knownIssue_print(void *ptr) {
825 KnownIssues *t = static_cast<KnownIssues*>(ptr);
826 if(t==NULL) {
827 return FALSE;
828 } else {
829 t->print();
830 return TRUE;
831 }
832 }
833
834 U_CAPI void udbg_knownIssue_close(void *ptr) {
835 KnownIssues *t = static_cast<KnownIssues*>(ptr);
836 delete t;
837 }