* Ensure if ICU formatters allocation fails, we return an empty string
* Use a CheckedArrayByteSink for formatting to a char*, instead of allocating a BString then copying it back to the buffer using strncpy git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@37708 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
+55
-29
@@ -187,6 +187,8 @@ BCountry::GetIcon(BBitmap* result)
|
|||||||
DateFormat*
|
DateFormat*
|
||||||
BCountry::DateFormatter(bool longFormat)
|
BCountry::DateFormatter(bool longFormat)
|
||||||
{
|
{
|
||||||
|
// TODO: ICU allows for 4 different levels of expansion :
|
||||||
|
// short, medium, long, and full. Our bool parameter is not enough...
|
||||||
if (longFormat) {
|
if (longFormat) {
|
||||||
if (fICULongDateFormatter == NULL) {
|
if (fICULongDateFormatter == NULL) {
|
||||||
fICULongDateFormatter = DateFormat::createDateInstance(
|
fICULongDateFormatter = DateFormat::createDateInstance(
|
||||||
@@ -206,6 +208,8 @@ BCountry::DateFormatter(bool longFormat)
|
|||||||
DateFormat*
|
DateFormat*
|
||||||
BCountry::TimeFormatter(bool longFormat)
|
BCountry::TimeFormatter(bool longFormat)
|
||||||
{
|
{
|
||||||
|
// TODO: ICU allows for 4 different levels of expansion :
|
||||||
|
// short, medium, long, and full. Our bool parameter is not enough...
|
||||||
if (longFormat) {
|
if (longFormat) {
|
||||||
if (fICULongTimeFormatter == NULL) {
|
if (fICULongTimeFormatter == NULL) {
|
||||||
fICULongTimeFormatter = DateFormat::createTimeInstance(
|
fICULongTimeFormatter = DateFormat::createTimeInstance(
|
||||||
@@ -225,20 +229,6 @@ BCountry::TimeFormatter(bool longFormat)
|
|||||||
status_t
|
status_t
|
||||||
BCountry::FormatDate(char* string, size_t maxSize, time_t time, bool longFormat)
|
BCountry::FormatDate(char* string, size_t maxSize, time_t time, bool longFormat)
|
||||||
{
|
{
|
||||||
BString fullString;
|
|
||||||
status_t returnCode;
|
|
||||||
returnCode = FormatDate(&fullString, time, longFormat);
|
|
||||||
if (returnCode == B_OK)
|
|
||||||
strncpy(string, fullString.String(), maxSize);
|
|
||||||
return returnCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
status_t
|
|
||||||
BCountry::FormatDate(BString *string, time_t time, bool longFormat)
|
|
||||||
{
|
|
||||||
// TODO: ICU allows for 4 different levels of expansion :
|
|
||||||
// short, medium, long, and full. Our bool parameter is not enough...
|
|
||||||
ICU_VERSION::DateFormat* dateFormatter = DateFormatter(longFormat);
|
ICU_VERSION::DateFormat* dateFormatter = DateFormatter(longFormat);
|
||||||
if (dateFormatter == NULL)
|
if (dateFormatter == NULL)
|
||||||
return B_NO_MEMORY;
|
return B_NO_MEMORY;
|
||||||
@@ -246,7 +236,30 @@ BCountry::FormatDate(BString *string, time_t time, bool longFormat)
|
|||||||
UnicodeString ICUString;
|
UnicodeString ICUString;
|
||||||
ICUString = dateFormatter->format((UDate)time * 1000, ICUString);
|
ICUString = dateFormatter->format((UDate)time * 1000, ICUString);
|
||||||
|
|
||||||
|
CheckedArrayByteSink stringConverter(string, maxSize);
|
||||||
|
|
||||||
|
ICUString.toUTF8(stringConverter);
|
||||||
|
|
||||||
|
if (stringConverter.Overflowed())
|
||||||
|
return B_BAD_VALUE;
|
||||||
|
else
|
||||||
|
return B_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
status_t
|
||||||
|
BCountry::FormatDate(BString *string, time_t time, bool longFormat)
|
||||||
|
{
|
||||||
string->Truncate(0);
|
string->Truncate(0);
|
||||||
|
// We make the string empty, this way even in cases where ICU fail we at
|
||||||
|
// least return something sane
|
||||||
|
ICU_VERSION::DateFormat* dateFormatter = DateFormatter(longFormat);
|
||||||
|
if (dateFormatter == NULL)
|
||||||
|
return B_NO_MEMORY;
|
||||||
|
|
||||||
|
UnicodeString ICUString;
|
||||||
|
ICUString = dateFormatter->format((UDate)time * 1000, ICUString);
|
||||||
|
|
||||||
BStringByteSink stringConverter(string);
|
BStringByteSink stringConverter(string);
|
||||||
|
|
||||||
ICUString.toUTF8(stringConverter);
|
ICUString.toUTF8(stringConverter);
|
||||||
@@ -259,6 +272,8 @@ status_t
|
|||||||
BCountry::FormatDate(BString* string, int*& fieldPositions, int& fieldCount,
|
BCountry::FormatDate(BString* string, int*& fieldPositions, int& fieldCount,
|
||||||
time_t time, bool longFormat)
|
time_t time, bool longFormat)
|
||||||
{
|
{
|
||||||
|
string->Truncate(0);
|
||||||
|
|
||||||
ICU_VERSION::DateFormat* dateFormatter = DateFormatter(longFormat);
|
ICU_VERSION::DateFormat* dateFormatter = DateFormatter(longFormat);
|
||||||
if (dateFormatter == NULL)
|
if (dateFormatter == NULL)
|
||||||
return B_NO_MEMORY;
|
return B_NO_MEMORY;
|
||||||
@@ -287,7 +302,6 @@ BCountry::FormatDate(BString* string, int*& fieldPositions, int& fieldCount,
|
|||||||
for (int i = 0 ; i < fieldCount ; i++ )
|
for (int i = 0 ; i < fieldCount ; i++ )
|
||||||
fieldPositions[i] = fieldPosStorage[i];
|
fieldPositions[i] = fieldPosStorage[i];
|
||||||
|
|
||||||
string->Truncate(0);
|
|
||||||
BStringByteSink stringConverter(string);
|
BStringByteSink stringConverter(string);
|
||||||
|
|
||||||
ICUString.toUTF8(stringConverter);
|
ICUString.toUTF8(stringConverter);
|
||||||
@@ -299,6 +313,8 @@ BCountry::FormatDate(BString* string, int*& fieldPositions, int& fieldCount,
|
|||||||
status_t
|
status_t
|
||||||
BCountry::DateFormat(BString& format, bool longFormat)
|
BCountry::DateFormat(BString& format, bool longFormat)
|
||||||
{
|
{
|
||||||
|
format.Truncate(0);
|
||||||
|
|
||||||
ICU_VERSION::DateFormat* dateFormatter = DateFormatter(longFormat);
|
ICU_VERSION::DateFormat* dateFormatter = DateFormatter(longFormat);
|
||||||
if (dateFormatter == NULL)
|
if (dateFormatter == NULL)
|
||||||
return B_NO_MEMORY;
|
return B_NO_MEMORY;
|
||||||
@@ -406,19 +422,6 @@ BCountry::StartOfWeek()
|
|||||||
status_t
|
status_t
|
||||||
BCountry::FormatTime(char* string, size_t maxSize, time_t time, bool longFormat)
|
BCountry::FormatTime(char* string, size_t maxSize, time_t time, bool longFormat)
|
||||||
{
|
{
|
||||||
BString fullString;
|
|
||||||
status_t returnCode = FormatTime(&fullString, time, longFormat);
|
|
||||||
if (returnCode == B_OK)
|
|
||||||
strncpy(string, fullString.String(), maxSize);
|
|
||||||
return returnCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
status_t
|
|
||||||
BCountry::FormatTime(BString* string, time_t time, bool longFormat)
|
|
||||||
{
|
|
||||||
// TODO: ICU allows for 4 different levels of expansion :
|
|
||||||
// short, medium, long, and full. Our bool parameter is not enough...
|
|
||||||
ICU_VERSION::DateFormat* timeFormatter = TimeFormatter(longFormat);
|
ICU_VERSION::DateFormat* timeFormatter = TimeFormatter(longFormat);
|
||||||
if (timeFormatter == NULL)
|
if (timeFormatter == NULL)
|
||||||
return B_NO_MEMORY;
|
return B_NO_MEMORY;
|
||||||
@@ -426,7 +429,29 @@ BCountry::FormatTime(BString* string, time_t time, bool longFormat)
|
|||||||
UnicodeString ICUString;
|
UnicodeString ICUString;
|
||||||
ICUString = timeFormatter->format((UDate)time * 1000, ICUString);
|
ICUString = timeFormatter->format((UDate)time * 1000, ICUString);
|
||||||
|
|
||||||
|
CheckedArrayByteSink stringConverter(string, maxSize);
|
||||||
|
|
||||||
|
ICUString.toUTF8(stringConverter);
|
||||||
|
|
||||||
|
if (stringConverter.Overflowed())
|
||||||
|
return B_BAD_VALUE;
|
||||||
|
else
|
||||||
|
return B_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
status_t
|
||||||
|
BCountry::FormatTime(BString* string, time_t time, bool longFormat)
|
||||||
|
{
|
||||||
string->Truncate(0);
|
string->Truncate(0);
|
||||||
|
|
||||||
|
ICU_VERSION::DateFormat* timeFormatter = TimeFormatter(longFormat);
|
||||||
|
if (timeFormatter == NULL)
|
||||||
|
return B_NO_MEMORY;
|
||||||
|
|
||||||
|
UnicodeString ICUString;
|
||||||
|
ICUString = timeFormatter->format((UDate)time * 1000, ICUString);
|
||||||
|
|
||||||
BStringByteSink stringConverter(string);
|
BStringByteSink stringConverter(string);
|
||||||
|
|
||||||
ICUString.toUTF8(stringConverter);
|
ICUString.toUTF8(stringConverter);
|
||||||
@@ -439,6 +464,8 @@ status_t
|
|||||||
BCountry::FormatTime(BString* string, int*& fieldPositions, int& fieldCount,
|
BCountry::FormatTime(BString* string, int*& fieldPositions, int& fieldCount,
|
||||||
time_t time, bool longFormat)
|
time_t time, bool longFormat)
|
||||||
{
|
{
|
||||||
|
string->Truncate(0);
|
||||||
|
|
||||||
ICU_VERSION::DateFormat* timeFormatter = TimeFormatter(longFormat);
|
ICU_VERSION::DateFormat* timeFormatter = TimeFormatter(longFormat);
|
||||||
if (timeFormatter == NULL)
|
if (timeFormatter == NULL)
|
||||||
return B_NO_MEMORY;
|
return B_NO_MEMORY;
|
||||||
@@ -467,7 +494,6 @@ BCountry::FormatTime(BString* string, int*& fieldPositions, int& fieldCount,
|
|||||||
for (int i = 0 ; i < fieldCount ; i++ )
|
for (int i = 0 ; i < fieldCount ; i++ )
|
||||||
fieldPositions[i] = fieldPosStorage[i];
|
fieldPositions[i] = fieldPosStorage[i];
|
||||||
|
|
||||||
string->Truncate(0);
|
|
||||||
BStringByteSink stringConverter(string);
|
BStringByteSink stringConverter(string);
|
||||||
|
|
||||||
ICUString.toUTF8(stringConverter);
|
ICUString.toUTF8(stringConverter);
|
||||||
|
|||||||
Reference in New Issue
Block a user