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