+U_CAPI int32_t U_EXPORT2
+uameasfmt_getUnitName( const UAMeasureFormat* measfmt,
+ UAMeasureUnit unit,
+ UChar* result,
+ int32_t resultCapacity,
+ UErrorCode* status )
+{
+ if (U_FAILURE(*status)) {
+ return 0;
+ }
+ if ( ((result==NULL)? resultCapacity!=0: resultCapacity<0) ) {
+ *status = U_ILLEGAL_ARGUMENT_ERROR;
+ return 0;
+ }
+ LocalPointer<const MeasureUnit> munit(createObjectForMeasureUnit(unit, status));
+ if (U_FAILURE(*status)) {
+ return 0;
+ }
+ UnicodeString res;
+ res.setTo(result, 0, resultCapacity);
+ ((MeasureFormat*)measfmt)->getUnitName(munit.getAlias(), res);
+ if (res.isBogus()) {
+ *status = U_MISSING_RESOURCE_ERROR;
+ return 0;
+ }
+ return res.extract(result, resultCapacity, *status);
+}
+
+U_CAPI int32_t U_EXPORT2
+uameasfmt_getMultipleUnitNames( const UAMeasureFormat* measfmt,
+ const UAMeasureUnit* units,
+ int32_t unitCount,
+ UAMeasureNameListStyle listStyle,
+ UChar* result,
+ int32_t resultCapacity,
+ UErrorCode* status )
+{
+ if (U_FAILURE(*status)) {
+ return 0;
+ }
+ if ( ((result==NULL)? resultCapacity!=0: resultCapacity<0) || unitCount <= 0 || unitCount > kMeasuresMax ) {
+ *status = U_ILLEGAL_ARGUMENT_ERROR;
+ return 0;
+ }
+ int32_t i;
+ const MeasureUnit * unitPtrs[kMeasuresMax];
+ for (i = 0; i < unitCount && U_SUCCESS(*status); i++) {
+ unitPtrs[i] = createObjectForMeasureUnit(units[i], status);
+ }
+ if (U_FAILURE(*status)) {
+ while (i-- > 0) {
+ delete unitPtrs[i];
+ }
+ return 0;
+ }
+ UnicodeString res;
+ res.setTo(result, 0, resultCapacity);
+ ((MeasureFormat*)measfmt)->getMultipleUnitNames(unitPtrs, unitCount, listStyle, res);
+ for (i = 0; i < unitCount; i++) {
+ delete unitPtrs[i];
+ }
+ if (res.isBogus()) {
+ *status = U_MISSING_RESOURCE_ERROR;
+ return 0;
+ }
+ return res.extract(result, resultCapacity, *status);
+}
+
+// Temporary hack until we can use the forthcoming C++ MeasureUnitPreferences class for this
+typedef struct {
+ const char* key;
+ int32_t count;
+ UAMeasureUnit units[2];
+} KeyToUnits;
+static const KeyToUnits keyToUnits[] = {
+ { "acre", 1, { UAMEASUNIT_AREA_ACRE } },
+ { "celsius", 1, { UAMEASUNIT_TEMPERATURE_CELSIUS } },
+ { "centimeter", 1, { UAMEASUNIT_LENGTH_CENTIMETER } },
+ { "fahrenheit", 1, { UAMEASUNIT_TEMPERATURE_FAHRENHEIT } },
+ { "foodcalorie", 1, { UAMEASUNIT_ENERGY_FOODCALORIE } },
+ { "foot", 1, { UAMEASUNIT_LENGTH_FOOT } },
+ { "foot inch", 2, { UAMEASUNIT_LENGTH_FOOT, UAMEASUNIT_LENGTH_INCH } },
+ { "gallon", 1, { UAMEASUNIT_VOLUME_GALLON } },
+ { "gram", 1, { UAMEASUNIT_MASS_GRAM } },
+ { "hectare", 1, { UAMEASUNIT_AREA_HECTARE } },
+ { "hectopascal", 1, { UAMEASUNIT_PRESSURE_HECTOPASCAL } },
+ { "inch", 1, { UAMEASUNIT_LENGTH_INCH } },
+ { "inch-hg", 1, { UAMEASUNIT_PRESSURE_INCH_HG } },
+ { "kilocalorie", 1, { UAMEASUNIT_ENERGY_KILOCALORIE } },
+ { "kilogram", 1, { UAMEASUNIT_MASS_KILOGRAM } },
+ { "kilogram gram", 2, { UAMEASUNIT_MASS_KILOGRAM, UAMEASUNIT_MASS_GRAM } },
+ { "kilometer", 1, { UAMEASUNIT_LENGTH_KILOMETER } },
+ { "kilometer-per-hour", 1, { UAMEASUNIT_SPEED_KILOMETER_PER_HOUR } },
+ { "liter-per-100kilometers", 1, { UAMEASUNIT_CONSUMPTION_LITER_PER_100_KILOMETERs } },
+ { "liter-per-kilometer", 1, { UAMEASUNIT_CONSUMPTION_LITER_PER_KILOMETER } },
+ { "liter", 1, { UAMEASUNIT_VOLUME_LITER } },
+ { "meter", 1, { UAMEASUNIT_LENGTH_METER } },
+ { "meter centimeter", 2, { UAMEASUNIT_LENGTH_METER, UAMEASUNIT_LENGTH_CENTIMETER } },
+ { "meter-per-second", 1, { UAMEASUNIT_SPEED_METER_PER_SECOND } },
+ { "mile", 1, { UAMEASUNIT_LENGTH_MILE } },
+ { "mile-per-gallon", 1, { UAMEASUNIT_CONSUMPTION_MILE_PER_GALLON } },
+ { "mile-per-gallon-imperial", 1, { UAMEASUNIT_CONSUMPTION_MILE_PER_GALLON_IMPERIAL } },
+ { "mile-per-hour", 1, { UAMEASUNIT_SPEED_MILE_PER_HOUR } },
+ { "mile-scandinavian", 1, { UAMEASUNIT_LENGTH_MILE_SCANDINAVIAN } },
+ { "millibar", 1, { UAMEASUNIT_PRESSURE_MILLIBAR } },
+ { "milligram-per-deciliter", 1, { UAMEASUNIT_CONCENTRATION_MILLIGRAM_PER_DECILITER } },
+ { "millimeter", 1, { UAMEASUNIT_LENGTH_MILLIMETER } },
+ { "millimeter-of-mercury", 1, { UAMEASUNIT_PRESSURE_MILLIMETER_OF_MERCURY } },
+ { "millimole-per-liter", 1, { UAMEASUNIT_CONCENTRATION_MILLIMOLE_PER_LITER } },
+ { "minute second", 2, { UAMEASUNIT_DURATION_MINUTE, UAMEASUNIT_DURATION_SECOND } },
+ { "pound", 1, { UAMEASUNIT_MASS_POUND } },
+ { "pound ounce", 2, { UAMEASUNIT_MASS_POUND, UAMEASUNIT_MASS_OUNCE } },
+ { "stone pound", 2, { UAMEASUNIT_MASS_STONE, UAMEASUNIT_MASS_POUND } },
+ { "yard", 1, { UAMEASUNIT_LENGTH_YARD } },
+ { "year-person month-person", 2, { UAMEASUNIT_DURATION_YEAR, UAMEASUNIT_DURATION_MONTH } },
+};
+enum { kKeyToUnitsCount = UPRV_LENGTHOF(keyToUnits) };
+
+enum { kCombinedKeyMax = 64, kKeyValueMax = 15 };
+
+static int compareKeyToUnits(const void* searchKey, const void* tableEntry) {
+ return uprv_strncmp(((const KeyToUnits*)searchKey)->key, ((const KeyToUnits*)tableEntry)->key, kCombinedKeyMax);
+}
+
+U_CAPI int32_t U_EXPORT2
+uameasfmt_getUnitsForUsage( const char* locale,
+ const char* category,
+ const char* usage,
+ UAMeasureUnit* units,
+ int32_t unitsCapacity,
+ UErrorCode* status )
+{
+ if (U_FAILURE(*status)) {
+ return 0;
+ }
+ if ( category==NULL || ((units==NULL)? unitsCapacity!=0: unitsCapacity<0) ) {
+ *status = U_ILLEGAL_ARGUMENT_ERROR;
+ return 0;
+ }
+ char combinedKey[kCombinedKeyMax+1] = "";
+ char* combinedKeyPtr;
+ uprv_strncat(combinedKey, category, kCombinedKeyMax);
+ if (usage != NULL) {
+ uprv_strncat(combinedKey, "-", kCombinedKeyMax-uprv_strlen(combinedKey));
+ uprv_strncat(combinedKey, usage, kCombinedKeyMax-uprv_strlen(combinedKey));
+ }
+
+ // get unitPreferenceData bundle
+ UResourceBundle *prefb = ures_openDirect(NULL, "supplementalData", status);
+ ures_getByKey(prefb, "unitPreferenceData", prefb, status);
+ if (U_FAILURE(*status)) {
+ return 0;
+ }
+
+ // Get region to use
+ char region[ULOC_COUNTRY_CAPACITY];
+ UErrorCode localStatus;
+ UBool usedOverride = FALSE;
+ // First check for ms overrides, except in certain categories
+ if (uprv_strcmp(category, "concentr") != 0 && uprv_strcmp(category, "duration") != 0) {
+ char msValue[kKeyValueMax + 1];
+ localStatus = U_ZERO_ERROR;
+ int32_t msValueLen = uloc_getKeywordValue(locale, "ms", msValue, kKeyValueMax, &localStatus);
+ if (U_SUCCESS(localStatus) && msValueLen> 2) {
+ msValue[kKeyValueMax] = 0; // ensure termination
+ if (uprv_strcmp(msValue, "metric") == 0) {
+ uprv_strcpy(region, "001");
+ usedOverride = TRUE;
+ } else if (uprv_strcmp(msValue, "ussystem") == 0) {
+ uprv_strcpy(region, "US");
+ usedOverride = TRUE;
+ } else if (uprv_strcmp(msValue, "uksystem") == 0) {
+ uprv_strcpy(region, "GB");
+ usedOverride = TRUE;
+ }
+ }
+ }
+ if (!usedOverride) {
+ (void)ulocimp_getRegionForSupplementalData(locale, TRUE, region, sizeof(region), status);
+ if (U_FAILURE(*status)) {
+ return 0;
+ }
+ }
+
+ UResourceBundle *unitb = NULL;
+ localStatus = U_ZERO_ERROR;
+ int32_t retval = 0;
+ UResourceBundle *regb = ures_getByKey(prefb, region, NULL, &localStatus);
+ if (U_SUCCESS(localStatus)) {
+ unitb = ures_getByKey(regb, combinedKey, unitb, &localStatus);
+ if (U_FAILURE(localStatus)) {
+ combinedKeyPtr = uprv_strstr(combinedKey, "-informal");
+ if (combinedKeyPtr != NULL) {
+ *combinedKeyPtr = 0;
+ localStatus = U_ZERO_ERROR;
+ unitb = ures_getByKey(regb, combinedKey, unitb, &localStatus);
+ }
+ }
+ } else {
+ combinedKeyPtr = uprv_strstr(combinedKey, "-informal");
+ if (combinedKeyPtr != NULL) {
+ *combinedKeyPtr = 0;
+ }
+ }
+ if (U_FAILURE(localStatus)) {
+ localStatus = U_ZERO_ERROR;
+ regb = ures_getByKey(prefb, "001", regb, &localStatus);
+ if (U_SUCCESS(localStatus)) {
+ unitb = ures_getByKey(regb, combinedKey, unitb, &localStatus);
+ if (U_FAILURE(localStatus)) {
+ combinedKeyPtr = uprv_strstr(combinedKey, "-small");
+ if (combinedKeyPtr == NULL) {
+ combinedKeyPtr = uprv_strstr(combinedKey, "-large");
+ }
+ if (combinedKeyPtr != NULL) {
+ *combinedKeyPtr = 0;
+ localStatus = U_ZERO_ERROR;
+ unitb = ures_getByKey(regb, combinedKey, unitb, &localStatus);
+ }
+ }
+ }
+ }
+ if (U_FAILURE(localStatus)) {
+ *status = localStatus;
+ } else {
+ int32_t keyLen = kCombinedKeyMax;
+ const char* unitsKey = ures_getUTF8String(unitb, combinedKey, &keyLen, FALSE, status);
+ if (U_SUCCESS(*status)) {
+ KeyToUnits searchKey = { unitsKey, 0, { (UAMeasureUnit)0 } };
+ const KeyToUnits* keyToUnitsPtr = (const KeyToUnits*)bsearch(&searchKey, keyToUnits, kKeyToUnitsCount,
+ sizeof(KeyToUnits), compareKeyToUnits);
+ if (keyToUnitsPtr == NULL) {
+ *status = U_MISSING_RESOURCE_ERROR;
+ } else {
+ retval = keyToUnitsPtr->count;
+ if (units != NULL) {
+ if (retval > unitsCapacity) {
+ *status = U_BUFFER_OVERFLOW_ERROR;
+ } else {
+ units[0] = keyToUnitsPtr->units[0];
+ if (retval > 1) {
+ units[1] = keyToUnitsPtr->units[1];
+ }
+ }
+ }
+ }
+ }
+ }
+
+ ures_close(unitb);
+ ures_close(regb);
+ ures_close(prefb);
+
+ return retval;
+}
+
+U_CAPI const char * U_EXPORT2
+uameasfmt_getUnitCategory(UAMeasureUnit unit,
+ UErrorCode* status )
+{
+ if (U_FAILURE(*status)) {
+ return NULL;
+ }
+ LocalPointer<const MeasureUnit> munit(createObjectForMeasureUnit(unit, status));
+ if (U_FAILURE(*status)) {
+ return NULL;
+ }
+ return munit->getType();
+}