blob: dcf71b28872f6925ff79740edacbe2aee0881c11 [file] [log] [blame]
Jungshik Shin87232d82017-05-13 21:10:13 -07001// © 2016 and later: Unicode, Inc. and others.
Jungshik Shin5feb9ad2016-10-21 12:52:48 -07002// License & terms of use: http://www.unicode.org/copyright.html
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00003/********************************************************************
4 * COPYRIGHT:
Jungshik Shin5feb9ad2016-10-21 12:52:48 -07005 * Copyright (c) 2007-2016, International Business Machines Corporation and
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00006 * others. All Rights Reserved.
7 ********************************************************************/
8
9#include "udbgutil.h"
10#include <string.h>
11#include "ustr_imp.h"
Jungshik Shin5feb9ad2016-10-21 12:52:48 -070012#include "cmemory.h"
jshin@chromium.org6f31ac32014-03-26 22:15:14 +000013#include "cstring.h"
14#include "putilimp.h"
15#include "unicode/ulocdata.h"
16#include "unicode/ucnv.h"
17#include "unicode/unistr.h"
Jungshik Shin5feb9ad2016-10-21 12:52:48 -070018#include "cstr.h"
jshin@chromium.org6f31ac32014-03-26 22:15:14 +000019
20/*
21To add a new enum type
22 (For example: UShoeSize with values USHOE_WIDE=0, USHOE_REGULAR, USHOE_NARROW, USHOE_COUNT)
23
Jungshik Shin70f82502016-01-29 00:32:36 -080024 0. Make sure that all lines you add are protected with appropriate uconfig guards,
25 such as '#if !UCONFIG_NO_SHOES'.
jshin@chromium.org6f31ac32014-03-26 22:15:14 +000026 1. udbgutil.h: add UDBG_UShoeSize to the UDebugEnumType enum before UDBG_ENUM_COUNT
27 ( The subsequent steps involve this file, udbgutil.cpp )
28 2. Find the marker "Add new enum types above this line"
29 3. Before that marker, add a #include of any header file you need.
30 4. Each enum type has three things in this section: a #define, a count_, and an array of Fields.
31 It may help to copy and paste a previous definition.
32 5. In the case of the USHOE_... strings above, "USHOE_" is common to all values- six characters
33 " #define LEN_USHOE 6 "
34 6 characters will strip off "USHOE_" leaving enum values of WIDE, REGULAR, and NARROW.
35 6. Define the 'count_' variable, with the number of enum values. If the enum has a _MAX or _COUNT value,
36 that can be helpful for automatically defining the count. Otherwise define it manually.
37 " static const int32_t count_UShoeSize = USHOE_COUNT; "
38 7. Define the field names, in order.
39 " static const Field names_UShoeSize[] = {
40 " FIELD_NAME_STR( LEN_USHOE, USHOE_WIDE ),
41 " FIELD_NAME_STR( LEN_USHOE, USHOE_REGULAR ),
42 " FIELD_NAME_STR( LEN_USHOE, USHOE_NARROW ),
43 " };
44 ( The following command was usedfor converting ucol.h into partially correct entities )
45 grep "^[ ]*UCOL" < unicode/ucol.h |
46 sed -e 's%^[ ]*\([A-Z]*\)_\([A-Z_]*\).*% FIELD_NAME_STR( LEN_\1, \1_\2 ),%g'
47 8. Now, a bit farther down, add the name of the enum itself to the end of names_UDebugEnumType
48 ( UDebugEnumType is an enum, too!)
49 names_UDebugEnumType[] { ...
50 " FIELD_NAME_STR( LEN_UDBG, UDBG_UShoeSize ), "
51 9. Find the function _udbg_enumCount and add the count macro:
52 " COUNT_CASE(UShoeSize)
53 10. Find the function _udbg_enumFields and add the field macro:
54 " FIELD_CASE(UShoeSize)
55 11. verify that your test code, and Java data generation, works properly.
56*/
57
58/**
59 * Structure representing an enum value
60 */
61struct Field {
62 int32_t prefix; /**< how many characters to remove in the prefix - i.e. UCHAR_ = 5 */
63 const char *str; /**< The actual string value */
64 int32_t num; /**< The numeric value */
65};
66
67/**
jshin@chromium.org6f31ac32014-03-26 22:15:14 +000068 * Define another field name. Used in an array of Field s
69 * @param y the common prefix length (i.e. 6 for "USHOE_" )
70 * @param x the actual enum value - it will be copied in both string and symbolic form.
71 * @see Field
72 */
73#define FIELD_NAME_STR(y,x) { y, #x, x }
74
75
76// TODO: Currently, this whole functionality goes away with UCONFIG_NO_FORMATTING. Should be split up.
77#if !UCONFIG_NO_FORMATTING
78
79// Calendar
80#include "unicode/ucal.h"
81
82// 'UCAL_' = 5
83#define LEN_UCAL 5 /* UCAL_ */
84static const int32_t count_UCalendarDateFields = UCAL_FIELD_COUNT;
85static const Field names_UCalendarDateFields[] =
86{
87 FIELD_NAME_STR( LEN_UCAL, UCAL_ERA ),
88 FIELD_NAME_STR( LEN_UCAL, UCAL_YEAR ),
89 FIELD_NAME_STR( LEN_UCAL, UCAL_MONTH ),
90 FIELD_NAME_STR( LEN_UCAL, UCAL_WEEK_OF_YEAR ),
91 FIELD_NAME_STR( LEN_UCAL, UCAL_WEEK_OF_MONTH ),
92 FIELD_NAME_STR( LEN_UCAL, UCAL_DATE ),
93 FIELD_NAME_STR( LEN_UCAL, UCAL_DAY_OF_YEAR ),
94 FIELD_NAME_STR( LEN_UCAL, UCAL_DAY_OF_WEEK ),
95 FIELD_NAME_STR( LEN_UCAL, UCAL_DAY_OF_WEEK_IN_MONTH ),
96 FIELD_NAME_STR( LEN_UCAL, UCAL_AM_PM ),
97 FIELD_NAME_STR( LEN_UCAL, UCAL_HOUR ),
98 FIELD_NAME_STR( LEN_UCAL, UCAL_HOUR_OF_DAY ),
99 FIELD_NAME_STR( LEN_UCAL, UCAL_MINUTE ),
100 FIELD_NAME_STR( LEN_UCAL, UCAL_SECOND ),
101 FIELD_NAME_STR( LEN_UCAL, UCAL_MILLISECOND ),
102 FIELD_NAME_STR( LEN_UCAL, UCAL_ZONE_OFFSET ),
103 FIELD_NAME_STR( LEN_UCAL, UCAL_DST_OFFSET ),
104 FIELD_NAME_STR( LEN_UCAL, UCAL_YEAR_WOY ),
105 FIELD_NAME_STR( LEN_UCAL, UCAL_DOW_LOCAL ),
106 FIELD_NAME_STR( LEN_UCAL, UCAL_EXTENDED_YEAR ),
107 FIELD_NAME_STR( LEN_UCAL, UCAL_JULIAN_DAY ),
108 FIELD_NAME_STR( LEN_UCAL, UCAL_MILLISECONDS_IN_DAY ),
109 FIELD_NAME_STR( LEN_UCAL, UCAL_IS_LEAP_MONTH ),
110};
111
112
113static const int32_t count_UCalendarMonths = UCAL_UNDECIMBER+1;
114static const Field names_UCalendarMonths[] =
115{
116 FIELD_NAME_STR( LEN_UCAL, UCAL_JANUARY ),
117 FIELD_NAME_STR( LEN_UCAL, UCAL_FEBRUARY ),
118 FIELD_NAME_STR( LEN_UCAL, UCAL_MARCH ),
119 FIELD_NAME_STR( LEN_UCAL, UCAL_APRIL ),
120 FIELD_NAME_STR( LEN_UCAL, UCAL_MAY ),
121 FIELD_NAME_STR( LEN_UCAL, UCAL_JUNE ),
122 FIELD_NAME_STR( LEN_UCAL, UCAL_JULY ),
123 FIELD_NAME_STR( LEN_UCAL, UCAL_AUGUST ),
124 FIELD_NAME_STR( LEN_UCAL, UCAL_SEPTEMBER ),
125 FIELD_NAME_STR( LEN_UCAL, UCAL_OCTOBER ),
126 FIELD_NAME_STR( LEN_UCAL, UCAL_NOVEMBER ),
127 FIELD_NAME_STR( LEN_UCAL, UCAL_DECEMBER ),
128 FIELD_NAME_STR( LEN_UCAL, UCAL_UNDECIMBER)
129};
130
131#include "unicode/udat.h"
132
133#define LEN_UDAT 5 /* "UDAT_" */
134static const int32_t count_UDateFormatStyle = UDAT_SHORT+1;
135static const Field names_UDateFormatStyle[] =
136{
137 FIELD_NAME_STR( LEN_UDAT, UDAT_FULL ),
138 FIELD_NAME_STR( LEN_UDAT, UDAT_LONG ),
139 FIELD_NAME_STR( LEN_UDAT, UDAT_MEDIUM ),
140 FIELD_NAME_STR( LEN_UDAT, UDAT_SHORT ),
141 /* end regular */
142 /*
143 * negative enums.. leave out for now.
144 FIELD_NAME_STR( LEN_UDAT, UDAT_NONE ),
145 FIELD_NAME_STR( LEN_UDAT, UDAT_PATTERN ),
146 */
147};
148
149#endif
150
151#include "unicode/uloc.h"
152
153#define LEN_UAR 12 /* "ULOC_ACCEPT_" */
154static const int32_t count_UAcceptResult = 3;
155static const Field names_UAcceptResult[] =
156{
157 FIELD_NAME_STR( LEN_UAR, ULOC_ACCEPT_FAILED ),
158 FIELD_NAME_STR( LEN_UAR, ULOC_ACCEPT_VALID ),
159 FIELD_NAME_STR( LEN_UAR, ULOC_ACCEPT_FALLBACK ),
160};
161
162#if !UCONFIG_NO_COLLATION
163#include "unicode/ucol.h"
164#define LEN_UCOL 5 /* UCOL_ */
165static const int32_t count_UColAttributeValue = UCOL_ATTRIBUTE_VALUE_COUNT;
166static const Field names_UColAttributeValue[] = {
167 FIELD_NAME_STR( LEN_UCOL, UCOL_PRIMARY ),
168 FIELD_NAME_STR( LEN_UCOL, UCOL_SECONDARY ),
169 FIELD_NAME_STR( LEN_UCOL, UCOL_TERTIARY ),
170// FIELD_NAME_STR( LEN_UCOL, UCOL_CE_STRENGTH_LIMIT ),
171 FIELD_NAME_STR( LEN_UCOL, UCOL_QUATERNARY ),
172 // gap
173 FIELD_NAME_STR( LEN_UCOL, UCOL_IDENTICAL ),
174// FIELD_NAME_STR( LEN_UCOL, UCOL_STRENGTH_LIMIT ),
175 FIELD_NAME_STR( LEN_UCOL, UCOL_OFF ),
176 FIELD_NAME_STR( LEN_UCOL, UCOL_ON ),
177 // gap
178 FIELD_NAME_STR( LEN_UCOL, UCOL_SHIFTED ),
179 FIELD_NAME_STR( LEN_UCOL, UCOL_NON_IGNORABLE ),
180 // gap
181 FIELD_NAME_STR( LEN_UCOL, UCOL_LOWER_FIRST ),
182 FIELD_NAME_STR( LEN_UCOL, UCOL_UPPER_FIRST ),
183};
184
185#endif
186
187
Jungshik Shin70f82502016-01-29 00:32:36 -0800188#if UCONFIG_ENABLE_PLUGINS
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000189#include "unicode/icuplug.h"
190
191#define LEN_UPLUG_REASON 13 /* UPLUG_REASON_ */
192static const int32_t count_UPlugReason = UPLUG_REASON_COUNT;
193static const Field names_UPlugReason[] = {
194 FIELD_NAME_STR( LEN_UPLUG_REASON, UPLUG_REASON_QUERY ),
195 FIELD_NAME_STR( LEN_UPLUG_REASON, UPLUG_REASON_LOAD ),
196 FIELD_NAME_STR( LEN_UPLUG_REASON, UPLUG_REASON_UNLOAD ),
197};
198
199#define LEN_UPLUG_LEVEL 12 /* UPLUG_LEVEL_ */
200static const int32_t count_UPlugLevel = UPLUG_LEVEL_COUNT;
201static const Field names_UPlugLevel[] = {
202 FIELD_NAME_STR( LEN_UPLUG_LEVEL, UPLUG_LEVEL_INVALID ),
203 FIELD_NAME_STR( LEN_UPLUG_LEVEL, UPLUG_LEVEL_UNKNOWN ),
204 FIELD_NAME_STR( LEN_UPLUG_LEVEL, UPLUG_LEVEL_LOW ),
205 FIELD_NAME_STR( LEN_UPLUG_LEVEL, UPLUG_LEVEL_HIGH ),
206};
Jungshik Shin70f82502016-01-29 00:32:36 -0800207#endif
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000208
209#define LEN_UDBG 5 /* "UDBG_" */
210static const int32_t count_UDebugEnumType = UDBG_ENUM_COUNT;
211static const Field names_UDebugEnumType[] =
212{
213 FIELD_NAME_STR( LEN_UDBG, UDBG_UDebugEnumType ),
214#if !UCONFIG_NO_FORMATTING
215 FIELD_NAME_STR( LEN_UDBG, UDBG_UCalendarDateFields ),
216 FIELD_NAME_STR( LEN_UDBG, UDBG_UCalendarMonths ),
217 FIELD_NAME_STR( LEN_UDBG, UDBG_UDateFormatStyle ),
218#endif
Jungshik Shin70f82502016-01-29 00:32:36 -0800219#if UCONFIG_ENABLE_PLUGINS
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000220 FIELD_NAME_STR( LEN_UDBG, UDBG_UPlugReason ),
221 FIELD_NAME_STR( LEN_UDBG, UDBG_UPlugLevel ),
Jungshik Shin70f82502016-01-29 00:32:36 -0800222#endif
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000223 FIELD_NAME_STR( LEN_UDBG, UDBG_UAcceptResult ),
224#if !UCONFIG_NO_COLLATION
225 FIELD_NAME_STR( LEN_UDBG, UDBG_UColAttributeValue ),
226#endif
227};
228
229
230// --- Add new enum types above this line ---
231
Jungshik Shin5feb9ad2016-10-21 12:52:48 -0700232#define COUNT_CASE(x) case UDBG_##x: return (actual?count_##x:UPRV_LENGTHOF(names_##x));
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000233#define COUNT_FAIL_CASE(x) case UDBG_##x: return -1;
234
235#define FIELD_CASE(x) case UDBG_##x: return names_##x;
236#define FIELD_FAIL_CASE(x) case UDBG_##x: return NULL;
237
238// low level
239
240/**
241 * @param type type of item
Frank Tang1f164ee2022-11-08 12:31:27 -0800242 * @param actual true: for the actual enum's type (UCAL_FIELD_COUNT, etc), or false for the string count
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000243 */
244static int32_t _udbg_enumCount(UDebugEnumType type, UBool actual) {
245 switch(type) {
246 COUNT_CASE(UDebugEnumType)
247#if !UCONFIG_NO_FORMATTING
248 COUNT_CASE(UCalendarDateFields)
249 COUNT_CASE(UCalendarMonths)
250 COUNT_CASE(UDateFormatStyle)
251#endif
Jungshik Shin70f82502016-01-29 00:32:36 -0800252#if UCONFIG_ENABLE_PLUGINS
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000253 COUNT_CASE(UPlugReason)
254 COUNT_CASE(UPlugLevel)
Jungshik Shin70f82502016-01-29 00:32:36 -0800255#endif
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000256 COUNT_CASE(UAcceptResult)
257#if !UCONFIG_NO_COLLATION
258 COUNT_CASE(UColAttributeValue)
259#endif
260 // COUNT_FAIL_CASE(UNonExistentEnum)
261 default:
262 return -1;
263 }
264}
265
266static const Field* _udbg_enumFields(UDebugEnumType type) {
267 switch(type) {
268 FIELD_CASE(UDebugEnumType)
269#if !UCONFIG_NO_FORMATTING
270 FIELD_CASE(UCalendarDateFields)
271 FIELD_CASE(UCalendarMonths)
272 FIELD_CASE(UDateFormatStyle)
273#endif
Jungshik Shin70f82502016-01-29 00:32:36 -0800274#if UCONFIG_ENABLE_PLUGINS
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000275 FIELD_CASE(UPlugReason)
276 FIELD_CASE(UPlugLevel)
Jungshik Shin70f82502016-01-29 00:32:36 -0800277#endif
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000278 FIELD_CASE(UAcceptResult)
Jungshik Shin70f82502016-01-29 00:32:36 -0800279 // FIELD_FAIL_CASE(UNonExistentEnum)
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000280#if !UCONFIG_NO_COLLATION
281 FIELD_CASE(UColAttributeValue)
282#endif
283 default:
284 return NULL;
285 }
286}
287
288// implementation
289
290int32_t udbg_enumCount(UDebugEnumType type) {
Frank Tang1f164ee2022-11-08 12:31:27 -0800291 return _udbg_enumCount(type, false);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000292}
293
294int32_t udbg_enumExpectedCount(UDebugEnumType type) {
Frank Tang1f164ee2022-11-08 12:31:27 -0800295 return _udbg_enumCount(type, true);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000296}
297
298const char * udbg_enumName(UDebugEnumType type, int32_t field) {
299 if(field<0 ||
Frank Tang1f164ee2022-11-08 12:31:27 -0800300 field>=_udbg_enumCount(type,false)) { // also will catch unsupported items
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000301 return NULL;
302 } else {
303 const Field *fields = _udbg_enumFields(type);
304 if(fields == NULL) {
305 return NULL;
306 } else {
307 return fields[field].str + fields[field].prefix;
308 }
309 }
310}
311
312int32_t udbg_enumArrayValue(UDebugEnumType type, int32_t field) {
313 if(field<0 ||
Frank Tang1f164ee2022-11-08 12:31:27 -0800314 field>=_udbg_enumCount(type,false)) { // also will catch unsupported items
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000315 return -1;
316 } else {
317 const Field *fields = _udbg_enumFields(type);
318 if(fields == NULL) {
319 return -1;
320 } else {
321 return fields[field].num;
322 }
323 }
324}
325
326int32_t udbg_enumByName(UDebugEnumType type, const char *value) {
Frank Tang1f164ee2022-11-08 12:31:27 -0800327 if(type<0||type>=_udbg_enumCount(UDBG_UDebugEnumType, true)) {
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000328 return -1; // type out of range
329 }
330 const Field *fields = _udbg_enumFields(type);
Jungshik Shin70f82502016-01-29 00:32:36 -0800331 if (fields != NULL) {
Frank Tang1f164ee2022-11-08 12:31:27 -0800332 for(int32_t field = 0;field<_udbg_enumCount(type, false);field++) {
Jungshik Shin70f82502016-01-29 00:32:36 -0800333 if(!strcmp(value, fields[field].str + fields[field].prefix)) {
334 return fields[field].num;
335 }
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000336 }
Jungshik Shin70f82502016-01-29 00:32:36 -0800337 // try with the prefix
Frank Tang1f164ee2022-11-08 12:31:27 -0800338 for(int32_t field = 0;field<_udbg_enumCount(type, false);field++) {
Jungshik Shin70f82502016-01-29 00:32:36 -0800339 if(!strcmp(value, fields[field].str)) {
340 return fields[field].num;
341 }
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000342 }
343 }
344 // fail
345 return -1;
346}
347
348/* platform info */
349/**
350 * Print the current platform
351 */
352U_CAPI const char *udbg_getPlatform(void)
353{
Jungshik Shin87232d82017-05-13 21:10:13 -0700354#if U_PLATFORM_USES_ONLY_WIN32_API
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000355 return "Windows";
Jungshik Shin87232d82017-05-13 21:10:13 -0700356#elif U_PLATFORM == U_PF_CYGWIN
357 return "Cygwin";
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000358#elif U_PLATFORM == U_PF_UNKNOWN
359 return "unknown";
360#elif U_PLATFORM == U_PF_DARWIN
361 return "Darwin";
362#elif U_PLATFORM == U_PF_BSD
363 return "BSD";
364#elif U_PLATFORM == U_PF_QNX
365 return "QNX";
366#elif U_PLATFORM == U_PF_LINUX
367 return "Linux";
368#elif U_PLATFORM == U_PF_ANDROID
369 return "Android";
370#elif U_PLATFORM == U_PF_CLASSIC_MACOS
371 return "MacOS (Classic)";
372#elif U_PLATFORM == U_PF_OS390
373 return "IBM z";
374#elif U_PLATFORM == U_PF_OS400
375 return "IBM i";
376#else
377 return "Other (POSIX-like)";
378#endif
379}
380
381struct USystemParams;
382
383typedef int32_t U_CALLCONV USystemParameterCallback(const USystemParams *param, char *target, int32_t targetCapacity, UErrorCode *status);
384
385struct USystemParams {
386 const char *paramName;
387 USystemParameterCallback *paramFunction;
388 const char *paramStr;
389 int32_t paramInt;
390};
391
392/* parameter types */
393U_CAPI int32_t
394paramEmpty(const USystemParams * /* param */, char *target, int32_t targetCapacity, UErrorCode *status) {
395 if(U_FAILURE(*status))return 0;
396 return u_terminateChars(target, targetCapacity, 0, status);
397}
398
399U_CAPI int32_t
400paramStatic(const USystemParams *param, char *target, int32_t targetCapacity, UErrorCode *status) {
401 if(param->paramStr==NULL) return paramEmpty(param,target,targetCapacity,status);
402 if(U_FAILURE(*status))return 0;
Jungshik Shin42d50272018-10-24 01:22:09 -0700403 int32_t len = static_cast<int32_t>(uprv_strlen(param->paramStr));
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000404 if(target!=NULL) {
405 uprv_strncpy(target,param->paramStr,uprv_min(len,targetCapacity));
406 }
407 return u_terminateChars(target, targetCapacity, len, status);
408}
409
410static const char *nullString = "(null)";
411
412static int32_t stringToStringBuffer(char *target, int32_t targetCapacity, const char *str, UErrorCode *status) {
413 if(str==NULL) str=nullString;
414
Jungshik Shin42d50272018-10-24 01:22:09 -0700415 int32_t len = static_cast<int32_t>(uprv_strlen(str));
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000416 if (U_SUCCESS(*status)) {
417 if(target!=NULL) {
418 uprv_strncpy(target,str,uprv_min(len,targetCapacity));
419 }
420 } else {
421 const char *s = u_errorName(*status);
Jungshik Shin42d50272018-10-24 01:22:09 -0700422 len = static_cast<int32_t>(uprv_strlen(s));
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000423 if(target!=NULL) {
424 uprv_strncpy(target,s,uprv_min(len,targetCapacity));
425 }
426 }
427 return u_terminateChars(target, targetCapacity, len, status);
428}
429
430static int32_t integerToStringBuffer(char *target, int32_t targetCapacity, int32_t n, int32_t radix, UErrorCode *status) {
431 if(U_FAILURE(*status)) return 0;
432 char str[300];
433 T_CString_integerToString(str,n,radix);
434 return stringToStringBuffer(target,targetCapacity,str,status);
435}
436
437U_CAPI int32_t
438paramInteger(const USystemParams *param, char *target, int32_t targetCapacity, UErrorCode *status) {
439 if(U_FAILURE(*status))return 0;
440 if(param->paramStr==NULL || param->paramStr[0]=='d') {
441 return integerToStringBuffer(target,targetCapacity,param->paramInt, 10,status);
442 } else if(param->paramStr[0]=='x') {
443 return integerToStringBuffer(target,targetCapacity,param->paramInt, 16,status);
444 } else if(param->paramStr[0]=='o') {
445 return integerToStringBuffer(target,targetCapacity,param->paramInt, 8,status);
446 } else if(param->paramStr[0]=='b') {
447 return integerToStringBuffer(target,targetCapacity,param->paramInt, 2,status);
448 } else {
449 *status = U_INTERNAL_PROGRAM_ERROR;
450 return 0;
451 }
452}
453
454
455U_CAPI int32_t
456paramCldrVersion(const USystemParams * /* param */, char *target, int32_t targetCapacity, UErrorCode *status) {
457 if(U_FAILURE(*status))return 0;
458 char str[200]="";
459 UVersionInfo icu;
460
461 ulocdata_getCLDRVersion(icu, status);
462 if(U_SUCCESS(*status)) {
463 u_versionToString(icu, str);
464 return stringToStringBuffer(target,targetCapacity,str,status);
465 } else {
466 return 0;
467 }
468}
469
470
471#if !UCONFIG_NO_FORMATTING
472U_CAPI int32_t
473paramTimezoneDefault(const USystemParams * /* param */, char *target, int32_t targetCapacity, UErrorCode *status) {
474 if(U_FAILURE(*status))return 0;
475 UChar buf[100];
476 char buf2[100];
477 int32_t len;
478
479 len = ucal_getDefaultTimeZone(buf, 100, status);
480 if(U_SUCCESS(*status)&&len>0) {
481 u_UCharsToChars(buf, buf2, len+1);
482 return stringToStringBuffer(target,targetCapacity, buf2,status);
483 } else {
484 return 0;
485 }
486}
487#endif
488
489U_CAPI int32_t
490paramLocaleDefaultBcp47(const USystemParams * /* param */, char *target, int32_t targetCapacity, UErrorCode *status) {
491 if(U_FAILURE(*status))return 0;
492 const char *def = uloc_getDefault();
Frank Tang1f164ee2022-11-08 12:31:27 -0800493 return uloc_toLanguageTag(def,target,targetCapacity,false,status);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000494}
495
496
497/* simple 1-liner param functions */
498#define STRING_PARAM(func, str) U_CAPI int32_t \
499 func(const USystemParams *, char *target, int32_t targetCapacity, UErrorCode *status) \
500 { return stringToStringBuffer(target,targetCapacity,(str),status); }
501
502STRING_PARAM(paramIcudataPath, u_getDataDirectory())
503STRING_PARAM(paramPlatform, udbg_getPlatform())
504STRING_PARAM(paramLocaleDefault, uloc_getDefault())
505#if !UCONFIG_NO_CONVERSION
506STRING_PARAM(paramConverterDefault, ucnv_getDefaultName())
507#endif
508
509#if !UCONFIG_NO_FORMATTING
510STRING_PARAM(paramTimezoneVersion, ucal_getTZDataVersion(status))
511#endif
512
513static const USystemParams systemParams[] = {
514 { "copyright", paramStatic, U_COPYRIGHT_STRING,0 },
515 { "product", paramStatic, "icu4c",0 },
516 { "product.full", paramStatic, "International Components for Unicode for C/C++",0 },
517 { "version", paramStatic, U_ICU_VERSION,0 },
518 { "version.unicode", paramStatic, U_UNICODE_VERSION,0 },
519 { "platform.number", paramInteger, "d",U_PLATFORM},
520 { "platform.type", paramPlatform, NULL ,0},
521 { "locale.default", paramLocaleDefault, NULL, 0},
522 { "locale.default.bcp47", paramLocaleDefaultBcp47, NULL, 0},
523#if !UCONFIG_NO_CONVERSION
524 { "converter.default", paramConverterDefault, NULL, 0},
525#endif
526 { "icudata.name", paramStatic, U_ICUDATA_NAME, 0},
527 { "icudata.path", paramIcudataPath, NULL, 0},
528
529 { "cldr.version", paramCldrVersion, NULL, 0},
530
531#if !UCONFIG_NO_FORMATTING
532 { "tz.version", paramTimezoneVersion, NULL, 0},
533 { "tz.default", paramTimezoneDefault, NULL, 0},
534#endif
535
536 { "cpu.bits", paramInteger, "d", (sizeof(void*))*8},
537 { "cpu.big_endian", paramInteger, "b", U_IS_BIG_ENDIAN},
538 { "os.wchar_width", paramInteger, "d", U_SIZEOF_WCHAR_T},
539 { "os.charset_family", paramInteger, "d", U_CHARSET_FAMILY},
540#if defined (U_HOST)
541 { "os.host", paramStatic, U_HOST, 0},
542#endif
543#if defined (U_BUILD)
544 { "build.build", paramStatic, U_BUILD, 0},
545#endif
546#if defined (U_CC)
547 { "build.cc", paramStatic, U_CC, 0},
548#endif
549#if defined (U_CXX)
550 { "build.cxx", paramStatic, U_CXX, 0},
551#endif
552#if defined (CYGWINMSVC)
553 { "build.cygwinmsvc", paramInteger, "b", 1},
554#endif
555 { "uconfig.internal_digitlist", paramInteger, "b", 1}, /* always 1 */
556 { "uconfig.have_parseallinput", paramInteger, "b", UCONFIG_HAVE_PARSEALLINPUT},
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000557
558
559};
560
Jungshik Shin5feb9ad2016-10-21 12:52:48 -0700561#define U_SYSPARAM_COUNT UPRV_LENGTHOF(systemParams)
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000562
563U_CAPI const char *udbg_getSystemParameterNameByIndex(int32_t i) {
564 if(i>=0 && i < (int32_t)U_SYSPARAM_COUNT) {
565 return systemParams[i].paramName;
566 } else {
567 return NULL;
568 }
569}
570
571
572U_CAPI int32_t udbg_getSystemParameterValueByIndex(int32_t i, char *buffer, int32_t bufferCapacity, UErrorCode *status) {
573 if(i>=0 && i< (int32_t)U_SYSPARAM_COUNT) {
574 return systemParams[i].paramFunction(&(systemParams[i]),buffer,bufferCapacity,status);
575 } else {
576 return 0;
577 }
578}
579
580U_CAPI void udbg_writeIcuInfo(FILE *out) {
581 char str[2000];
582 /* todo: API for writing DTD? */
583 fprintf(out, " <icuSystemParams type=\"icu4c\">\n");
584 const char *paramName;
585 for(int32_t i=0;(paramName=udbg_getSystemParameterNameByIndex(i))!=NULL;i++) {
586 UErrorCode status2 = U_ZERO_ERROR;
587 udbg_getSystemParameterValueByIndex(i, str,2000,&status2);
588 if(U_SUCCESS(status2)) {
589 fprintf(out," <param name=\"%s\">%s</param>\n", paramName,str);
590 } else {
591 fprintf(out," <!-- n=\"%s\" ERROR: %s -->\n", paramName, u_errorName(status2));
592 }
593 }
594 fprintf(out, " </icuSystemParams>\n");
595}
596
Frank Tangf90543d2020-10-30 19:02:04 -0700597#define UNICODE_BUG_URL "https://unicode-org.atlassian.net/browse/"
598#define OLD_CLDR_PREFIX "cldrbug:"
599#define CLDR_BUG_PREFIX "CLDR-"
600#define ICU_BUG_PREFIX "ICU-"
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000601
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000602
603
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000604#include <set>
605#include <map>
606#include <string>
607#include <ostream>
608#include <iostream>
609
610class KnownIssues {
611public:
612 KnownIssues();
613 ~KnownIssues();
614 void add(const char *ticket, const char *where, const UChar *msg, UBool *firstForTicket, UBool *firstForWhere);
615 void add(const char *ticket, const char *where, const char *msg, UBool *firstForTicket, UBool *firstForWhere);
616 UBool print();
617private:
618 std::map< std::string,
619 std::map < std::string, std::set < std::string > > > fTable;
620};
621
622KnownIssues::KnownIssues()
623 : fTable()
624{
625}
626
627KnownIssues::~KnownIssues()
628{
629}
630
Frank Tangf90543d2020-10-30 19:02:04 -0700631/**
632 * Map cldr:1234 to CLDR-1234
633 * Map 1234 to ICU-1234
634 */
635static std::string mapTicketId(const char *ticketStr) {
636 std::string ticket(ticketStr);
637 // TODO: Can remove this function once all logKnownIssue calls are switched over
638 // to the ICU-1234 and CLDR-1234 format.
639 if(ticket.rfind(OLD_CLDR_PREFIX) == 0) {
640 // map cldrbug:1234 to CLDR-1234
641 ticket.replace(0, uprv_strlen(OLD_CLDR_PREFIX), CLDR_BUG_PREFIX);
642 } else if(::isdigit(ticket[0])) {
643 // map 1234 to ICU-1234
644 ticket.insert(0, ICU_BUG_PREFIX);
645 }
646 return ticket;
647}
648
649void KnownIssues::add(const char *ticketStr, const char *where, const UChar *msg, UBool *firstForTicket, UBool *firstForWhere)
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000650{
Frank Tangf90543d2020-10-30 19:02:04 -0700651 const std::string ticket = mapTicketId(ticketStr);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000652 if(fTable.find(ticket) == fTable.end()) {
Frank Tang1f164ee2022-11-08 12:31:27 -0800653 if(firstForTicket!=NULL) *firstForTicket = true;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000654 fTable[ticket] = std::map < std::string, std::set < std::string > >();
655 } else {
Frank Tang1f164ee2022-11-08 12:31:27 -0800656 if(firstForTicket!=NULL) *firstForTicket = false;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000657 }
658 if(where==NULL) return;
659
660 if(fTable[ticket].find(where) == fTable[ticket].end()) {
Frank Tang1f164ee2022-11-08 12:31:27 -0800661 if(firstForWhere!=NULL) *firstForWhere = true;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000662 fTable[ticket][where] = std::set < std::string >();
663 } else {
Frank Tang1f164ee2022-11-08 12:31:27 -0800664 if(firstForWhere!=NULL) *firstForWhere = false;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000665 }
666 if(msg==NULL || !*msg) return;
667
Jungshik Shin5feb9ad2016-10-21 12:52:48 -0700668 const icu::UnicodeString ustr(msg);
669
670 fTable[ticket][where].insert(std::string(icu::CStr(ustr)()));
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000671}
672
Frank Tangf90543d2020-10-30 19:02:04 -0700673void KnownIssues::add(const char *ticketStr, const char *where, const char *msg, UBool *firstForTicket, UBool *firstForWhere)
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000674{
Frank Tangf90543d2020-10-30 19:02:04 -0700675 const std::string ticket = mapTicketId(ticketStr);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000676 if(fTable.find(ticket) == fTable.end()) {
Frank Tang1f164ee2022-11-08 12:31:27 -0800677 if(firstForTicket!=NULL) *firstForTicket = true;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000678 fTable[ticket] = std::map < std::string, std::set < std::string > >();
679 } else {
Frank Tang1f164ee2022-11-08 12:31:27 -0800680 if(firstForTicket!=NULL) *firstForTicket = false;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000681 }
682 if(where==NULL) return;
683
684 if(fTable[ticket].find(where) == fTable[ticket].end()) {
Frank Tang1f164ee2022-11-08 12:31:27 -0800685 if(firstForWhere!=NULL) *firstForWhere = true;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000686 fTable[ticket][where] = std::set < std::string >();
687 } else {
Frank Tang1f164ee2022-11-08 12:31:27 -0800688 if(firstForWhere!=NULL) *firstForWhere = false;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000689 }
690 if(msg==NULL || !*msg) return;
691
692 std::string str(msg);
693 fTable[ticket][where].insert(str);
694}
695
696UBool KnownIssues::print()
697{
698 if(fTable.empty()) {
Frank Tang1f164ee2022-11-08 12:31:27 -0800699 return false;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000700 }
701
702 std::cout << "KNOWN ISSUES" << std::endl;
703 for( std::map< std::string,
704 std::map < std::string, std::set < std::string > > >::iterator i = fTable.begin();
705 i != fTable.end();
706 i++ ) {
Frank Tangf90543d2020-10-30 19:02:04 -0700707 const std::string ticketid = (*i).first;
708 std::cout << "[" << ticketid << "] ";
709 if(ticketid.rfind(ICU_BUG_PREFIX) == 0 || ticketid.rfind(CLDR_BUG_PREFIX) == 0) {
710 // If it's a unicode.org bug.
711 std::cout << UNICODE_BUG_URL << ticketid;
712 } // Else: some other kind of bug. Allow this, but without a URL.
713 std::cout << std::endl;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000714
715 for( std::map< std::string, std::set < std::string > >::iterator ii = (*i).second.begin();
716 ii != (*i).second.end();
717 ii++ ) {
718 std::cout << " " << (*ii).first << std::endl;
719 for ( std::set < std::string >::iterator iii = (*ii).second.begin();
720 iii != (*ii).second.end();
721 iii++ ) {
722 std::cout << " " << '"' << (*iii) << '"' << std::endl;
723 }
724 }
725 }
Frank Tang1f164ee2022-11-08 12:31:27 -0800726 return true;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000727}
728
729U_CAPI void *udbg_knownIssue_openU(void *ptr, const char *ticket, char *where, const UChar *msg, UBool *firstForTicket,
730 UBool *firstForWhere) {
731 KnownIssues *t = static_cast<KnownIssues*>(ptr);
732 if(t==NULL) {
733 t = new KnownIssues();
734 }
735
736 t->add(ticket, where, msg, firstForTicket, firstForWhere);
737
738 return static_cast<void*>(t);
739}
740
741U_CAPI void *udbg_knownIssue_open(void *ptr, const char *ticket, char *where, const char *msg, UBool *firstForTicket,
742 UBool *firstForWhere) {
743 KnownIssues *t = static_cast<KnownIssues*>(ptr);
744 if(t==NULL) {
745 t = new KnownIssues();
746 }
747
748 t->add(ticket, where, msg, firstForTicket, firstForWhere);
749
750 return static_cast<void*>(t);
751}
752
753U_CAPI UBool udbg_knownIssue_print(void *ptr) {
754 KnownIssues *t = static_cast<KnownIssues*>(ptr);
755 if(t==NULL) {
Frank Tang1f164ee2022-11-08 12:31:27 -0800756 return false;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000757 } else {
758 t->print();
Frank Tang1f164ee2022-11-08 12:31:27 -0800759 return true;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000760 }
761}
762
763U_CAPI void udbg_knownIssue_close(void *ptr) {
764 KnownIssues *t = static_cast<KnownIssues*>(ptr);
765 delete t;
766}