From b2c385c0727b400b447efa535a3a67042bd52af8 Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Wed, 17 Feb 2010 00:02:50 +0000 Subject: [PATCH] -Added new TimeFormat API for formatting and localizing a time (uptime, copy duration, ...) -Added a very simple test that shows the API is corrupting memory and ends up crashing -Fixed build of other locale tests git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@35503 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/os/locale/Format.h | 2 +- headers/os/locale/NumberFormat.h | 2 +- headers/os/locale/TimeFormat.h | 25 +++++++++++++++++++ src/kits/locale/FloatFormat.cpp | 2 +- src/kits/locale/Format.cpp | 3 +-- src/kits/locale/IntegerFormat.cpp | 2 +- src/kits/locale/Jamfile | 1 + src/kits/locale/LocaleRoster.cpp | 10 +++----- src/kits/locale/NumberFormat.cpp | 4 +-- src/kits/locale/TimeFormat.cpp | 37 ++++++++++++++++++++++++++++ src/tests/kits/locale/Jamfile | 3 ++- src/tests/kits/locale/formatTest.cpp | 15 +++++++++++ 12 files changed, 90 insertions(+), 16 deletions(-) create mode 100644 headers/os/locale/TimeFormat.h create mode 100644 src/kits/locale/TimeFormat.cpp create mode 100644 src/tests/kits/locale/formatTest.cpp diff --git a/headers/os/locale/Format.h b/headers/os/locale/Format.h index 75f3bd0c89..ccd13e19ff 100644 --- a/headers/os/locale/Format.h +++ b/headers/os/locale/Format.h @@ -39,7 +39,7 @@ class BFormat { BFormat &operator=(const BFormat &other); - BFormat(BFormatImpl *impl); + BFormat(); protected: BFormatImpl *fImpl; diff --git a/headers/os/locale/NumberFormat.h b/headers/os/locale/NumberFormat.h index 1f13442eb4..0d19097c89 100644 --- a/headers/os/locale/NumberFormat.h +++ b/headers/os/locale/NumberFormat.h @@ -13,7 +13,7 @@ class BNumberFormat : public BFormat { BNumberFormat &operator=(const BNumberFormat &other); - BNumberFormat(BNumberFormatImpl *impl); + BNumberFormat(); private: inline BNumberFormatImpl *NumberFormatImpl() const; diff --git a/headers/os/locale/TimeFormat.h b/headers/os/locale/TimeFormat.h new file mode 100644 index 0000000000..5b906c7c44 --- /dev/null +++ b/headers/os/locale/TimeFormat.h @@ -0,0 +1,25 @@ +/* + * Copyright 2010, Haiku, Inc. + * Distributed under the terms of the MIT License. + */ +#ifndef _B_TIME_FORMAT_H_ +#define _B_TIME_FORMAT_H_ + + +#include +#include + + +class BString; + + +class BTimeFormat : public BNumberFormat { + public: + status_t Format(int64 number, BString *buffer) const; + + // TODO : version for char* buffer, size_t bufferSize + // TODO : parsing ? +}; + + +#endif diff --git a/src/kits/locale/FloatFormat.cpp b/src/kits/locale/FloatFormat.cpp index 8ecb5f6132..a35162594e 100644 --- a/src/kits/locale/FloatFormat.cpp +++ b/src/kits/locale/FloatFormat.cpp @@ -45,7 +45,7 @@ BFloatFormat::operator=(const BFloatFormat &other) // constructor BFloatFormat::BFloatFormat(BFloatFormatImpl *impl) - : BNumberFormat(impl), + : BNumberFormat(), BFloatFormatParameters(impl ? impl->DefaultFloatFormatParameters() : NULL) { diff --git a/src/kits/locale/Format.cpp b/src/kits/locale/Format.cpp index 97eba4cb5c..4e7654143c 100644 --- a/src/kits/locale/Format.cpp +++ b/src/kits/locale/Format.cpp @@ -21,8 +21,7 @@ BFormat::operator=(const BFormat &other) } // constructor -BFormat::BFormat(BFormatImpl *impl) - : fImpl(impl) +BFormat::BFormat() { } diff --git a/src/kits/locale/IntegerFormat.cpp b/src/kits/locale/IntegerFormat.cpp index 870becf399..b6d954c243 100644 --- a/src/kits/locale/IntegerFormat.cpp +++ b/src/kits/locale/IntegerFormat.cpp @@ -45,7 +45,7 @@ BIntegerFormat::operator=(const BIntegerFormat &other) // constructor BIntegerFormat::BIntegerFormat(BIntegerFormatImpl *impl) - : BNumberFormat(impl), + : BNumberFormat(), BIntegerFormatParameters(impl ? impl->DefaultIntegerFormatParameters() : NULL) { diff --git a/src/kits/locale/Jamfile b/src/kits/locale/Jamfile index 6ebc7fdf53..e83ec61249 100644 --- a/src/kits/locale/Jamfile +++ b/src/kits/locale/Jamfile @@ -31,6 +31,7 @@ SharedLibrary liblocale.so NumberFormatImpl.cpp NumberFormatParameters.cpp PropertyFile.cpp + TimeFormat.cpp UnicodeChar.cpp : be $(TARGET_LIBSTDC++) libicu-common.so libicu-i18n.so ; diff --git a/src/kits/locale/LocaleRoster.cpp b/src/kits/locale/LocaleRoster.cpp index a1c6debffe..010bd698be 100644 --- a/src/kits/locale/LocaleRoster.cpp +++ b/src/kits/locale/LocaleRoster.cpp @@ -602,18 +602,15 @@ BLocaleRoster::LoadCatalog(const char *signature, const char *language, catalog = info->fInstantiateFunc(signature, lang, fingerprint); if (catalog) { info->fLoadedCatalogs.AddItem(catalog); -#if 0 - // Chain-loading of catalogs has been disabled, as with the - // current way of handling languages (there are no general - // languages like 'English', but only specialized ones, like - // 'English-american') it does not make sense... - // // Chain-load catalogs for languages that depend on // other languages. // The current implementation uses the filename in order to // detect dependencies (parenthood) between languages (it // traverses from "english-british-oxford" to "english-british" // to "english"): + // TODO :use ICU facilities instead, so we can handle more + // complex things such as fr_FR@euro, or whatever, encodings + // and so on. int32 pos; BString langName(lang); BCatalogAddOn *currCatalog=catalog, *nextCatalog; @@ -628,7 +625,6 @@ BLocaleRoster::LoadCatalog(const char *signature, const char *language, currCatalog = nextCatalog; } } -#endif return catalog; } } diff --git a/src/kits/locale/NumberFormat.cpp b/src/kits/locale/NumberFormat.cpp index 604f1524c3..48001cd12f 100644 --- a/src/kits/locale/NumberFormat.cpp +++ b/src/kits/locale/NumberFormat.cpp @@ -21,8 +21,8 @@ BNumberFormat::operator=(const BNumberFormat &other) } // constructor -BNumberFormat::BNumberFormat(BNumberFormatImpl *impl) - : BFormat(impl) +BNumberFormat::BNumberFormat() + : BFormat() { } diff --git a/src/kits/locale/TimeFormat.cpp b/src/kits/locale/TimeFormat.cpp new file mode 100644 index 0000000000..eada59cd35 --- /dev/null +++ b/src/kits/locale/TimeFormat.cpp @@ -0,0 +1,37 @@ +/* + * Copyright 2010, Adrien Destugues, pulkomandy@gmail.com + * Distributed under the terms of the MIT License. + */ + +#include + +#include +#include +#include +#include + +status_t BTimeFormat::Format(int64 number, BString* buffer) const +{ + // create time unit amount instance - a combination of Number and time unit + UErrorCode status = U_ZERO_ERROR; + TimeUnitAmount* source = new TimeUnitAmount(number/1000000, TimeUnit::UTIMEUNIT_SECOND, status); + // create time unit format instance + TimeUnitFormat* format = new TimeUnitFormat(status); + // format a time unit amount + UnicodeString formatted; + Formattable formattable(source); + if (!U_SUCCESS(status)) { + delete source; + delete format; + return B_ERROR; + } + + formatted = ((icu_4_2::Format*)format)->format(formattable, formatted, status); + + BStringByteSink bbs(buffer); + formatted.toUTF8(bbs); + + delete source; + delete format; + return B_OK; +} diff --git a/src/tests/kits/locale/Jamfile b/src/tests/kits/locale/Jamfile index 4670d952c0..ed167dfa66 100644 --- a/src/tests/kits/locale/Jamfile +++ b/src/tests/kits/locale/Jamfile @@ -23,7 +23,7 @@ rule LocaleTest # LocaleTest ; local sources = $(1) ; local name = $(sources[1]:B) ; - Application $(name) : $(sources) : be liblocale.so $(TARGET_LIBSUPC++) ; + Application $(name) : $(sources) : be liblocale.so $(TARGET_LIBSTDC++) $(TARGET_LIBSUPC++) ; } LocaleTest catalogSpeed.cpp ; @@ -32,6 +32,7 @@ LocaleTest collatorSpeed.cpp ; LocaleTest collatorTest.cpp ; LocaleTest genericNumberFormatTest.cpp ; LocaleTest localeTest.cpp ; +LocaleTest formatTest.cpp ; Application ICUTest : ICUTest.cpp : diff --git a/src/tests/kits/locale/formatTest.cpp b/src/tests/kits/locale/formatTest.cpp new file mode 100644 index 0000000000..7dd7dc8c28 --- /dev/null +++ b/src/tests/kits/locale/formatTest.cpp @@ -0,0 +1,15 @@ +#include + +#include + +#include +#include + +int main() { + BTimeFormat timeFormatter; + BString str; + + timeFormatter.Format(123456, &str); + + std::cout << str.String(); +}