udbgutil.cpp revision c69afcec261fc345fda8daf46f0ea6b4351dc777
1/********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 2007, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6
7#include "unicode/udbgutil.h"
8
9
10
11struct Field {
12        int32_t prefix; /* how many characters to remove - i.e. UCHAR_ = 5 */
13	const char *str;
14	int32_t num;
15};
16
17#define DBG_ARRAY_COUNT(x) (sizeof(x)/sizeof(x[0]))
18
19
20// The fields
21
22#if !UCONFIG_NO_FORMATTING
23
24#include "unicode/ucal.h"
25// Calendar
26
27
28// 'UCAL_' = 5
29#define FIELD_NAME_STR(y,x)  { y, #x, x }
30
31#define LEN_UCAL 5 /* UCAL_ */
32
33static const int32_t count_UCalendarDateFields = UCAL_FIELD_COUNT;
34
35static const Field names_UCalendarDateFields[] =
36{
37    FIELD_NAME_STR( LEN_UCAL, UCAL_ERA ),
38    FIELD_NAME_STR( LEN_UCAL, UCAL_YEAR ),
39    FIELD_NAME_STR( LEN_UCAL, UCAL_MONTH ),
40    FIELD_NAME_STR( LEN_UCAL, UCAL_WEEK_OF_YEAR ),
41    FIELD_NAME_STR( LEN_UCAL, UCAL_WEEK_OF_MONTH ),
42    FIELD_NAME_STR( LEN_UCAL, UCAL_DATE ),
43    FIELD_NAME_STR( LEN_UCAL, UCAL_DAY_OF_YEAR ),
44    FIELD_NAME_STR( LEN_UCAL, UCAL_DAY_OF_WEEK ),
45    FIELD_NAME_STR( LEN_UCAL, UCAL_DAY_OF_WEEK_IN_MONTH ),
46    FIELD_NAME_STR( LEN_UCAL, UCAL_AM_PM ),
47    FIELD_NAME_STR( LEN_UCAL, UCAL_HOUR ),
48    FIELD_NAME_STR( LEN_UCAL, UCAL_HOUR_OF_DAY ),
49    FIELD_NAME_STR( LEN_UCAL, UCAL_MINUTE ),
50    FIELD_NAME_STR( LEN_UCAL, UCAL_SECOND ),
51    FIELD_NAME_STR( LEN_UCAL, UCAL_MILLISECOND ),
52    FIELD_NAME_STR( LEN_UCAL, UCAL_ZONE_OFFSET ),
53    FIELD_NAME_STR( LEN_UCAL, UCAL_DST_OFFSET ),
54    FIELD_NAME_STR( LEN_UCAL, UCAL_YEAR_WOY ),
55    FIELD_NAME_STR( LEN_UCAL, UCAL_DOW_LOCAL ),
56    FIELD_NAME_STR( LEN_UCAL, UCAL_EXTENDED_YEAR ),
57    FIELD_NAME_STR( LEN_UCAL, UCAL_JULIAN_DAY ),
58    FIELD_NAME_STR( LEN_UCAL, UCAL_MILLISECONDS_IN_DAY ),
59    FIELD_NAME_STR( LEN_UCAL, UCAL_IS_LEAP_MONTH ),
60};
61
62
63static const int32_t count_UCalendarMonths = UCAL_UNDECIMBER+1;
64
65static const Field names_UCalendarMonths[] =
66{
67  FIELD_NAME_STR( LEN_UCAL, UCAL_JANUARY ),
68  FIELD_NAME_STR( LEN_UCAL, UCAL_FEBRUARY ),
69  FIELD_NAME_STR( LEN_UCAL, UCAL_MARCH ),
70  FIELD_NAME_STR( LEN_UCAL, UCAL_APRIL ),
71  FIELD_NAME_STR( LEN_UCAL, UCAL_MAY ),
72  FIELD_NAME_STR( LEN_UCAL, UCAL_JUNE ),
73  FIELD_NAME_STR( LEN_UCAL, UCAL_JULY ),
74  FIELD_NAME_STR( LEN_UCAL, UCAL_AUGUST ),
75  FIELD_NAME_STR( LEN_UCAL, UCAL_SEPTEMBER ),
76  FIELD_NAME_STR( LEN_UCAL, UCAL_OCTOBER ),
77  FIELD_NAME_STR( LEN_UCAL, UCAL_NOVEMBER ),
78  FIELD_NAME_STR( LEN_UCAL, UCAL_DECEMBER ),
79  FIELD_NAME_STR( LEN_UCAL, UCAL_UNDECIMBER)
80};
81
82#include "unicode/udat.h"
83
84#define LEN_UDAT 5 /* "UDAT_" */
85
86static const int32_t count_UDateFormatStyle = UDAT_SHORT+1;
87
88static const Field names_UDateFormatStyle[] =
89{
90        FIELD_NAME_STR( LEN_UDAT, UDAT_FULL ),
91        FIELD_NAME_STR( LEN_UDAT, UDAT_LONG ),
92        FIELD_NAME_STR( LEN_UDAT, UDAT_MEDIUM ),
93        FIELD_NAME_STR( LEN_UDAT, UDAT_SHORT ),
94        /* end regular */
95    /*
96     *  negative enums.. leave out for now.
97        FIELD_NAME_STR( LEN_UDAT, UDAT_NONE ),
98        FIELD_NAME_STR( LEN_UDAT, UDAT_IGNORE ),
99     */
100};
101
102
103
104#define LEN_UDBG 5 /* "UDBG_" */
105
106static const int32_t count_UDebugEnumType = UDBG_ENUM_COUNT;
107
108static const Field names_UDebugEnumType[] =
109{
110    FIELD_NAME_STR( LEN_UDBG, UDBG_UDebugEnumType ),
111    FIELD_NAME_STR( LEN_UDBG, UDBG_UCalendarDateFields ),
112    FIELD_NAME_STR( LEN_UDBG, UDBG_UCalendarMonths ),
113    FIELD_NAME_STR( LEN_UDBG, UDBG_UDateFormatStyle ),
114};
115
116
117#define COUNT_CASE(x)  case UDBG_##x: return (actual?count_##x:DBG_ARRAY_COUNT(names_##x));
118#define COUNT_FAIL_CASE(x) case UDBG_##x: return -1;
119
120#define FIELD_CASE(x)  case UDBG_##x: return names_##x;
121#define FIELD_FAIL_CASE(x) case UDBG_##x: return NULL;
122
123#else
124
125#define COUNT_CASE(x)
126#define COUNT_FAIL_CASE(x)
127
128#define FIELD_CASE(X)
129#define FIELD_FAIL_CASE(x)
130
131#endif
132
133// low level
134
135/**
136 * @param type type of item
137 * @param actual TRUE: for the actual enum's type (UCAL_FIELD_COUNT, etc), or FALSE for the string count
138 */
139static int32_t _udbg_enumCount(UDebugEnumType type, UBool actual) {
140	switch(type) {
141		COUNT_CASE(UDebugEnumType)
142		COUNT_CASE(UCalendarDateFields)
143		COUNT_CASE(UCalendarMonths)
144		COUNT_CASE(UDateFormatStyle)
145		// COUNT_FAIL_CASE(UNonExistentEnum)
146	default:
147		return -1;
148	}
149}
150
151static const Field* _udbg_enumFields(UDebugEnumType type) {
152	switch(type) {
153		FIELD_CASE(UDebugEnumType)
154		FIELD_CASE(UCalendarDateFields)
155		FIELD_CASE(UCalendarMonths)
156		FIELD_CASE(UDateFormatStyle)
157		// FIELD_FAIL_CASE(UNonExistentEnum)
158	default:
159		return NULL;
160	}
161}
162
163// implementation
164
165int32_t  udbg_enumCount(UDebugEnumType type) {
166	return _udbg_enumCount(type, FALSE);
167}
168
169int32_t  udbg_enumExpectedCount(UDebugEnumType type) {
170	return _udbg_enumCount(type, TRUE);
171}
172
173const char *  udbg_enumName(UDebugEnumType type, int32_t field) {
174	if(field<0 ||
175				field>=_udbg_enumCount(type,FALSE)) { // also will catch unsupported items
176		return NULL;
177	} else {
178		const Field *fields = _udbg_enumFields(type);
179		if(fields == NULL) {
180			return NULL;
181		} else {
182			return fields[field].str + fields[field].prefix;
183		}
184	}
185}
186
187int32_t  udbg_enumArrayValue(UDebugEnumType type, int32_t field) {
188	if(field<0 ||
189				field>=_udbg_enumCount(type,FALSE)) { // also will catch unsupported items
190		return -1;
191	} else {
192		const Field *fields = _udbg_enumFields(type);
193		if(fields == NULL) {
194			return -1;
195		} else {
196			return fields[field].num;
197		}
198	}
199}
200