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