1 /********************************************************************
3 * Copyright (c) 2007-2012, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
12 #include "unicode/ulocdata.h"
13 #include "unicode/ucnv.h"
15 To add a new enum type
16 (For example: UShoeSize with values USHOE_WIDE=0, USHOE_REGULAR, USHOE_NARROW, USHOE_COUNT)
18 1. udbgutil.h: add UDBG_UShoeSize to the UDebugEnumType enum before UDBG_ENUM_COUNT
19 ( The subsequent steps involve this file, udbgutil.cpp )
20 2. Find the marker "Add new enum types above this line"
21 3. Before that marker, add a #include of any header file you need.
22 4. Each enum type has three things in this section: a #define, a count_, and an array of Fields.
23 It may help to copy and paste a previous definition.
24 5. In the case of the USHOE_... strings above, "USHOE_" is common to all values- six characters
25 " #define LEN_USHOE 6 "
26 6 characters will strip off "USHOE_" leaving enum values of WIDE, REGULAR, and NARROW.
27 6. Define the 'count_' variable, with the number of enum values. If the enum has a _MAX or _COUNT value,
28 that can be helpful for automatically defining the count. Otherwise define it manually.
29 " static const int32_t count_UShoeSize = USHOE_COUNT; "
30 7. Define the field names, in order.
31 " static const Field names_UShoeSize[] = {
32 " FIELD_NAME_STR( LEN_USHOE, USHOE_WIDE ),
33 " FIELD_NAME_STR( LEN_USHOE, USHOE_REGULAR ),
34 " FIELD_NAME_STR( LEN_USHOE, USHOE_NARROW ),
36 ( The following command was usedfor converting ucol.h into partially correct entities )
37 grep "^[ ]*UCOL" < unicode/ucol.h |
38 sed -e 's%^[ ]*\([A-Z]*\)_\([A-Z_]*\).*% FIELD_NAME_STR( LEN_\1, \1_\2 ),%g'
39 8. Now, a bit farther down, add the name of the enum itself to the end of names_UDebugEnumType
40 ( UDebugEnumType is an enum, too!)
41 names_UDebugEnumType[] { ...
42 " FIELD_NAME_STR( LEN_UDBG, UDBG_UShoeSize ), "
43 9. Find the function _udbg_enumCount and add the count macro:
44 " COUNT_CASE(UShoeSize)
45 10. Find the function _udbg_enumFields and add the field macro:
46 " FIELD_CASE(UShoeSize)
47 11. verify that your test code, and Java data generation, works properly.
51 * Structure representing an enum value
54 int32_t prefix
; /**< how many characters to remove in the prefix - i.e. UCHAR_ = 5 */
55 const char *str
; /**< The actual string value */
56 int32_t num
; /**< The numeric value */
60 * Calculate the size of an array.
62 #define DBG_ARRAY_COUNT(x) (sizeof(x)/sizeof(x[0]))
65 * Define another field name. Used in an array of Field s
66 * @param y the common prefix length (i.e. 6 for "USHOE_" )
67 * @param x the actual enum value - it will be copied in both string and symbolic form.
70 #define FIELD_NAME_STR(y,x) { y, #x, x }
73 // TODO: Currently, this whole functionality goes away with UCONFIG_NO_FORMATTING. Should be split up.
74 #if !UCONFIG_NO_FORMATTING
77 #include "unicode/ucal.h"
80 #define LEN_UCAL 5 /* UCAL_ */
81 static const int32_t count_UCalendarDateFields
= UCAL_FIELD_COUNT
;
82 static const Field names_UCalendarDateFields
[] =
84 FIELD_NAME_STR( LEN_UCAL
, UCAL_ERA
),
85 FIELD_NAME_STR( LEN_UCAL
, UCAL_YEAR
),
86 FIELD_NAME_STR( LEN_UCAL
, UCAL_MONTH
),
87 FIELD_NAME_STR( LEN_UCAL
, UCAL_WEEK_OF_YEAR
),
88 FIELD_NAME_STR( LEN_UCAL
, UCAL_WEEK_OF_MONTH
),
89 FIELD_NAME_STR( LEN_UCAL
, UCAL_DATE
),
90 FIELD_NAME_STR( LEN_UCAL
, UCAL_DAY_OF_YEAR
),
91 FIELD_NAME_STR( LEN_UCAL
, UCAL_DAY_OF_WEEK
),
92 FIELD_NAME_STR( LEN_UCAL
, UCAL_DAY_OF_WEEK_IN_MONTH
),
93 FIELD_NAME_STR( LEN_UCAL
, UCAL_AM_PM
),
94 FIELD_NAME_STR( LEN_UCAL
, UCAL_HOUR
),
95 FIELD_NAME_STR( LEN_UCAL
, UCAL_HOUR_OF_DAY
),
96 FIELD_NAME_STR( LEN_UCAL
, UCAL_MINUTE
),
97 FIELD_NAME_STR( LEN_UCAL
, UCAL_SECOND
),
98 FIELD_NAME_STR( LEN_UCAL
, UCAL_MILLISECOND
),
99 FIELD_NAME_STR( LEN_UCAL
, UCAL_ZONE_OFFSET
),
100 FIELD_NAME_STR( LEN_UCAL
, UCAL_DST_OFFSET
),
101 FIELD_NAME_STR( LEN_UCAL
, UCAL_YEAR_WOY
),
102 FIELD_NAME_STR( LEN_UCAL
, UCAL_DOW_LOCAL
),
103 FIELD_NAME_STR( LEN_UCAL
, UCAL_EXTENDED_YEAR
),
104 FIELD_NAME_STR( LEN_UCAL
, UCAL_JULIAN_DAY
),
105 FIELD_NAME_STR( LEN_UCAL
, UCAL_MILLISECONDS_IN_DAY
),
106 FIELD_NAME_STR( LEN_UCAL
, UCAL_IS_LEAP_MONTH
),
110 static const int32_t count_UCalendarMonths
= UCAL_UNDECIMBER
+1;
111 static const Field names_UCalendarMonths
[] =
113 FIELD_NAME_STR( LEN_UCAL
, UCAL_JANUARY
),
114 FIELD_NAME_STR( LEN_UCAL
, UCAL_FEBRUARY
),
115 FIELD_NAME_STR( LEN_UCAL
, UCAL_MARCH
),
116 FIELD_NAME_STR( LEN_UCAL
, UCAL_APRIL
),
117 FIELD_NAME_STR( LEN_UCAL
, UCAL_MAY
),
118 FIELD_NAME_STR( LEN_UCAL
, UCAL_JUNE
),
119 FIELD_NAME_STR( LEN_UCAL
, UCAL_JULY
),
120 FIELD_NAME_STR( LEN_UCAL
, UCAL_AUGUST
),
121 FIELD_NAME_STR( LEN_UCAL
, UCAL_SEPTEMBER
),
122 FIELD_NAME_STR( LEN_UCAL
, UCAL_OCTOBER
),
123 FIELD_NAME_STR( LEN_UCAL
, UCAL_NOVEMBER
),
124 FIELD_NAME_STR( LEN_UCAL
, UCAL_DECEMBER
),
125 FIELD_NAME_STR( LEN_UCAL
, UCAL_UNDECIMBER
)
128 #include "unicode/udat.h"
130 #define LEN_UDAT 5 /* "UDAT_" */
131 static const int32_t count_UDateFormatStyle
= UDAT_SHORT
+1;
132 static const Field names_UDateFormatStyle
[] =
134 FIELD_NAME_STR( LEN_UDAT
, UDAT_FULL
),
135 FIELD_NAME_STR( LEN_UDAT
, UDAT_LONG
),
136 FIELD_NAME_STR( LEN_UDAT
, UDAT_MEDIUM
),
137 FIELD_NAME_STR( LEN_UDAT
, UDAT_SHORT
),
140 * negative enums.. leave out for now.
141 FIELD_NAME_STR( LEN_UDAT, UDAT_NONE ),
142 FIELD_NAME_STR( LEN_UDAT, UDAT_IGNORE ),
148 #include "unicode/uloc.h"
150 #define LEN_UAR 12 /* "ULOC_ACCEPT_" */
151 static const int32_t count_UAcceptResult
= 3;
152 static const Field names_UAcceptResult
[] =
154 FIELD_NAME_STR( LEN_UAR
, ULOC_ACCEPT_FAILED
),
155 FIELD_NAME_STR( LEN_UAR
, ULOC_ACCEPT_VALID
),
156 FIELD_NAME_STR( LEN_UAR
, ULOC_ACCEPT_FALLBACK
),
159 #if !UCONFIG_NO_COLLATION
160 #include "unicode/ucol.h"
161 #define LEN_UCOL 5 /* UCOL_ */
162 static const int32_t count_UColAttributeValue
= UCOL_ATTRIBUTE_VALUE_COUNT
;
163 static const Field names_UColAttributeValue
[] = {
164 FIELD_NAME_STR( LEN_UCOL
, UCOL_PRIMARY
),
165 FIELD_NAME_STR( LEN_UCOL
, UCOL_SECONDARY
),
166 FIELD_NAME_STR( LEN_UCOL
, UCOL_TERTIARY
),
167 // FIELD_NAME_STR( LEN_UCOL, UCOL_CE_STRENGTH_LIMIT ),
168 FIELD_NAME_STR( LEN_UCOL
, UCOL_QUATERNARY
),
170 FIELD_NAME_STR( LEN_UCOL
, UCOL_IDENTICAL
),
171 // FIELD_NAME_STR( LEN_UCOL, UCOL_STRENGTH_LIMIT ),
172 FIELD_NAME_STR( LEN_UCOL
, UCOL_OFF
),
173 FIELD_NAME_STR( LEN_UCOL
, UCOL_ON
),
175 FIELD_NAME_STR( LEN_UCOL
, UCOL_SHIFTED
),
176 FIELD_NAME_STR( LEN_UCOL
, UCOL_NON_IGNORABLE
),
178 FIELD_NAME_STR( LEN_UCOL
, UCOL_LOWER_FIRST
),
179 FIELD_NAME_STR( LEN_UCOL
, UCOL_UPPER_FIRST
),
185 #include "unicode/icuplug.h"
187 #define LEN_UPLUG_REASON 13 /* UPLUG_REASON_ */
188 static const int32_t count_UPlugReason
= UPLUG_REASON_COUNT
;
189 static const Field names_UPlugReason
[] = {
190 FIELD_NAME_STR( LEN_UPLUG_REASON
, UPLUG_REASON_QUERY
),
191 FIELD_NAME_STR( LEN_UPLUG_REASON
, UPLUG_REASON_LOAD
),
192 FIELD_NAME_STR( LEN_UPLUG_REASON
, UPLUG_REASON_UNLOAD
),
195 #define LEN_UPLUG_LEVEL 12 /* UPLUG_LEVEL_ */
196 static const int32_t count_UPlugLevel
= UPLUG_LEVEL_COUNT
;
197 static const Field names_UPlugLevel
[] = {
198 FIELD_NAME_STR( LEN_UPLUG_LEVEL
, UPLUG_LEVEL_INVALID
),
199 FIELD_NAME_STR( LEN_UPLUG_LEVEL
, UPLUG_LEVEL_UNKNOWN
),
200 FIELD_NAME_STR( LEN_UPLUG_LEVEL
, UPLUG_LEVEL_LOW
),
201 FIELD_NAME_STR( LEN_UPLUG_LEVEL
, UPLUG_LEVEL_HIGH
),
204 #define LEN_UDBG 5 /* "UDBG_" */
205 static const int32_t count_UDebugEnumType
= UDBG_ENUM_COUNT
;
206 static const Field names_UDebugEnumType
[] =
208 FIELD_NAME_STR( LEN_UDBG
, UDBG_UDebugEnumType
),
209 #if !UCONFIG_NO_FORMATTING
210 FIELD_NAME_STR( LEN_UDBG
, UDBG_UCalendarDateFields
),
211 FIELD_NAME_STR( LEN_UDBG
, UDBG_UCalendarMonths
),
212 FIELD_NAME_STR( LEN_UDBG
, UDBG_UDateFormatStyle
),
214 FIELD_NAME_STR( LEN_UDBG
, UDBG_UPlugReason
),
215 FIELD_NAME_STR( LEN_UDBG
, UDBG_UPlugLevel
),
216 FIELD_NAME_STR( LEN_UDBG
, UDBG_UAcceptResult
),
217 #if !UCONFIG_NO_COLLATION
218 FIELD_NAME_STR( LEN_UDBG
, UDBG_UColAttributeValue
),
223 // --- Add new enum types above this line ---
225 #define COUNT_CASE(x) case UDBG_##x: return (actual?count_##x:DBG_ARRAY_COUNT(names_##x));
226 #define COUNT_FAIL_CASE(x) case UDBG_##x: return -1;
228 #define FIELD_CASE(x) case UDBG_##x: return names_##x;
229 #define FIELD_FAIL_CASE(x) case UDBG_##x: return NULL;
234 * @param type type of item
235 * @param actual TRUE: for the actual enum's type (UCAL_FIELD_COUNT, etc), or FALSE for the string count
237 static int32_t _udbg_enumCount(UDebugEnumType type
, UBool actual
) {
239 COUNT_CASE(UDebugEnumType
)
240 #if !UCONFIG_NO_FORMATTING
241 COUNT_CASE(UCalendarDateFields
)
242 COUNT_CASE(UCalendarMonths
)
243 COUNT_CASE(UDateFormatStyle
)
245 COUNT_CASE(UPlugReason
)
246 COUNT_CASE(UPlugLevel
)
247 COUNT_CASE(UAcceptResult
)
248 #if !UCONFIG_NO_COLLATION
249 COUNT_CASE(UColAttributeValue
)
251 // COUNT_FAIL_CASE(UNonExistentEnum)
257 static const Field
* _udbg_enumFields(UDebugEnumType type
) {
259 FIELD_CASE(UDebugEnumType
)
260 #if !UCONFIG_NO_FORMATTING
261 FIELD_CASE(UCalendarDateFields
)
262 FIELD_CASE(UCalendarMonths
)
263 FIELD_CASE(UDateFormatStyle
)
265 FIELD_CASE(UPlugReason
)
266 FIELD_CASE(UPlugLevel
)
267 FIELD_CASE(UAcceptResult
)
268 // FIELD_FAIL_CASE(UNonExistentEnum)
269 #if !UCONFIG_NO_COLLATION
270 FIELD_CASE(UColAttributeValue
)
279 int32_t udbg_enumCount(UDebugEnumType type
) {
280 return _udbg_enumCount(type
, FALSE
);
283 int32_t udbg_enumExpectedCount(UDebugEnumType type
) {
284 return _udbg_enumCount(type
, TRUE
);
287 const char * udbg_enumName(UDebugEnumType type
, int32_t field
) {
289 field
>=_udbg_enumCount(type
,FALSE
)) { // also will catch unsupported items
292 const Field
*fields
= _udbg_enumFields(type
);
296 return fields
[field
].str
+ fields
[field
].prefix
;
301 int32_t udbg_enumArrayValue(UDebugEnumType type
, int32_t field
) {
303 field
>=_udbg_enumCount(type
,FALSE
)) { // also will catch unsupported items
306 const Field
*fields
= _udbg_enumFields(type
);
310 return fields
[field
].num
;
315 int32_t udbg_enumByName(UDebugEnumType type
, const char *value
) {
316 if(type
<0||type
>=_udbg_enumCount(UDBG_UDebugEnumType
, TRUE
)) {
317 return -1; // type out of range
319 const Field
*fields
= _udbg_enumFields(type
);
320 for(int32_t field
= 0;field
<_udbg_enumCount(type
, FALSE
);field
++) {
321 if(!strcmp(value
, fields
[field
].str
+ fields
[field
].prefix
)) {
322 return fields
[field
].num
;
325 // try with the prefix
326 for(int32_t field
= 0;field
<_udbg_enumCount(type
, FALSE
);field
++) {
327 if(!strcmp(value
, fields
[field
].str
)) {
328 return fields
[field
].num
;
337 * Print the current platform
339 U_CAPI
const char *udbg_getPlatform(void)
341 #if U_PLATFORM_HAS_WIN32_API
343 #elif U_PLATFORM == U_PF_UNKNOWN
346 return "Other (POSIX-like)";
350 struct USystemParams
;
352 typedef int32_t U_CALLCONV
USystemParameterCallback(const USystemParams
*param
, char *target
, int32_t targetCapacity
, UErrorCode
*status
);
354 struct USystemParams
{
355 const char *paramName
;
356 USystemParameterCallback
*paramFunction
;
357 const char *paramStr
;
361 /* parameter types */
363 paramEmpty(const USystemParams
* /* param */, char *target
, int32_t targetCapacity
, UErrorCode
*status
) {
364 if(U_FAILURE(*status
))return 0;
365 return u_terminateChars(target
, targetCapacity
, 0, status
);
369 paramStatic(const USystemParams
*param
, char *target
, int32_t targetCapacity
, UErrorCode
*status
) {
370 if(param
->paramStr
==NULL
) return paramEmpty(param
,target
,targetCapacity
,status
);
371 if(U_FAILURE(*status
))return 0;
372 int32_t len
= uprv_strlen(param
->paramStr
);
374 uprv_strncpy(target
,param
->paramStr
,uprv_min(len
,targetCapacity
));
376 return u_terminateChars(target
, targetCapacity
, len
, status
);
379 static int32_t stringToStringBuffer(char *target
, int32_t targetCapacity
, const char *str
, UErrorCode
*status
) {
380 int32_t len
= uprv_strlen(str
);
382 uprv_strncpy(target
,str
,uprv_min(len
,targetCapacity
));
384 return u_terminateChars(target
, targetCapacity
, len
, status
);
387 static int32_t integerToStringBuffer(char *target
, int32_t targetCapacity
, int32_t n
, int32_t radix
, UErrorCode
*status
) {
388 if(U_FAILURE(*status
)) return 0;
390 T_CString_integerToString(str
,n
,radix
);
391 return stringToStringBuffer(target
,targetCapacity
,str
,status
);
395 paramInteger(const USystemParams
*param
, char *target
, int32_t targetCapacity
, UErrorCode
*status
) {
396 if(U_FAILURE(*status
))return 0;
397 if(param
->paramStr
==NULL
|| param
->paramStr
[0]=='d') {
398 return integerToStringBuffer(target
,targetCapacity
,param
->paramInt
, 10,status
);
399 } else if(param
->paramStr
[0]=='x') {
400 return integerToStringBuffer(target
,targetCapacity
,param
->paramInt
, 16,status
);
401 } else if(param
->paramStr
[0]=='o') {
402 return integerToStringBuffer(target
,targetCapacity
,param
->paramInt
, 8,status
);
403 } else if(param
->paramStr
[0]=='b') {
404 return integerToStringBuffer(target
,targetCapacity
,param
->paramInt
, 2,status
);
406 *status
= U_INTERNAL_PROGRAM_ERROR
;
413 paramCldrVersion(const USystemParams
* /* param */, char *target
, int32_t targetCapacity
, UErrorCode
*status
) {
414 if(U_FAILURE(*status
))return 0;
418 ulocdata_getCLDRVersion(icu
, status
);
419 if(U_SUCCESS(*status
)) {
420 u_versionToString(icu
, str
);
421 return stringToStringBuffer(target
,targetCapacity
,str
,status
);
428 #if !UCONFIG_NO_FORMATTING
430 paramTimezoneDefault(const USystemParams
* /* param */, char *target
, int32_t targetCapacity
, UErrorCode
*status
) {
431 if(U_FAILURE(*status
))return 0;
436 len
= ucal_getDefaultTimeZone(buf
, 100, status
);
437 if(U_SUCCESS(*status
)&&len
>0) {
438 u_UCharsToChars(buf
, buf2
, len
+1);
439 return stringToStringBuffer(target
,targetCapacity
, buf2
,status
);
447 paramLocaleDefaultBcp47(const USystemParams
* /* param */, char *target
, int32_t targetCapacity
, UErrorCode
*status
) {
448 if(U_FAILURE(*status
))return 0;
449 const char *def
= uloc_getDefault();
450 return uloc_toLanguageTag(def
,target
,targetCapacity
,FALSE
,status
);
454 /* simple 1-liner param functions */
455 #define STRING_PARAM(func, str) U_CAPI int32_t \
456 func(const USystemParams *, char *target, int32_t targetCapacity, UErrorCode *status) \
457 { return stringToStringBuffer(target,targetCapacity,(str),status); }
459 STRING_PARAM(paramIcudataPath
, u_getDataDirectory())
460 STRING_PARAM(paramPlatform
, udbg_getPlatform())
461 STRING_PARAM(paramLocaleDefault
, uloc_getDefault())
462 #if !UCONFIG_NO_CONVERSION
463 STRING_PARAM(paramConverterDefault
, ucnv_getDefaultName())
466 #if !UCONFIG_NO_FORMATTING
467 STRING_PARAM(paramTimezoneVersion
, ucal_getTZDataVersion(status
))
470 static USystemParams systemParams
[] = {
471 { "copyright", paramStatic
, U_COPYRIGHT_STRING
,0 },
472 { "product", paramStatic
, "icu4c",0 },
473 { "product.full", paramStatic
, "International Components for Unicode for C/C++",0 },
474 { "version", paramStatic
, U_ICU_VERSION
,0 },
475 { "version.unicode", paramStatic
, U_UNICODE_VERSION
,0 },
476 { "platform.number", paramInteger
, "d",U_PLATFORM
},
477 { "platform.type", paramPlatform
, NULL
,0},
478 { "locale.default", paramLocaleDefault
, NULL
, 0},
479 { "locale.default.bcp47", paramLocaleDefaultBcp47
, NULL
, 0},
480 #if !UCONFIG_NO_CONVERSION
481 { "converter.default", paramConverterDefault
, NULL
, 0},
483 { "icudata.name", paramStatic
, U_ICUDATA_NAME
, 0},
484 { "icudata.path", paramIcudataPath
, NULL
, 0},
486 { "cldr.version", paramCldrVersion
, NULL
, 0},
488 #if !UCONFIG_NO_FORMATTING
489 { "tz.version", paramTimezoneVersion
, NULL
, 0},
490 { "tz.default", paramTimezoneDefault
, NULL
, 0},
493 { "cpu.bits", paramInteger
, "d", (sizeof(void*))*8},
494 { "cpu.big_endian", paramInteger
, "b", U_IS_BIG_ENDIAN
},
495 { "os.wchar_width", paramInteger
, "d", U_SIZEOF_WCHAR_T
},
496 { "os.charset_family", paramInteger
, "d", U_CHARSET_FAMILY
},
498 { "os.host", paramStatic
, U_HOST
, 0},
500 #if defined (U_BUILD)
501 { "build.build", paramStatic
, U_BUILD
, 0},
504 { "build.cc", paramStatic
, U_CC
, 0},
507 { "build.cxx", paramStatic
, U_CXX
, 0},
509 #if defined (CYGWINMSVC)
510 { "build.cygwinmsvc", paramInteger
, "b", 1},
517 #define U_SYSPARAM_COUNT (sizeof(systemParams)/sizeof(systemParams[0]))
519 U_CAPI
const char *udbg_getSystemParameterNameByIndex(int32_t i
) {
520 if(i
>=0 && i
< (int32_t)U_SYSPARAM_COUNT
) {
521 return systemParams
[i
].paramName
;
528 U_CAPI
int32_t udbg_getSystemParameterValueByIndex(int32_t i
, char *buffer
, int32_t bufferCapacity
, UErrorCode
*status
) {
529 if(i
>=0 && i
< (int32_t)U_SYSPARAM_COUNT
) {
530 return systemParams
[i
].paramFunction(&(systemParams
[i
]),buffer
,bufferCapacity
,status
);
536 U_CAPI
void udbg_writeIcuInfo(FILE *out
) {
538 /* todo: API for writing DTD? */
539 fprintf(out
, " <icuSystemParams type=\"icu4c\">\n");
540 const char *paramName
;
541 for(int32_t i
=0;(paramName
=udbg_getSystemParameterNameByIndex(i
))!=NULL
;i
++) {
542 UErrorCode status2
= U_ZERO_ERROR
;
543 udbg_getSystemParameterValueByIndex(i
, str
,2000,&status2
);
544 if(U_SUCCESS(status2
)) {
545 fprintf(out
," <param name=\"%s\">%s</param>\n", paramName
,str
);
547 fprintf(out
," <!-- n=\"%s\" ERROR: %s -->\n", paramName
, u_errorName(status2
));
550 fprintf(out
, " </icuSystemParams>\n");