Author: pulkomandy Date: 2010-08-04 20:45:06 +0200 (Wed, 04 Aug 2010) New Revision: 37907 Changeset: http://dev.haiku-os.org/changeset/37907 Removed: haiku/trunk/src/kits/locale/langinfo.cpp Modified: haiku/trunk/headers/os/locale/Country.h haiku/trunk/headers/os/locale/Locale.h haiku/trunk/headers/os/locale/LocaleRoster.h haiku/trunk/headers/private/locale/MutableLocaleRoster.h haiku/trunk/src/apps/aboutsystem/AboutSystem.cpp haiku/trunk/src/apps/deskbar/CalendarMenuWindow.cpp haiku/trunk/src/apps/deskbar/TimeView.cpp haiku/trunk/src/apps/deskbar/TimeView.h haiku/trunk/src/bin/dstcheck.cpp haiku/trunk/src/kits/locale/Country.cpp haiku/trunk/src/kits/locale/Jamfile haiku/trunk/src/kits/locale/Locale.cpp haiku/trunk/src/kits/locale/LocaleRoster.cpp haiku/trunk/src/kits/locale/MutableLocaleRoster.cpp haiku/trunk/src/preferences/locale/FormatSettingsView.cpp haiku/trunk/src/preferences/locale/FormatSettingsView.h haiku/trunk/src/preferences/locale/LocaleSettings.cpp haiku/trunk/src/preferences/locale/LocaleWindow.cpp haiku/trunk/src/preferences/time/DateTimeEdit.cpp haiku/trunk/src/preferences/time/ZoneView.cpp Log: * Move the locale-related stuff away from BCountry to BLocale. * Remove the be_locale global object and go through the be_locale_roster instead * Rework the storage of data for the be_locale_roster, since BLocale already holds a BCountry and a BLanguage. Modified: haiku/trunk/headers/os/locale/Country.h =================================================================== --- haiku/trunk/headers/os/locale/Country.h 2010-08-04 18:16:07 UTC (rev 37906) +++ haiku/trunk/headers/os/locale/Country.h 2010-08-04 18:45:06 UTC (rev 37907) @@ -55,69 +55,16 @@ const char* GetLocalizedString(uint32 id) const; - // Date - - status_t FormatDate(char* string, size_t maxSize, - time_t time, bool longFormat); - status_t FormatDate(BString* string, time_t time, - bool longFormat); - status_t FormatDate(BString* string, - int*& fieldPositions, int& fieldCount, - time_t time, bool longFormat); - status_t GetDateFields(BDateElement*& fields, - int& fieldCount, bool longFormat) const; - status_t GetDateFormat(BString&, bool longFormat) const; - status_t SetDateFormat(const char* formatString, - bool longFormat = true); - - int StartOfWeek() const; - - // Time - - status_t FormatTime(char* string, size_t maxSize, - time_t time, bool longFormat); - status_t FormatTime(BString* string, time_t time, - bool longFormat); - status_t FormatTime(BString* string, - int*& fieldPositions, int& fieldCount, - time_t time, bool longFormat); - status_t GetTimeFields(BDateElement*& fields, - int& fieldCount, bool longFormat) const; - - status_t SetTimeFormat(const char* formatString, - bool longFormat = true); - status_t GetTimeFormat(BString& out, - bool longFormat) const; - - // numbers - - status_t FormatNumber(char* string, size_t maxSize, - double value); - status_t FormatNumber(BString* string, double value); - status_t FormatNumber(char* string, size_t maxSize, - int32 value); - status_t FormatNumber(BString* string, int32 value); - // measurements int8 Measurement() const; - // monetary - - ssize_t FormatMonetary(char* string, size_t maxSize, - double value); - ssize_t FormatMonetary(BString* string, double value); - // timezones int GetTimeZones(BList& timezones) const; private: icu_44::Locale* fICULocale; - BString fLongDateFormat; - BString fShortDateFormat; - BString fLongTimeFormat; - BString fShortTimeFormat; }; Modified: haiku/trunk/headers/os/locale/Locale.h =================================================================== --- haiku/trunk/headers/os/locale/Locale.h 2010-08-04 18:16:07 UTC (rev 37906) +++ haiku/trunk/headers/os/locale/Locale.h 2010-08-04 18:45:06 UTC (rev 37907) @@ -17,13 +17,21 @@ class BLocale { public: - BLocale(); + BLocale(const char* languageAndCountryCode + = "en_US"); + BLocale(const BLocale& other); + BLocale& operator=(const BLocale& other); ~BLocale(); const BCollator* Collator() const { return &fCollator; } const BCountry* Country() const { return &fCountry; } const BLanguage* Language() const { return &fLanguage; } + const char* Code() const; + void SetCountry(const BCountry& newCountry); + void SetCollator(const BCollator& newCollator); + void SetLanguage(const char* languageCode); + // see definitions in LocaleStrings.h const char* GetString(uint32 id); @@ -35,17 +43,55 @@ void FormatDateTime(BString* buffer, const char* fmt, time_t value); - // Country short-hands, TODO: all these should go, once the - // Date...Format classes are done - void FormatDate(char* target, size_t maxSize, - time_t value, bool longFormat); - void FormatDate(BString* target, time_t value, + // Date + + status_t FormatDate(char* string, size_t maxSize, + time_t time, bool longFormat); + status_t FormatDate(BString* string, time_t time, bool longFormat); - void FormatTime(char* target, size_t maxSize, - time_t value, bool longFormat); - void FormatTime(BString* target, time_t value, + status_t FormatDate(BString* string, + int*& fieldPositions, int& fieldCount, + time_t time, bool longFormat); + status_t GetDateFields(BDateElement*& fields, + int& fieldCount, bool longFormat) const; + status_t GetDateFormat(BString&, bool longFormat) const; + status_t SetDateFormat(const char* formatString, + bool longFormat = true); + + int StartOfWeek() const; + + // Time + + status_t FormatTime(char* string, size_t maxSize, + time_t time, bool longFormat); + status_t FormatTime(BString* string, time_t time, bool longFormat); + status_t FormatTime(BString* string, + int*& fieldPositions, int& fieldCount, + time_t time, bool longFormat); + status_t GetTimeFields(BDateElement*& fields, + int& fieldCount, bool longFormat) const; + status_t SetTimeFormat(const char* formatString, + bool longFormat = true); + status_t GetTimeFormat(BString& out, + bool longFormat) const; + + // numbers + + status_t FormatNumber(char* string, size_t maxSize, + double value); + status_t FormatNumber(BString* string, double value); + status_t FormatNumber(char* string, size_t maxSize, + int32 value); + status_t FormatNumber(BString* string, int32 value); + + // monetary + + ssize_t FormatMonetary(char* string, size_t maxSize, + double value); + ssize_t FormatMonetary(BString* string, double value); + // Collator short-hands int StringCompare(const char* s1, const char* s2) const; @@ -59,44 +105,16 @@ BCollator fCollator; BCountry fCountry; BLanguage fLanguage; + + icu_44::Locale* fICULocale; + BString fLongDateFormat; + BString fShortDateFormat; + BString fLongTimeFormat; + BString fShortTimeFormat; }; -// global objects -extern BLocale* be_locale; - - -//---------------------------------------------------------------------- -//--- country short-hands inlines --- -inline void -BLocale::FormatDate(char* target, size_t maxSize, time_t timer, bool longFormat) -{ - fCountry.FormatDate(target, maxSize, timer, longFormat); -} - - -inline void -BLocale::FormatDate(BString* target, time_t timer, bool longFormat) -{ - fCountry.FormatDate(target, timer, longFormat); -} - - -inline void -BLocale::FormatTime(char* target, size_t maxSize, time_t timer, bool longFormat) -{ - fCountry.FormatTime(target, maxSize, timer, longFormat); -} - - -inline void -BLocale::FormatTime(BString* target, time_t timer, bool longFormat) -{ - fCountry.FormatTime(target, timer, longFormat); -} - - -//--- locale short-hands inlines --- +//--- collator short-hands inlines --- // #pragma mark - inline int Modified: haiku/trunk/headers/os/locale/LocaleRoster.h =================================================================== --- haiku/trunk/headers/os/locale/LocaleRoster.h 2010-08-04 18:16:07 UTC (rev 37906) +++ haiku/trunk/headers/os/locale/LocaleRoster.h 2010-08-04 18:45:06 UTC (rev 37907) @@ -31,6 +31,7 @@ status_t GetDefaultCollator(BCollator* collator) const; status_t GetDefaultLanguage(BLanguage* language) const; status_t GetDefaultCountry(BCountry* country) const; + status_t GetDefaultLocale(BLocale* locale) const; status_t GetDefaultTimeZone(BTimeZone* timezone) const; status_t GetLanguage(const char* languageCode, Modified: haiku/trunk/headers/private/locale/MutableLocaleRoster.h =================================================================== --- haiku/trunk/headers/private/locale/MutableLocaleRoster.h 2010-08-04 18:16:07 UTC (rev 37906) +++ haiku/trunk/headers/private/locale/MutableLocaleRoster.h 2010-08-04 18:45:06 UTC (rev 37907) @@ -11,6 +11,7 @@ #include <image.h> #include <Language.h> #include <List.h> +#include <Locale.h> #include <Locker.h> #include <LocaleRoster.h> #include <Message.h> @@ -33,6 +34,7 @@ ~MutableLocaleRoster(); status_t SetDefaultCountry(const BCountry& country); + status_t SetDefaultLocale(const BLocale& locale); status_t SetDefaultTimeZone(const BTimeZone& zone); status_t SetPreferredLanguages(const BMessage* message); @@ -106,9 +108,7 @@ BList fCatalogAddOnInfos; BMessage fPreferredLanguages; - BCollator fDefaultCollator; - BCountry fDefaultCountry; - BLanguage fDefaultLanguage; + BLocale fDefaultLocale; BTimeZone fDefaultTimeZone; RosterData(); @@ -122,6 +122,7 @@ const void* right); status_t SetDefaultCountry(const BCountry& country); + status_t SetDefaultLocale(const BLocale& locale); status_t SetDefaultTimeZone(const BTimeZone& zone); status_t SetPreferredLanguages(const BMessage* msg); private: @@ -132,6 +133,7 @@ status_t _SaveTimeSettings(); status_t _SetDefaultCountry(const BCountry& country); + status_t _SetDefaultLocale(const BLocale& locale); status_t _SetDefaultTimeZone(const BTimeZone& zone); status_t _SetPreferredLanguages(const BMessage* msg); Modified: haiku/trunk/src/apps/aboutsystem/AboutSystem.cpp =================================================================== --- haiku/trunk/src/apps/aboutsystem/AboutSystem.cpp 2010-08-04 18:16:07 UTC (rev 37906) +++ haiku/trunk/src/apps/aboutsystem/AboutSystem.cpp 2010-08-04 18:45:06 UTC (rev 37907) @@ -188,8 +188,9 @@ language->GetTranslatedName(rightName); delete language; - return be_locale->Collator()->Compare(leftName.String(), - rightName.String()); + BCollator collator; + be_locale_roster->GetDefaultCollator(&collator); + return collator.Compare(leftName.String(), rightName.String()); } Modified: haiku/trunk/src/apps/deskbar/CalendarMenuWindow.cpp =================================================================== --- haiku/trunk/src/apps/deskbar/CalendarMenuWindow.cpp 2010-08-04 18:16:07 UTC (rev 37906) +++ haiku/trunk/src/apps/deskbar/CalendarMenuWindow.cpp 2010-08-04 18:45:06 UTC (rev 37907) @@ -89,10 +89,10 @@ fCalendarView(NULL), fSuppressFirstClose(true) { - BCountry country; - be_locale_roster->GetDefaultCountry(&country); + BLocale locale; + be_locale_roster->GetDefaultLocale(&locale); BPrivate::week_start startOfWeek - = (BPrivate::week_start)country.StartOfWeek(); + = (BPrivate::week_start)locale.StartOfWeek(); RemoveShortcut('H', B_COMMAND_KEY | B_CONTROL_KEY); AddShortcut('W', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED)); Modified: haiku/trunk/src/apps/deskbar/TimeView.cpp =================================================================== --- haiku/trunk/src/apps/deskbar/TimeView.cpp 2010-08-04 18:16:07 UTC (rev 37906) +++ haiku/trunk/src/apps/deskbar/TimeView.cpp 2010-08-04 18:45:06 UTC (rev 37907) @@ -91,7 +91,7 @@ fLastDateStr[0] = 0; fNeedToUpdate = true; - be_locale_roster->GetDefaultCountry(&fCountry); + be_locale_roster->GetDefaultLocale(&fLocale); } @@ -105,7 +105,7 @@ data->FindBool("interval", &fInterval); fShowingDate = false; - be_locale_roster->GetDefaultCountry(&fCountry); + be_locale_roster->GetDefaultCountry(&fLocale); } #endif @@ -313,7 +313,7 @@ fMinute = time.tm_min; fHour = time.tm_hour; - fCountry.FormatTime(fTimeStr, 64, fTime, fShowSeconds); + fLocale.FormatTime(fTimeStr, 64, fTime, fShowSeconds); } @@ -322,7 +322,7 @@ { char tmp[64]; - fCountry.FormatDate(tmp, 64, fTime, fFullDate && CanShowFullDate()); + fLocale.FormatDate(tmp, 64, fTime, fFullDate && CanShowFullDate()); // remove leading 0 from date when month is less than 10 (MM/DD/YY) // or remove leading 0 from date when day is less than 10 (DD/MM/YY) @@ -465,7 +465,7 @@ void TTimeView::Update() { - be_locale_roster->GetDefaultCountry(&fCountry); + be_locale_roster->GetDefaultLocale(&fLocale); GetCurrentTime(); GetCurrentDate(); Modified: haiku/trunk/src/apps/deskbar/TimeView.h =================================================================== --- haiku/trunk/src/apps/deskbar/TimeView.h 2010-08-04 18:16:07 UTC (rev 37906) +++ haiku/trunk/src/apps/deskbar/TimeView.h 2010-08-04 18:45:06 UTC (rev 37907) @@ -36,7 +36,7 @@ #include <OS.h> -#include <Country.h> +#include <Locale.h> #include <Messenger.h> #include <View.h> @@ -129,7 +129,7 @@ BMessenger fCalendarWindow; BMessageRunner* fLongClickMessageRunner; - BCountry fCountry; // For date and time localizing purposes + BLocale fLocale; // For date and time localizing purposes }; Modified: haiku/trunk/src/bin/dstcheck.cpp =================================================================== --- haiku/trunk/src/bin/dstcheck.cpp 2010-08-04 18:16:07 UTC (rev 37906) +++ haiku/trunk/src/bin/dstcheck.cpp 2010-08-04 18:45:06 UTC (rev 37907) @@ -84,8 +84,8 @@ time(&t); localtime_r(&t, &tm); - BCountry here; - be_locale_roster->GetDefaultCountry(&here); + BLocale here; + be_locale_roster->GetDefaultLocale(&here); here.FormatTime(timestring, 15, t, false); Modified: haiku/trunk/src/kits/locale/Country.cpp =================================================================== --- haiku/trunk/src/kits/locale/Country.cpp 2010-08-04 18:16:07 UTC (rev 37906) +++ haiku/trunk/src/kits/locale/Country.cpp 2010-08-04 18:45:06 UTC (rev 37907) @@ -8,7 +8,6 @@ #include <Country.h> #include <AutoDeleter.h> -#include <CalendarView.h> #include <IconUtils.h> #include <List.h> #include <Resources.h> @@ -16,10 +15,7 @@ #include <TimeZone.h> #include <unicode/datefmt.h> -#include <unicode/dcfmtsym.h> -#include <unicode/decimfmt.h> -#include <unicode/dtfmtsym.h> -#include <unicode/smpdtfmt.h> +#include <unicode/locid.h> #include <ICUWrapper.h> #include <iostream> @@ -28,23 +24,11 @@ #include <new> #include <stdarg.h> #include <stdlib.h> -#include <vector> #define ICU_VERSION icu_44 -using BPrivate::ObjectDeleter; -using BPrivate::B_WEEK_START_MONDAY; -using BPrivate::B_WEEK_START_SUNDAY; - - -static DateFormat* CreateDateFormat(bool longFormat, const Locale& locale, - const BString& format); -static DateFormat* CreateTimeFormat(bool longFormat, const Locale& locale, - const BString& format); - - BCountry::BCountry(const char* languageCode, const char* countryCode) : fICULocale(new ICU_VERSION::Locale(languageCode, countryCode)) @@ -61,11 +45,7 @@ BCountry::BCountry(const BCountry& other) : - fICULocale(new ICU_VERSION::Locale(*other.fICULocale)), - fLongDateFormat(other.fLongDateFormat), - fShortDateFormat(other.fShortDateFormat), - fLongTimeFormat(other.fLongTimeFormat), - fShortTimeFormat(other.fShortTimeFormat) + fICULocale(new ICU_VERSION::Locale(*other.fICULocale)) { } @@ -76,14 +56,8 @@ if (this == &other) return *this; - delete fICULocale; - fICULocale = new ICU_VERSION::Locale(*other.fICULocale); + *fICULocale = *other.fICULocale; - fLongDateFormat = other.fLongDateFormat; - fShortDateFormat = other.fShortDateFormat; - fLongTimeFormat = other.fLongTimeFormat; - fShortTimeFormat = other.fShortTimeFormat; - return *this; } @@ -146,467 +120,6 @@ } -// #pragma mark - Date - - -status_t -BCountry::FormatDate(char* string, size_t maxSize, time_t time, bool longFormat) -{ - ObjectDeleter<DateFormat> dateFormatter = CreateDateFormat(longFormat, - *fICULocale, longFormat ? fLongDateFormat : fShortDateFormat); - if (dateFormatter.Get() == NULL) - return B_NO_MEMORY; - - UnicodeString ICUString; - ICUString = dateFormatter->format((UDate)time * 1000, ICUString); - - CheckedArrayByteSink stringConverter(string, maxSize); - - ICUString.toUTF8(stringConverter); - - if (stringConverter.Overflowed()) - return B_BAD_VALUE; - - return B_OK; -} - - -status_t -BCountry::FormatDate(BString *string, time_t time, bool longFormat) -{ - string->Truncate(0); - // We make the string empty, this way even in cases where ICU fail we at - // least return something sane - ObjectDeleter<DateFormat> dateFormatter = CreateDateFormat(longFormat, - *fICULocale, longFormat ? fLongDateFormat : fShortDateFormat); - if (dateFormatter.Get() == NULL) - return B_NO_MEMORY; - - UnicodeString ICUString; - ICUString = dateFormatter->format((UDate)time * 1000, ICUString); - - BStringByteSink stringConverter(string); - - ICUString.toUTF8(stringConverter); - - return B_OK; -} - - -status_t -BCountry::FormatDate(BString* string, int*& fieldPositions, int& fieldCount, - time_t time, bool longFormat) -{ - string->Truncate(0); - - ObjectDeleter<DateFormat> dateFormatter = CreateDateFormat(longFormat, - *fICULocale, longFormat ? fLongDateFormat : fShortDateFormat); - if (dateFormatter.Get() == NULL) - return B_NO_MEMORY; - - fieldPositions = NULL; - UErrorCode error = U_ZERO_ERROR; - ICU_VERSION::FieldPositionIterator positionIterator; - UnicodeString ICUString; - ICUString = dateFormatter->format((UDate)time * 1000, ICUString, - &positionIterator, error); - - if (error != U_ZERO_ERROR) - return B_ERROR; - - ICU_VERSION::FieldPosition field; - std::vector<int> fieldPosStorage; - fieldCount = 0; - while (positionIterator.next(field)) { - fieldPosStorage.push_back(field.getBeginIndex()); - fieldPosStorage.push_back(field.getEndIndex()); - fieldCount += 2; - } - - fieldPositions = (int*) malloc(fieldCount * sizeof(int)); - - for (int i = 0 ; i < fieldCount ; i++ ) - fieldPositions[i] = fieldPosStorage[i]; - - BStringByteSink stringConverter(string); - - ICUString.toUTF8(stringConverter); - - return B_OK; -} - - -status_t -BCountry::GetDateFormat(BString& format, bool longFormat) const -{ - if (longFormat && fLongDateFormat.Length() > 0) - format = fLongDateFormat; - else if (!longFormat && fShortDateFormat.Length() > 0) - format = fShortDateFormat; - else { - format.Truncate(0); - - ObjectDeleter<DateFormat> dateFormatter = CreateDateFormat(longFormat, - *fICULocale, longFormat ? fLongDateFormat : fShortDateFormat); - if (dateFormatter.Get() == NULL) - return B_NO_MEMORY; - - SimpleDateFormat* dateFormatterImpl - = static_cast<SimpleDateFormat*>(dateFormatter.Get()); - - UnicodeString ICUString; - ICUString = dateFormatterImpl->toPattern(ICUString); - - BStringByteSink stringConverter(&format); - - ICUString.toUTF8(stringConverter); - } - - return B_OK; -} - - -status_t -BCountry::SetDateFormat(const char* formatString, bool longFormat) -{ -printf("FV::SetDateFormat: df='%s'\n", formatString); - if (longFormat) - fLongDateFormat = formatString; - else - fShortDateFormat = formatString; - - return B_OK; -} - - -status_t -BCountry::GetDateFields(BDateElement*& fields, int& fieldCount, - bool longFormat) const -{ - ObjectDeleter<DateFormat> dateFormatter = CreateDateFormat(longFormat, - *fICULocale, longFormat ? fLongDateFormat : fShortDateFormat); - if (dateFormatter.Get() == NULL) - return B_NO_MEMORY; - - fields = NULL; - UErrorCode error = U_ZERO_ERROR; - ICU_VERSION::FieldPositionIterator positionIterator; - UnicodeString ICUString; - time_t now; - ICUString = dateFormatter->format((UDate)time(&now) * 1000, ICUString, - &positionIterator, error); - - if (U_FAILURE(error)) - return B_ERROR; - - ICU_VERSION::FieldPosition field; - std::vector<int> fieldPosStorage; - fieldCount = 0; - while (positionIterator.next(field)) { - fieldPosStorage.push_back(field.getField()); - fieldCount ++; - } - - fields = (BDateElement*) malloc(fieldCount * sizeof(BDateElement)); - - for (int i = 0 ; i < fieldCount ; i++ ) { - switch (fieldPosStorage[i]) { - case UDAT_YEAR_FIELD: - fields[i] = B_DATE_ELEMENT_YEAR; - break; - case UDAT_MONTH_FIELD: - fields[i] = B_DATE_ELEMENT_MONTH; - break; - case UDAT_DATE_FIELD: - fields[i] = B_DATE_ELEMENT_DAY; - break; - default: - fields[i] = B_DATE_ELEMENT_INVALID; - break; - } - } - - return B_OK; -} - - -int -BCountry::StartOfWeek() const -{ - UErrorCode err = U_ZERO_ERROR; - Calendar* c = Calendar::createInstance(*fICULocale, err); - - if (err == U_ZERO_ERROR && c->getFirstDayOfWeek(err) == UCAL_SUNDAY) { - delete c; - return B_WEEK_START_SUNDAY; - } else { - delete c; - // Might be another day, but BeAPI will not handle it - return B_WEEK_START_MONDAY; - } -} - - -// #pragma mark - Time - - -status_t -BCountry::FormatTime(char* string, size_t maxSize, time_t time, bool longFormat) -{ - ObjectDeleter<DateFormat> timeFormatter = CreateTimeFormat(longFormat, - *fICULocale, longFormat ? fLongTimeFormat : fShortTimeFormat); - if (timeFormatter.Get() == NULL) - return B_NO_MEMORY; - - UnicodeString ICUString; - ICUString = timeFormatter->format((UDate)time * 1000, ICUString); - - CheckedArrayByteSink stringConverter(string, maxSize); - - ICUString.toUTF8(stringConverter); - - if (stringConverter.Overflowed()) - return B_BAD_VALUE; - - return B_OK; -} - - -status_t -BCountry::FormatTime(BString* string, time_t time, bool longFormat) -{ - string->Truncate(0); - - ObjectDeleter<DateFormat> timeFormatter = CreateTimeFormat(longFormat, - *fICULocale, longFormat ? fLongTimeFormat : fShortTimeFormat); - if (timeFormatter.Get() == NULL) - return B_NO_MEMORY; - - UnicodeString ICUString; - ICUString = timeFormatter->format((UDate)time * 1000, ICUString); - - BStringByteSink stringConverter(string); - - ICUString.toUTF8(stringConverter); - - return B_OK; -} - - -status_t -BCountry::FormatTime(BString* string, int*& fieldPositions, int& fieldCount, - time_t time, bool longFormat) -{ - string->Truncate(0); - - ObjectDeleter<DateFormat> timeFormatter = CreateTimeFormat(longFormat, - *fICULocale, longFormat ? fLongTimeFormat : fShortTimeFormat); - if (timeFormatter.Get() == NULL) - return B_NO_MEMORY; - - fieldPositions = NULL; - UErrorCode error = U_ZERO_ERROR; - ICU_VERSION::FieldPositionIterator positionIterator; - UnicodeString ICUString; - ICUString = timeFormatter->format((UDate)time * 1000, ICUString, - &positionIterator, error); - - if (error != U_ZERO_ERROR) - return B_ERROR; - - ICU_VERSION::FieldPosition field; - std::vector<int> fieldPosStorage; - fieldCount = 0; - while (positionIterator.next(field)) { - fieldPosStorage.push_back(field.getBeginIndex()); - fieldPosStorage.push_back(field.getEndIndex()); - fieldCount += 2; - } - - fieldPositions = (int*) malloc(fieldCount * sizeof(int)); - - for (int i = 0 ; i < fieldCount ; i++ ) - fieldPositions[i] = fieldPosStorage[i]; - - BStringByteSink stringConverter(string); - - ICUString.toUTF8(stringConverter); - - return B_OK; -} - - -status_t -BCountry::GetTimeFields(BDateElement*& fields, int& fieldCount, - bool longFormat) const -{ - ObjectDeleter<DateFormat> timeFormatter = CreateTimeFormat(longFormat, - *fICULocale, longFormat ? fLongTimeFormat : fShortTimeFormat); - if (timeFormatter.Get() == NULL) - return B_NO_MEMORY; - - fields = NULL; - UErrorCode error = U_ZERO_ERROR; - ICU_VERSION::FieldPositionIterator positionIterator; - UnicodeString ICUString; - time_t now; - ICUString = timeFormatter->format((UDate)time(&now) * 1000, ICUString, - &positionIterator, error); - - if (error != U_ZERO_ERROR) - return B_ERROR; - - ICU_VERSION::FieldPosition field; - std::vector<int> fieldPosStorage; - fieldCount = 0; - while (positionIterator.next(field)) { - fieldPosStorage.push_back(field.getField()); - fieldCount ++; - } - - fields = (BDateElement*) malloc(fieldCount * sizeof(BDateElement)); - - for (int i = 0 ; i < fieldCount ; i++ ) { - switch (fieldPosStorage[i]) { - case UDAT_HOUR_OF_DAY1_FIELD: - case UDAT_HOUR_OF_DAY0_FIELD: - case UDAT_HOUR1_FIELD: - case UDAT_HOUR0_FIELD: - fields[i] = B_DATE_ELEMENT_HOUR; - break; - case UDAT_MINUTE_FIELD: - fields[i] = B_DATE_ELEMENT_MINUTE; - break; - case UDAT_SECOND_FIELD: - fields[i] = B_DATE_ELEMENT_SECOND; - break; - case UDAT_AM_PM_FIELD: - fields[i] = B_DATE_ELEMENT_AM_PM; - break; - default: - fields[i] = B_DATE_ELEMENT_INVALID; - break; - } - } - - return B_OK; -} - - -status_t -BCountry::SetTimeFormat(const char* formatString, bool longFormat) -{ - if (longFormat) - fLongTimeFormat = formatString; - else - fShortTimeFormat = formatString; - - return B_OK; -} - - -status_t -BCountry::GetTimeFormat(BString& format, bool longFormat) const -{ - if (longFormat && fLongTimeFormat.Length() > 0) - format = fLongTimeFormat; - else if (!longFormat && fShortTimeFormat.Length() > 0) - format = fShortTimeFormat; - else { - format.Truncate(0); - - ObjectDeleter<DateFormat> timeFormatter = CreateTimeFormat(longFormat, - *fICULocale, longFormat ? fLongTimeFormat : fShortTimeFormat); - if (timeFormatter.Get() == NULL) - return B_NO_MEMORY; - - SimpleDateFormat* timeFormatterImpl - = static_cast<SimpleDateFormat*>(timeFormatter.Get()); - - UnicodeString ICUString; - ICUString = timeFormatterImpl->toPattern(ICUString); - - BStringByteSink stringConverter(&format); - ICUString.toUTF8(stringConverter); - } - - return B_OK; -} - - -// #pragma mark - Numbers - - -status_t -BCountry::FormatNumber(char* string, size_t maxSize, double value) -{ - BString fullString; - status_t status = FormatNumber(&fullString, value); - if (status == B_OK) - strlcpy(string, fullString.String(), maxSize); - - return status; -} - - -status_t -BCountry::FormatNumber(BString* string, double value) -{ - UErrorCode err = U_ZERO_ERROR; - ObjectDeleter<NumberFormat> numberFormatter = NumberFormat::createInstance( - *fICULocale, NumberFormat::kNumberStyle, err); - - if (numberFormatter.Get() == NULL) - return B_NO_MEMORY; - if (U_FAILURE(err)) - return B_ERROR; - - UnicodeString ICUString; - ICUString = numberFormatter->format(value, ICUString); - - string->Truncate(0); - BStringByteSink stringConverter(string); - ICUString.toUTF8(stringConverter); - - return B_OK; -} - - -status_t -BCountry::FormatNumber(char* string, size_t maxSize, int32 value) -{ - BString fullString; - status_t status = FormatNumber(&fullString, value); - if (status == B_OK) - strlcpy(string, fullString.String(), maxSize); - - return status; -} - - -status_t -BCountry::FormatNumber(BString* string, int32 value) -{ - UErrorCode err = U_ZERO_ERROR; - ObjectDeleter<NumberFormat> numberFormatter = NumberFormat::createInstance( - *fICULocale, NumberFormat::kNumberStyle, err); - - if (numberFormatter.Get() == NULL) - return B_NO_MEMORY; - if (U_FAILURE(err)) - return B_ERROR; - - UnicodeString ICUString; - ICUString = numberFormatter->format((int32_t)value, ICUString); - - string->Truncate(0); - BStringByteSink stringConverter(string); - ICUString.toUTF8(stringConverter); - - return B_OK; -} - - // TODO does ICU even support this ? Is it in the keywords ? int8 BCountry::Measurement() const @@ -615,44 +128,6 @@ } -ssize_t -BCountry::FormatMonetary(char* string, size_t maxSize, double value) -{ - BString fullString; - ssize_t written = FormatMonetary(&fullString, value); - if (written < 0) - return written; - - return strlcpy(string, fullString.String(), maxSize); -} - - -ssize_t -BCountry::FormatMonetary(BString* string, double value) -{ - if (string == NULL) - return B_BAD_VALUE; - - UErrorCode err; - ObjectDeleter<NumberFormat> numberFormatter - = NumberFormat::createCurrencyInstance(*fICULocale, err); - - if (numberFormatter.Get() == NULL) - return B_NO_MEMORY; - if (U_FAILURE(err)) - return B_ERROR; - - UnicodeString ICUString; - ICUString = numberFormatter->format(value, ICUString); - - string->Truncate(0); - BStringByteSink stringConverter(string); - ICUString.toUTF8(stringConverter); - - return string->Length(); -} - - // #pragma mark - Timezones @@ -687,42 +162,3 @@ } -// #pragma mark - Helpers [... truncated: 1423 lines follow ...]