-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
This commit is contained in:
Adrien Destugues 2010-02-17 00:02:50 +00:00
parent 04761ff76d
commit b2c385c072
12 changed files with 90 additions and 16 deletions

View File

@ -39,7 +39,7 @@ class BFormat {
BFormat &operator=(const BFormat &other);
BFormat(BFormatImpl *impl);
BFormat();
protected:
BFormatImpl *fImpl;

View File

@ -13,7 +13,7 @@ class BNumberFormat : public BFormat {
BNumberFormat &operator=(const BNumberFormat &other);
BNumberFormat(BNumberFormatImpl *impl);
BNumberFormat();
private:
inline BNumberFormatImpl *NumberFormatImpl() const;

View File

@ -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 <NumberFormat.h>
#include <SupportDefs.h>
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

View File

@ -45,7 +45,7 @@ BFloatFormat::operator=(const BFloatFormat &other)
// constructor
BFloatFormat::BFloatFormat(BFloatFormatImpl *impl)
: BNumberFormat(impl),
: BNumberFormat(),
BFloatFormatParameters(impl ? impl->DefaultFloatFormatParameters()
: NULL)
{

View File

@ -21,8 +21,7 @@ BFormat::operator=(const BFormat &other)
}
// constructor
BFormat::BFormat(BFormatImpl *impl)
: fImpl(impl)
BFormat::BFormat()
{
}

View File

@ -45,7 +45,7 @@ BIntegerFormat::operator=(const BIntegerFormat &other)
// constructor
BIntegerFormat::BIntegerFormat(BIntegerFormatImpl *impl)
: BNumberFormat(impl),
: BNumberFormat(),
BIntegerFormatParameters(impl ? impl->DefaultIntegerFormatParameters()
: NULL)
{

View File

@ -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
;

View File

@ -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;
}
}

View File

@ -21,8 +21,8 @@ BNumberFormat::operator=(const BNumberFormat &other)
}
// constructor
BNumberFormat::BNumberFormat(BNumberFormatImpl *impl)
: BFormat(impl)
BNumberFormat::BNumberFormat()
: BFormat()
{
}

View File

@ -0,0 +1,37 @@
/*
* Copyright 2010, Adrien Destugues, pulkomandy@gmail.com
* Distributed under the terms of the MIT License.
*/
#include <TimeFormat.h>
#include <unicode/format.h>
#include <unicode/tmutfmt.h>
#include <unicode/utypes.h>
#include <ICUWrapper.h>
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;
}

View File

@ -23,7 +23,7 @@ rule LocaleTest
# LocaleTest <sources> ;
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 :

View File

@ -0,0 +1,15 @@
#include <iostream>
#include <time.h>
#include <String.h>
#include <TimeFormat.h>
int main() {
BTimeFormat timeFormatter;
BString str;
timeFormatter.Format(123456, &str);
std::cout << str.String();
}