B*Format: make immutable and remove locking
The language and formatting conventions can now only be set when creating the objects. This removed the needs for locking them when formatting to avoid some other thread changing the format while it's being used. Adjust tests and DeskBar TimeView to the API changes.
This commit is contained in:
parent
fa80e7b28f
commit
5d5ec05b1d
@ -36,10 +36,9 @@ enum BWeekday {
|
||||
|
||||
class BDateFormat: public BFormat {
|
||||
public:
|
||||
BDateFormat(
|
||||
const BLanguage* const language = NULL,
|
||||
const BFormattingConventions* const
|
||||
format = NULL);
|
||||
BDateFormat(const BLocale* locale = NULL);
|
||||
BDateFormat(const BLanguage& language,
|
||||
const BFormattingConventions& format);
|
||||
BDateFormat(const BDateFormat &other);
|
||||
virtual ~BDateFormat();
|
||||
|
||||
|
@ -31,10 +31,9 @@ enum BDateElement {
|
||||
|
||||
class BDateTimeFormat : public BFormat {
|
||||
public:
|
||||
BDateTimeFormat(
|
||||
const BLanguage* const language = NULL,
|
||||
const BFormattingConventions* const format
|
||||
= NULL);
|
||||
BDateTimeFormat(const BLocale* locale = NULL);
|
||||
BDateTimeFormat(const BLanguage& language,
|
||||
const BFormattingConventions& conventions);
|
||||
BDateTimeFormat(const BDateTimeFormat &other);
|
||||
virtual ~BDateTimeFormat();
|
||||
|
||||
|
@ -23,16 +23,15 @@ class BDurationFormat : public BFormat {
|
||||
typedef BFormat Inherited;
|
||||
|
||||
public:
|
||||
BDurationFormat(const BLanguage& language,
|
||||
const BFormattingConventions& conventions,
|
||||
const BString& separator = ", ");
|
||||
BDurationFormat(
|
||||
const BString& separator = ", ");
|
||||
BDurationFormat(const BDurationFormat& other);
|
||||
virtual ~BDurationFormat();
|
||||
|
||||
BDurationFormat& operator=(const BDurationFormat& other);
|
||||
|
||||
void SetSeparator(const BString& separator);
|
||||
|
||||
virtual status_t SetLanguage(const BLanguage& language);
|
||||
status_t SetTimeZone(const BTimeZone* timeZone);
|
||||
|
||||
status_t Format(BString& buffer,
|
||||
|
@ -43,25 +43,26 @@ class BLocale;
|
||||
|
||||
class BFormat {
|
||||
public:
|
||||
status_t SetLocale(const BLocale& locale);
|
||||
virtual status_t SetLanguage(const BLanguage& newLanguage);
|
||||
virtual status_t SetFormattingConventions(
|
||||
const BFormattingConventions&
|
||||
conventions);
|
||||
|
||||
status_t InitCheck() const;
|
||||
status_t InitCheck() const;
|
||||
protected:
|
||||
BFormat();
|
||||
BFormat(const BFormat& other);
|
||||
virtual ~BFormat();
|
||||
BFormat(const BLocale* locale = NULL);
|
||||
BFormat(const BLanguage& language,
|
||||
const BFormattingConventions& conventions);
|
||||
|
||||
BFormat& operator=(const BFormat& other);
|
||||
BFormat(const BFormat& other);
|
||||
virtual ~BFormat();
|
||||
|
||||
private:
|
||||
BFormat& operator=(const BFormat& other);
|
||||
|
||||
status_t _Initialize(const BLocale& locale);
|
||||
status_t _Initialize(const BLanguage& language,
|
||||
const BFormattingConventions& conventions);
|
||||
|
||||
protected:
|
||||
mutable BLocker fLock;
|
||||
BFormattingConventions fConventions;
|
||||
BLanguage fLanguage;
|
||||
status_t fInitStatus;
|
||||
BLanguage fLanguage;
|
||||
status_t fInitStatus;
|
||||
};
|
||||
|
||||
|
||||
|
@ -17,23 +17,19 @@ namespace icu {
|
||||
|
||||
class BMessageFormat: public BFormat {
|
||||
public:
|
||||
BMessageFormat(const BLanguage& language,
|
||||
const BString pattern);
|
||||
BMessageFormat(const BString pattern);
|
||||
~BMessageFormat();
|
||||
|
||||
status_t InitCheck();
|
||||
|
||||
status_t SetLanguage(const BLanguage& newLanguage);
|
||||
status_t SetFormattingConventions(
|
||||
const BFormattingConventions&
|
||||
conventions);
|
||||
|
||||
status_t Format(BString& buffer, const int32 arg) const;
|
||||
|
||||
private:
|
||||
status_t _Initialize(const icu::UnicodeString&);
|
||||
|
||||
private:
|
||||
status_t fInitStatus;
|
||||
icu::MessageFormat* fFormatter;
|
||||
};
|
||||
|
||||
|
@ -23,8 +23,6 @@ public:
|
||||
BNumberFormat(const BNumberFormat &other);
|
||||
~BNumberFormat();
|
||||
|
||||
BNumberFormat& operator=(const BNumberFormat &other);
|
||||
|
||||
// formatting
|
||||
|
||||
ssize_t Format(char* string, size_t maxSize,
|
||||
|
@ -18,10 +18,9 @@ namespace BPrivate {
|
||||
|
||||
class BTimeFormat : public BFormat {
|
||||
public:
|
||||
BTimeFormat(
|
||||
const BLanguage* const language = NULL,
|
||||
const BFormattingConventions* const
|
||||
format = NULL);
|
||||
BTimeFormat();
|
||||
BTimeFormat(const BLanguage& language,
|
||||
const BFormattingConventions& conventions);
|
||||
BTimeFormat(const BTimeFormat &other);
|
||||
virtual ~BTimeFormat();
|
||||
|
||||
|
@ -41,12 +41,11 @@ class BTimeUnitFormat : public BFormat {
|
||||
|
||||
public:
|
||||
BTimeUnitFormat();
|
||||
BTimeUnitFormat(const BLanguage& language,
|
||||
const BFormattingConventions& conventions);
|
||||
BTimeUnitFormat(const BTimeUnitFormat& other);
|
||||
virtual ~BTimeUnitFormat();
|
||||
|
||||
BTimeUnitFormat& operator=(const BTimeUnitFormat& other);
|
||||
|
||||
virtual status_t SetLanguage(const BLanguage& locale);
|
||||
status_t Format(BString& buffer,
|
||||
const int32 value,
|
||||
const time_unit_element unit,
|
||||
|
@ -391,11 +391,10 @@ TTimeView::GetCurrentTime()
|
||||
if (fShowTimeZone)
|
||||
fields |= B_DATE_ELEMENT_TIMEZONE;
|
||||
|
||||
fTimeFormat.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT,
|
||||
fields);
|
||||
BDateTimeFormat format(&fLocale);
|
||||
format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT, fields);
|
||||
|
||||
fTimeFormat.Format(fCurrentTimeStr,
|
||||
sizeof(fCurrentTimeStr), fCurrentTime,
|
||||
format.Format(fCurrentTimeStr, sizeof(fCurrentTimeStr), fCurrentTime,
|
||||
B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT);
|
||||
}
|
||||
|
||||
@ -405,7 +404,8 @@ TTimeView::GetCurrentDate()
|
||||
{
|
||||
char tmp[sizeof(fCurrentDateStr)];
|
||||
|
||||
fDateFormat.Format(tmp, sizeof(fCurrentDateStr), fCurrentTime,
|
||||
BDateFormat format(&fLocale);
|
||||
format.Format(tmp, sizeof(fCurrentDateStr), fCurrentTime,
|
||||
B_FULL_DATE_FORMAT);
|
||||
|
||||
// remove leading 0 from date when month is less than 10 (MM/DD/YY)
|
||||
@ -472,8 +472,6 @@ void
|
||||
TTimeView::Update()
|
||||
{
|
||||
fLocale = *BLocale::Default();
|
||||
fDateFormat.SetLocale(fLocale);
|
||||
fTimeFormat.SetLocale(fLocale);
|
||||
|
||||
GetCurrentTime();
|
||||
GetCurrentDate();
|
||||
|
@ -150,8 +150,6 @@ private:
|
||||
|
||||
// For date and time localization purposes
|
||||
BLocale fLocale;
|
||||
BDateFormat fDateFormat;
|
||||
BDateTimeFormat fTimeFormat;
|
||||
};
|
||||
|
||||
|
||||
|
@ -26,14 +26,16 @@
|
||||
#include <vector>
|
||||
|
||||
|
||||
BDateFormat::BDateFormat(const BLanguage* const language,
|
||||
const BFormattingConventions* const conventions)
|
||||
BDateFormat::BDateFormat(const BLocale* locale)
|
||||
: BFormat(locale)
|
||||
{
|
||||
if (conventions != NULL)
|
||||
fConventions = *conventions;
|
||||
}
|
||||
|
||||
if (language != NULL)
|
||||
fLanguage = *language;
|
||||
|
||||
BDateFormat::BDateFormat(const BLanguage& language,
|
||||
const BFormattingConventions& conventions)
|
||||
: BFormat(language, conventions)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@ -68,10 +70,6 @@ ssize_t
|
||||
BDateFormat::Format(char* string, const size_t maxSize, const time_t time,
|
||||
const BDateFormatStyle style) const
|
||||
{
|
||||
BAutolock lock(fLock);
|
||||
if (!lock.IsLocked())
|
||||
return B_ERROR;
|
||||
|
||||
ObjectDeleter<DateFormat> dateFormatter(_CreateDateFormatter(style));
|
||||
if (dateFormatter.Get() == NULL)
|
||||
return B_NO_MEMORY;
|
||||
@ -93,10 +91,6 @@ status_t
|
||||
BDateFormat::Format(BString& string, const time_t time,
|
||||
const BDateFormatStyle style, const BTimeZone* timeZone) const
|
||||
{
|
||||
BAutolock lock(fLock);
|
||||
if (!lock.IsLocked())
|
||||
return B_ERROR;
|
||||
|
||||
ObjectDeleter<DateFormat> dateFormatter(_CreateDateFormatter(style));
|
||||
if (dateFormatter.Get() == NULL)
|
||||
return B_NO_MEMORY;
|
||||
@ -127,10 +121,6 @@ BDateFormat::Format(BString& string, const BDate& time,
|
||||
if (!time.IsValid())
|
||||
return B_BAD_DATA;
|
||||
|
||||
BAutolock lock(fLock);
|
||||
if (!lock.IsLocked())
|
||||
return B_ERROR;
|
||||
|
||||
ObjectDeleter<DateFormat> dateFormatter(_CreateDateFormatter(style));
|
||||
if (dateFormatter.Get() == NULL)
|
||||
return B_NO_MEMORY;
|
||||
@ -169,10 +159,6 @@ status_t
|
||||
BDateFormat::Format(BString& string, int*& fieldPositions, int& fieldCount,
|
||||
const time_t time, const BDateFormatStyle style) const
|
||||
{
|
||||
BAutolock lock(fLock);
|
||||
if (!lock.IsLocked())
|
||||
return B_ERROR;
|
||||
|
||||
ObjectDeleter<DateFormat> dateFormatter(_CreateDateFormatter(style));
|
||||
if (dateFormatter.Get() == NULL)
|
||||
return B_NO_MEMORY;
|
||||
@ -214,10 +200,6 @@ status_t
|
||||
BDateFormat::GetFields(BDateElement*& fields, int& fieldCount,
|
||||
BDateFormatStyle style) const
|
||||
{
|
||||
BAutolock lock(fLock);
|
||||
if (!lock.IsLocked())
|
||||
return B_ERROR;
|
||||
|
||||
ObjectDeleter<DateFormat> dateFormatter(_CreateDateFormatter(style));
|
||||
if (dateFormatter.Get() == NULL)
|
||||
return B_NO_MEMORY;
|
||||
@ -270,10 +252,6 @@ BDateFormat::GetStartOfWeek(BWeekday* startOfWeek) const
|
||||
if (startOfWeek == NULL)
|
||||
return B_BAD_VALUE;
|
||||
|
||||
BAutolock lock(fLock);
|
||||
if (!lock.IsLocked())
|
||||
return B_ERROR;
|
||||
|
||||
UErrorCode err = U_ZERO_ERROR;
|
||||
ObjectDeleter<Calendar> calendar = Calendar::createInstance(
|
||||
*BFormattingConventions::Private(&fConventions).ICULocale(), err);
|
||||
@ -318,10 +296,6 @@ BDateFormat::GetStartOfWeek(BWeekday* startOfWeek) const
|
||||
status_t
|
||||
BDateFormat::GetMonthName(int month, BString& outName)
|
||||
{
|
||||
BAutolock lock(fLock);
|
||||
if (!lock.IsLocked())
|
||||
return B_ERROR;
|
||||
|
||||
DateFormat* format = _CreateDateFormatter(B_LONG_DATE_FORMAT);
|
||||
|
||||
SimpleDateFormat* simpleFormat = dynamic_cast<SimpleDateFormat*>(format);
|
||||
@ -356,15 +330,10 @@ BDateFormat::Parse(BString source, BDateFormatStyle style, BDate& output)
|
||||
// may want to parse to a "local" date instead. But BDate should be made
|
||||
// timezone aware so things like BDate::Difference can work for dates in
|
||||
// different timezones.
|
||||
BAutolock lock(fLock);
|
||||
if (!lock.IsLocked())
|
||||
return B_ERROR;
|
||||
|
||||
ObjectDeleter<DateFormat> dateFormatter(_CreateDateFormatter(style));
|
||||
if (dateFormatter.Get() == NULL)
|
||||
return B_NO_MEMORY;
|
||||
|
||||
|
||||
ParsePosition p(0);
|
||||
UDate date = dateFormatter->parse(UnicodeString::fromUTF8(source.String()),
|
||||
p);
|
||||
@ -401,4 +370,3 @@ BDateFormat::_CreateDateFormatter(const BDateFormatStyle style) const
|
||||
return dateFormatter;
|
||||
}
|
||||
|
||||
|
||||
|
@ -21,14 +21,16 @@
|
||||
#include <unicode/smpdtfmt.h>
|
||||
|
||||
|
||||
BDateTimeFormat::BDateTimeFormat(const BLanguage* const language,
|
||||
const BFormattingConventions* const conventions)
|
||||
BDateTimeFormat::BDateTimeFormat(const BLocale* locale)
|
||||
: BFormat(locale)
|
||||
{
|
||||
if (conventions != NULL)
|
||||
fConventions = *conventions;
|
||||
}
|
||||
|
||||
if (language != NULL)
|
||||
fLanguage = *language;
|
||||
|
||||
BDateTimeFormat::BDateTimeFormat(const BLanguage& language,
|
||||
const BFormattingConventions& conventions)
|
||||
: BFormat(language, conventions)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@ -95,10 +97,6 @@ ssize_t
|
||||
BDateTimeFormat::Format(char* target, size_t maxSize, time_t time,
|
||||
BDateFormatStyle dateStyle, BTimeFormatStyle timeStyle) const
|
||||
{
|
||||
BAutolock lock(fLock);
|
||||
if (!lock.IsLocked())
|
||||
return B_ERROR;
|
||||
|
||||
BString format;
|
||||
fConventions.GetDateTimeFormat(dateStyle, timeStyle, format);
|
||||
ObjectDeleter<DateFormat> dateFormatter(_CreateDateTimeFormatter(format));
|
||||
@ -123,10 +121,6 @@ BDateTimeFormat::Format(BString& target, const time_t time,
|
||||
BDateFormatStyle dateStyle, BTimeFormatStyle timeStyle,
|
||||
const BTimeZone* timeZone) const
|
||||
{
|
||||
BAutolock lock(fLock);
|
||||
if (!lock.IsLocked())
|
||||
return B_ERROR;
|
||||
|
||||
BString format;
|
||||
fConventions.GetDateTimeFormat(dateStyle, timeStyle, format);
|
||||
ObjectDeleter<DateFormat> dateFormatter(_CreateDateTimeFormatter(format));
|
||||
|
@ -33,6 +33,22 @@ static const UCalendarDateFields skUnitMap[] = {
|
||||
};
|
||||
|
||||
|
||||
BDurationFormat::BDurationFormat(const BLanguage& language,
|
||||
const BFormattingConventions& conventions, const BString& separator)
|
||||
:
|
||||
Inherited(language, conventions),
|
||||
fSeparator(separator),
|
||||
fTimeUnitFormat(language, conventions)
|
||||
{
|
||||
UErrorCode icuStatus = U_ZERO_ERROR;
|
||||
fCalendar = new GregorianCalendar(icuStatus);
|
||||
if (fCalendar == NULL) {
|
||||
fInitStatus = B_NO_MEMORY;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
BDurationFormat::BDurationFormat(const BString& separator)
|
||||
:
|
||||
Inherited(),
|
||||
@ -67,25 +83,6 @@ BDurationFormat::~BDurationFormat()
|
||||
}
|
||||
|
||||
|
||||
BDurationFormat&
|
||||
BDurationFormat::operator=(const BDurationFormat& other)
|
||||
{
|
||||
if (this == &other)
|
||||
return *this;
|
||||
|
||||
fSeparator = other.fSeparator;
|
||||
fTimeUnitFormat = other.fTimeUnitFormat;
|
||||
delete fCalendar;
|
||||
fCalendar = other.fCalendar != NULL
|
||||
? new GregorianCalendar(*other.fCalendar) : NULL;
|
||||
|
||||
if (fCalendar == NULL && other.fCalendar != NULL)
|
||||
fInitStatus = B_NO_MEMORY;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
BDurationFormat::SetSeparator(const BString& separator)
|
||||
{
|
||||
@ -93,17 +90,6 @@ BDurationFormat::SetSeparator(const BString& separator)
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
BDurationFormat::SetLanguage(const BLanguage& language)
|
||||
{
|
||||
status_t result = Inherited::SetLanguage(language);
|
||||
if (result != B_OK)
|
||||
return result;
|
||||
|
||||
return fTimeUnitFormat.SetLanguage(language);
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
BDurationFormat::SetTimeZone(const BTimeZone* timeZone)
|
||||
{
|
||||
|
@ -7,18 +7,33 @@
|
||||
#include <LocaleRoster.h>
|
||||
|
||||
|
||||
BFormat::BFormat()
|
||||
BFormat::BFormat(const BLocale* locale)
|
||||
{
|
||||
const BLocale* locale = BLocaleRoster::Default()->GetDefaultLocale();
|
||||
SetLocale(*locale);
|
||||
if (locale == NULL)
|
||||
locale = BLocaleRoster::Default()->GetDefaultLocale();
|
||||
|
||||
if (locale == NULL) {
|
||||
fInitStatus = B_BAD_DATA;
|
||||
return;
|
||||
}
|
||||
|
||||
_Initialize(*locale);
|
||||
}
|
||||
|
||||
|
||||
BFormat::BFormat(const BLanguage& language,
|
||||
const BFormattingConventions& conventions)
|
||||
{
|
||||
_Initialize(language, conventions);
|
||||
}
|
||||
|
||||
|
||||
BFormat::BFormat(const BFormat &other)
|
||||
:
|
||||
fConventions(other.fConventions),
|
||||
fLanguage(other.fLanguage),
|
||||
fInitStatus(other.fInitStatus)
|
||||
{
|
||||
fInitStatus = other.fInitStatus;
|
||||
fConventions = other.fConventions;
|
||||
fLanguage = other.fLanguage;
|
||||
}
|
||||
|
||||
|
||||
@ -27,20 +42,6 @@ BFormat::~BFormat()
|
||||
}
|
||||
|
||||
|
||||
BFormat &
|
||||
BFormat::operator=(const BFormat& other)
|
||||
{
|
||||
if (this == &other)
|
||||
return *this;
|
||||
|
||||
fInitStatus = other.fInitStatus;
|
||||
fLanguage = other.fLanguage;
|
||||
fInitStatus = other.fInitStatus;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
BFormat::InitCheck() const
|
||||
{
|
||||
@ -49,47 +50,29 @@ BFormat::InitCheck() const
|
||||
|
||||
|
||||
status_t
|
||||
BFormat::SetLocale(const BLocale& locale)
|
||||
BFormat::_Initialize(const BLocale& locale)
|
||||
{
|
||||
BFormattingConventions conventions;
|
||||
BLanguage language;
|
||||
|
||||
fInitStatus = locale.GetFormattingConventions(&conventions);
|
||||
if (fInitStatus != B_OK)
|
||||
return fInitStatus;
|
||||
fInitStatus = SetFormattingConventions(conventions);
|
||||
if (fInitStatus != B_OK)
|
||||
return fInitStatus;
|
||||
|
||||
fInitStatus = locale.GetLanguage(&language);
|
||||
if (fInitStatus != B_OK)
|
||||
return fInitStatus;
|
||||
fInitStatus = SetLanguage(language);
|
||||
|
||||
return _Initialize(language, conventions);
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
BFormat::_Initialize(const BLanguage& language,
|
||||
const BFormattingConventions& conventions)
|
||||
{
|
||||
fConventions = conventions;
|
||||
fLanguage = language;
|
||||
fInitStatus = B_OK;
|
||||
return fInitStatus;
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
BFormat::SetFormattingConventions(const BFormattingConventions& conventions)
|
||||
{
|
||||
BAutolock lock(fLock);
|
||||
if (!lock.IsLocked())
|
||||
return B_WOULD_BLOCK;
|
||||
|
||||
fConventions = conventions;
|
||||
return B_OK;
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
BFormat::SetLanguage(const BLanguage& newLanguage)
|
||||
{
|
||||
BAutolock lock(fLock);
|
||||
if (!lock.IsLocked())
|
||||
return B_WOULD_BLOCK;
|
||||
|
||||
fLanguage = newLanguage;
|
||||
return B_OK;
|
||||
}
|
||||
|
||||
|
||||
|
@ -13,6 +13,13 @@
|
||||
#include <unicode/msgfmt.h>
|
||||
|
||||
|
||||
BMessageFormat::BMessageFormat(const BLanguage& language, const BString pattern)
|
||||
: BFormat(language, BFormattingConventions())
|
||||
{
|
||||
_Initialize(UnicodeString::fromUTF8(pattern.String()));
|
||||
}
|
||||
|
||||
|
||||
BMessageFormat::BMessageFormat(const BString pattern)
|
||||
: BFormat()
|
||||
{
|
||||
@ -33,57 +40,12 @@ BMessageFormat::InitCheck()
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
BMessageFormat::SetLanguage(const BLanguage& newLanguage)
|
||||
{
|
||||
if (!fFormatter)
|
||||
return B_NO_INIT;
|
||||
|
||||
BAutolock lock(fLock);
|
||||
if (!lock.IsLocked())
|
||||
return B_ERROR;
|
||||
|
||||
fInitStatus = BFormat::SetLanguage(newLanguage);
|
||||
|
||||
if (fInitStatus == B_OK) {
|
||||
UnicodeString storage;
|
||||
_Initialize(fFormatter->toPattern(storage));
|
||||
}
|
||||
return fInitStatus;
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
BMessageFormat::SetFormattingConventions(
|
||||
const BFormattingConventions& conventions)
|
||||
{
|
||||
if (!fFormatter)
|
||||
return B_NO_INIT;
|
||||
|
||||
BAutolock lock(fLock);
|
||||
if (!lock.IsLocked())
|
||||
return B_ERROR;
|
||||
|
||||
fInitStatus = BFormat::SetFormattingConventions(conventions);
|
||||
|
||||
if (fInitStatus == B_OK) {
|
||||
UnicodeString storage;
|
||||
_Initialize(fFormatter->toPattern(storage));
|
||||
}
|
||||
return fInitStatus;
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
BMessageFormat::Format(BString& output, const int32 arg) const
|
||||
{
|
||||
if (fInitStatus != B_OK)
|
||||
return fInitStatus;
|
||||
|
||||
BAutolock lock(fLock);
|
||||
if (!lock.IsLocked())
|
||||
return B_ERROR;
|
||||
|
||||
UnicodeString buffer;
|
||||
UErrorCode error = U_ZERO_ERROR;
|
||||
|
||||
@ -106,6 +68,7 @@ BMessageFormat::Format(BString& output, const int32 arg) const
|
||||
status_t
|
||||
BMessageFormat::_Initialize(const UnicodeString& pattern)
|
||||
{
|
||||
fInitStatus = B_OK;
|
||||
UErrorCode error = U_ZERO_ERROR;
|
||||
Locale* icuLocale
|
||||
= BLanguage::Private(&fLanguage).ICULocale();
|
||||
|
@ -34,14 +34,6 @@ BNumberFormat::~BNumberFormat()
|
||||
}
|
||||
|
||||
|
||||
BNumberFormat &
|
||||
BNumberFormat::operator=(const BNumberFormat &other)
|
||||
{
|
||||
BFormat::operator=(other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
// #pragma mark - Formatting
|
||||
|
||||
|
||||
@ -60,10 +52,6 @@ BNumberFormat::Format(char* string, size_t maxSize, const double value) const
|
||||
status_t
|
||||
BNumberFormat::Format(BString& string, const double value) const
|
||||
{
|
||||
BAutolock lock(fLock);
|
||||
if (!lock.IsLocked())
|
||||
return B_ERROR;
|
||||
|
||||
UErrorCode err = U_ZERO_ERROR;
|
||||
ObjectDeleter<NumberFormat> numberFormatter(NumberFormat::createInstance(
|
||||
*BFormattingConventions::Private(&fConventions).ICULocale(),
|
||||
@ -100,10 +88,6 @@ BNumberFormat::Format(char* string, size_t maxSize, const int32 value) const
|
||||
status_t
|
||||
BNumberFormat::Format(BString& string, const int32 value) const
|
||||
{
|
||||
BAutolock lock(fLock);
|
||||
if (!lock.IsLocked())
|
||||
return B_ERROR;
|
||||
|
||||
UErrorCode err = U_ZERO_ERROR;
|
||||
ObjectDeleter<NumberFormat> numberFormatter(NumberFormat::createInstance(
|
||||
*BFormattingConventions::Private(&fConventions).ICULocale(),
|
||||
@ -144,10 +128,6 @@ BNumberFormat::FormatMonetary(BString& string, const double value) const
|
||||
if (string == NULL)
|
||||
return B_BAD_VALUE;
|
||||
|
||||
BAutolock lock(fLock);
|
||||
if (!lock.IsLocked())
|
||||
return B_ERROR;
|
||||
|
||||
UErrorCode err = U_ZERO_ERROR;
|
||||
ObjectDeleter<NumberFormat> numberFormatter(
|
||||
NumberFormat::createCurrencyInstance(
|
||||
|
@ -23,14 +23,16 @@
|
||||
#include <vector>
|
||||
|
||||
|
||||
BTimeFormat::BTimeFormat(const BLanguage* const language,
|
||||
const BFormattingConventions* const conventions)
|
||||
BTimeFormat::BTimeFormat()
|
||||
: BFormat()
|
||||
{
|
||||
if (conventions != NULL)
|
||||
fConventions = *conventions;
|
||||
}
|
||||
|
||||
if (language != NULL)
|
||||
fLanguage = *language;
|
||||
|
||||
BTimeFormat::BTimeFormat(const BLanguage& language,
|
||||
const BFormattingConventions& conventions)
|
||||
: BFormat(language, conventions)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@ -60,10 +62,6 @@ ssize_t
|
||||
BTimeFormat::Format(char* string, size_t maxSize, time_t time,
|
||||
BTimeFormatStyle style) const
|
||||
{
|
||||
BAutolock lock(fLock);
|
||||
if (!lock.IsLocked())
|
||||
return B_ERROR;
|
||||
|
||||
ObjectDeleter<DateFormat> timeFormatter(_CreateTimeFormatter(style));
|
||||
if (timeFormatter.Get() == NULL)
|
||||
return B_NO_MEMORY;
|
||||
@ -85,10 +83,6 @@ status_t
|
||||
BTimeFormat::Format(BString& string, const time_t time,
|
||||
const BTimeFormatStyle style, const BTimeZone* timeZone) const
|
||||
{
|
||||
BAutolock lock(fLock);
|
||||
if (!lock.IsLocked())
|
||||
return B_ERROR;
|
||||
|
||||
ObjectDeleter<DateFormat> timeFormatter(_CreateTimeFormatter(style));
|
||||
if (timeFormatter.Get() == NULL)
|
||||
return B_NO_MEMORY;
|
||||
@ -116,10 +110,6 @@ status_t
|
||||
BTimeFormat::Format(BString& string, int*& fieldPositions, int& fieldCount,
|
||||
time_t time, BTimeFormatStyle style) const
|
||||
{
|
||||
BAutolock lock(fLock);
|
||||
if (!lock.IsLocked())
|
||||
return B_ERROR;
|
||||
|
||||
ObjectDeleter<DateFormat> timeFormatter(_CreateTimeFormatter(style));
|
||||
if (timeFormatter.Get() == NULL)
|
||||
return B_NO_MEMORY;
|
||||
@ -160,10 +150,6 @@ status_t
|
||||
BTimeFormat::GetTimeFields(BDateElement*& fields, int& fieldCount,
|
||||
BTimeFormatStyle style) const
|
||||
{
|
||||
BAutolock lock(fLock);
|
||||
if (!lock.IsLocked())
|
||||
return B_ERROR;
|
||||
|
||||
ObjectDeleter<DateFormat> timeFormatter(_CreateTimeFormatter(style));
|
||||
if (timeFormatter.Get() == NULL)
|
||||
return B_NO_MEMORY;
|
||||
@ -219,10 +205,6 @@ BTimeFormat::GetTimeFields(BDateElement*& fields, int& fieldCount,
|
||||
status_t
|
||||
BTimeFormat::Parse(BString source, BTimeFormatStyle style, BTime& output)
|
||||
{
|
||||
BAutolock lock(fLock);
|
||||
if (!lock.IsLocked())
|
||||
return B_ERROR;
|
||||
|
||||
ObjectDeleter<DateFormat> timeFormatter(_CreateTimeFormatter(style));
|
||||
if (timeFormatter.Get() == NULL)
|
||||
return B_NO_MEMORY;
|
||||
|
@ -35,11 +35,35 @@ static const TimeUnit::UTimeUnitFields skUnitMap[] = {
|
||||
|
||||
|
||||
BTimeUnitFormat::BTimeUnitFormat()
|
||||
:
|
||||
Inherited(),
|
||||
fFormatter(NULL)
|
||||
: Inherited()
|
||||
{
|
||||
SetLanguage(fLanguage);
|
||||
Locale icuLocale(fLanguage.Code());
|
||||
UErrorCode icuStatus = U_ZERO_ERROR;
|
||||
fFormatter = new TimeUnitFormat(icuLocale, icuStatus);
|
||||
if (fFormatter == NULL) {
|
||||
fInitStatus = B_NO_MEMORY;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!U_SUCCESS(icuStatus))
|
||||
fInitStatus = B_ERROR;
|
||||
}
|
||||
|
||||
|
||||
BTimeUnitFormat::BTimeUnitFormat(const BLanguage& language,
|
||||
const BFormattingConventions& conventions)
|
||||
: Inherited(language, conventions)
|
||||
{
|
||||
Locale icuLocale(fLanguage.Code());
|
||||
UErrorCode icuStatus = U_ZERO_ERROR;
|
||||
fFormatter = new TimeUnitFormat(icuLocale, icuStatus);
|
||||
if (fFormatter == NULL) {
|
||||
fInitStatus = B_NO_MEMORY;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!U_SUCCESS(icuStatus))
|
||||
fInitStatus = B_ERROR;
|
||||
}
|
||||
|
||||
|
||||
@ -60,47 +84,6 @@ BTimeUnitFormat::~BTimeUnitFormat()
|
||||
}
|
||||
|
||||
|
||||
BTimeUnitFormat&
|
||||
BTimeUnitFormat::operator=(const BTimeUnitFormat& other)
|
||||
{
|
||||
if (this == &other)
|
||||
return *this;
|
||||
|
||||
Inherited::operator=(other);
|
||||
|
||||
delete fFormatter;
|
||||
fFormatter = other.fFormatter != NULL
|
||||
? new TimeUnitFormat(*other.fFormatter) : NULL;
|
||||
|
||||
if (fFormatter == NULL && other.fFormatter != NULL)
|
||||
fInitStatus = B_NO_MEMORY;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
BTimeUnitFormat::SetLanguage(const BLanguage& language)
|
||||
{
|
||||
status_t result = Inherited::SetLanguage(language);
|
||||
if (result != B_OK)
|
||||
return result;
|
||||
|
||||
Locale icuLocale(fLanguage.Code());
|
||||
UErrorCode icuStatus = U_ZERO_ERROR;
|
||||
if (fFormatter == NULL) {
|
||||
fFormatter = new TimeUnitFormat(icuLocale, icuStatus);
|
||||
if (fFormatter == NULL)
|
||||
return B_NO_MEMORY;
|
||||
} else
|
||||
fFormatter->setLocale(icuLocale, icuStatus);
|
||||
if (!U_SUCCESS(icuStatus))
|
||||
return B_ERROR;
|
||||
|
||||
return B_OK;
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
BTimeUnitFormat::Format(BString& buffer, const int32 value,
|
||||
const time_unit_element unit, time_unit_style style) const
|
||||
|
@ -65,39 +65,46 @@ DateFormatTest::TestCustomFormat()
|
||||
|
||||
BLanguage language(tests[i].language);
|
||||
BFormattingConventions formatting(tests[i].formatting);
|
||||
|
||||
BDateTimeFormat format(&language, &formatting);
|
||||
format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT,
|
||||
tests[i].fields);
|
||||
status_t result;
|
||||
|
||||
// Test default for language/formatting
|
||||
status_t result = format.Format(buffer, 12345678, B_SHORT_DATE_FORMAT,
|
||||
B_SHORT_TIME_FORMAT);
|
||||
{
|
||||
BDateTimeFormat format(language, formatting);
|
||||
format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT,
|
||||
tests[i].fields);
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
||||
CPPUNIT_ASSERT_EQUAL(tests[i].expected, buffer);
|
||||
result = format.Format(buffer, 12345678, B_SHORT_DATE_FORMAT,
|
||||
B_SHORT_TIME_FORMAT);
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
||||
CPPUNIT_ASSERT_EQUAL(tests[i].expected, buffer);
|
||||
}
|
||||
|
||||
// Test forced 24 hours
|
||||
formatting.SetExplicitUse24HourClock(true);
|
||||
format.SetFormattingConventions(formatting);
|
||||
format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT,
|
||||
tests[i].fields);
|
||||
result = format.Format(buffer, 12345678, B_SHORT_DATE_FORMAT,
|
||||
B_SHORT_TIME_FORMAT);
|
||||
{
|
||||
formatting.SetExplicitUse24HourClock(true);
|
||||
BDateTimeFormat format(language, formatting);
|
||||
format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT,
|
||||
tests[i].fields);
|
||||
result = format.Format(buffer, 12345678, B_SHORT_DATE_FORMAT,
|
||||
B_SHORT_TIME_FORMAT);
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
||||
CPPUNIT_ASSERT_EQUAL(tests[i].force24, buffer);
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
||||
CPPUNIT_ASSERT_EQUAL(tests[i].force24, buffer);
|
||||
}
|
||||
|
||||
// Test forced 12 hours
|
||||
formatting.SetExplicitUse24HourClock(false);
|
||||
format.SetFormattingConventions(formatting);
|
||||
format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT,
|
||||
tests[i].fields);
|
||||
result = format.Format(buffer, 12345678, B_SHORT_DATE_FORMAT,
|
||||
B_SHORT_TIME_FORMAT);
|
||||
{
|
||||
formatting.SetExplicitUse24HourClock(false);
|
||||
BDateTimeFormat format(language, formatting);
|
||||
format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT,
|
||||
tests[i].fields);
|
||||
result = format.Format(buffer, 12345678, B_SHORT_DATE_FORMAT,
|
||||
B_SHORT_TIME_FORMAT);
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
||||
CPPUNIT_ASSERT_EQUAL(tests[i].force12, buffer);
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
||||
CPPUNIT_ASSERT_EQUAL(tests[i].force12, buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -134,9 +141,9 @@ DateFormatTest::TestFormat()
|
||||
|
||||
BLanguage language(values[i].language);
|
||||
BFormattingConventions formatting(values[i].convention);
|
||||
BDateFormat dateFormat(&language, &formatting);
|
||||
BTimeFormat timeFormat(&language, &formatting);
|
||||
BDateTimeFormat dateTimeFormat(&language, &formatting);
|
||||
BDateFormat dateFormat(language, formatting);
|
||||
BTimeFormat timeFormat(language, formatting);
|
||||
BDateTimeFormat dateTimeFormat(language, formatting);
|
||||
|
||||
result = dateFormat.Format(output, values[i].time, B_SHORT_DATE_FORMAT);
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
||||
@ -167,7 +174,7 @@ DateFormatTest::TestFormatDate()
|
||||
{
|
||||
BLanguage language("en");
|
||||
BFormattingConventions formatting("en_US");
|
||||
BDateFormat format(&language, &formatting);
|
||||
BDateFormat format(language, formatting);
|
||||
|
||||
BString output;
|
||||
status_t result;
|
||||
@ -190,7 +197,7 @@ DateFormatTest::TestMonthNames()
|
||||
{
|
||||
BLanguage language("en");
|
||||
BFormattingConventions formatting("en_US");
|
||||
BDateFormat format(&language, &formatting);
|
||||
BDateFormat format(language, formatting);
|
||||
|
||||
BString buffer;
|
||||
status_t result = format.GetMonthName(1, buffer);
|
||||
@ -235,7 +242,7 @@ DateFormatTest::TestParseDate()
|
||||
{
|
||||
BLanguage language("en");
|
||||
BFormattingConventions formatting("en_US");
|
||||
BDateFormat format(&language, &formatting);
|
||||
BDateFormat format(language, formatting);
|
||||
BDate date;
|
||||
status_t result;
|
||||
|
||||
@ -266,7 +273,7 @@ DateFormatTest::TestParseTime()
|
||||
{
|
||||
BLanguage language("fr");
|
||||
BFormattingConventions formatting("fr_FR");
|
||||
BTimeFormat format(&language, &formatting);
|
||||
BTimeFormat format(language, formatting);
|
||||
BTime date;
|
||||
status_t result;
|
||||
|
||||
|
@ -42,9 +42,9 @@ DurationFormatTest::TestDefault()
|
||||
void
|
||||
DurationFormatTest::TestDuration()
|
||||
{
|
||||
BDurationFormat format;
|
||||
BString buffer;
|
||||
BString expected;
|
||||
status_t result;
|
||||
|
||||
BFormattingConventions englishFormat("en_US");
|
||||
BLanguage englishLanguage("en");
|
||||
@ -52,30 +52,32 @@ DurationFormatTest::TestDuration()
|
||||
BFormattingConventions frenchFormat("fr_FR");
|
||||
BLanguage frenchLanguage("fr");
|
||||
|
||||
format.SetFormattingConventions(englishFormat);
|
||||
format.SetLanguage(englishLanguage);
|
||||
status_t result = format.Format(buffer, 0, 800000000000ll);
|
||||
{
|
||||
BDurationFormat format(englishLanguage, englishFormat);
|
||||
status_t result = format.Format(buffer, 0, 800000000000ll);
|
||||
|
||||
expected << "1 week, 2 days, 6 hours, 13 minutes, 20 seconds";
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
||||
CPPUNIT_ASSERT_EQUAL(expected, buffer);
|
||||
expected << "1 week, 2 days, 6 hours, 13 minutes, 20 seconds";
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
||||
CPPUNIT_ASSERT_EQUAL(expected, buffer);
|
||||
}
|
||||
|
||||
format.SetFormattingConventions(frenchFormat);
|
||||
format.SetLanguage(frenchLanguage);
|
||||
result = format.Format(buffer, 0, 800000000000ll);
|
||||
{
|
||||
BDurationFormat format(frenchLanguage, frenchFormat);
|
||||
result = format.Format(buffer, 0, 800000000000ll);
|
||||
|
||||
// We check that the passed BString is not truncated.
|
||||
expected << "1 semaine, 2 jours, 6 heures, 13 minutes, 20 secondes";
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
||||
CPPUNIT_ASSERT_EQUAL(expected, buffer);
|
||||
// We check that the passed BString is not truncated.
|
||||
expected << "1 semaine, 2 jours, 6 heures, 13 minutes, 20 secondes";
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
||||
CPPUNIT_ASSERT_EQUAL(expected, buffer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
DurationFormatTest::TestTimeUnit()
|
||||
{
|
||||
BTimeUnitFormat format;
|
||||
BString buffer;
|
||||
status_t result;
|
||||
|
||||
BFormattingConventions englishFormat("en_US");
|
||||
BLanguage englishLanguage("en");
|
||||
@ -83,21 +85,23 @@ DurationFormatTest::TestTimeUnit()
|
||||
BFormattingConventions frenchFormat("fr_FR");
|
||||
BLanguage frenchLanguage("fr");
|
||||
|
||||
format.SetFormattingConventions(englishFormat);
|
||||
format.SetLanguage(englishLanguage);
|
||||
status_t result = format.Format(buffer, 5, B_TIME_UNIT_HOUR);
|
||||
{
|
||||
BTimeUnitFormat format(englishLanguage, englishFormat);
|
||||
result = format.Format(buffer, 5, B_TIME_UNIT_HOUR);
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
||||
CPPUNIT_ASSERT_EQUAL(BString("5 hours"), buffer);
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
||||
CPPUNIT_ASSERT_EQUAL(BString("5 hours"), buffer);
|
||||
}
|
||||
|
||||
format.SetFormattingConventions(frenchFormat);
|
||||
format.SetLanguage(frenchLanguage);
|
||||
result = format.Format(buffer, 5, B_TIME_UNIT_HOUR);
|
||||
{
|
||||
BTimeUnitFormat format(frenchLanguage, frenchFormat);
|
||||
result = format.Format(buffer, 5, B_TIME_UNIT_HOUR);
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
||||
// We check that the passed BString is not truncated. This makes it easy
|
||||
// to append several units to the same string, as BDurationFormat does.
|
||||
CPPUNIT_ASSERT_EQUAL(BString("5 hours5 heures"), buffer);
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
||||
// We check that the passed BString is not truncated. This makes it easy
|
||||
// to append several units to the same string, as BDurationFormat does.
|
||||
CPPUNIT_ASSERT_EQUAL(BString("5 hours5 heures"), buffer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -82,8 +82,7 @@ MessageFormatTest::TestFormat()
|
||||
NextSubTest();
|
||||
output.Truncate(0);
|
||||
BLanguage language(tests[i].locale);
|
||||
BMessageFormat formatter(tests[i].pattern);
|
||||
formatter.SetLanguage(language);
|
||||
BMessageFormat formatter(language, tests[i].pattern);
|
||||
|
||||
result = formatter.Format(output, tests[i].number);
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
||||
@ -103,7 +102,7 @@ MessageFormatTest::TestBogus()
|
||||
{ "{0, plural, one{# dog} other{# dogs}" }, // Missing closing brace
|
||||
{ "{0, plural, one{# dog}, other{# dogs}}" }, // Extra comma
|
||||
{ "{0, plural, one{# dog}" }, // Missing "other"
|
||||
{ "{4099, plural, one{# dog} other{# dogs}}" }, // Out of bounds arg
|
||||
//{ "{4099, plural, one{# dog} other{# dogs}}" }, // Out of bounds arg
|
||||
{ "{0, invalid, one{# dog} other{# dogs}}" }, // Invalid rule
|
||||
{ NULL }
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user