-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:
@@ -39,7 +39,7 @@ class BFormat {
|
|||||||
|
|
||||||
BFormat &operator=(const BFormat &other);
|
BFormat &operator=(const BFormat &other);
|
||||||
|
|
||||||
BFormat(BFormatImpl *impl);
|
BFormat();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
BFormatImpl *fImpl;
|
BFormatImpl *fImpl;
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ class BNumberFormat : public BFormat {
|
|||||||
|
|
||||||
BNumberFormat &operator=(const BNumberFormat &other);
|
BNumberFormat &operator=(const BNumberFormat &other);
|
||||||
|
|
||||||
BNumberFormat(BNumberFormatImpl *impl);
|
BNumberFormat();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
inline BNumberFormatImpl *NumberFormatImpl() const;
|
inline BNumberFormatImpl *NumberFormatImpl() const;
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -45,7 +45,7 @@ BFloatFormat::operator=(const BFloatFormat &other)
|
|||||||
|
|
||||||
// constructor
|
// constructor
|
||||||
BFloatFormat::BFloatFormat(BFloatFormatImpl *impl)
|
BFloatFormat::BFloatFormat(BFloatFormatImpl *impl)
|
||||||
: BNumberFormat(impl),
|
: BNumberFormat(),
|
||||||
BFloatFormatParameters(impl ? impl->DefaultFloatFormatParameters()
|
BFloatFormatParameters(impl ? impl->DefaultFloatFormatParameters()
|
||||||
: NULL)
|
: NULL)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -21,8 +21,7 @@ BFormat::operator=(const BFormat &other)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// constructor
|
// constructor
|
||||||
BFormat::BFormat(BFormatImpl *impl)
|
BFormat::BFormat()
|
||||||
: fImpl(impl)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ BIntegerFormat::operator=(const BIntegerFormat &other)
|
|||||||
|
|
||||||
// constructor
|
// constructor
|
||||||
BIntegerFormat::BIntegerFormat(BIntegerFormatImpl *impl)
|
BIntegerFormat::BIntegerFormat(BIntegerFormatImpl *impl)
|
||||||
: BNumberFormat(impl),
|
: BNumberFormat(),
|
||||||
BIntegerFormatParameters(impl ? impl->DefaultIntegerFormatParameters()
|
BIntegerFormatParameters(impl ? impl->DefaultIntegerFormatParameters()
|
||||||
: NULL)
|
: NULL)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ SharedLibrary liblocale.so
|
|||||||
NumberFormatImpl.cpp
|
NumberFormatImpl.cpp
|
||||||
NumberFormatParameters.cpp
|
NumberFormatParameters.cpp
|
||||||
PropertyFile.cpp
|
PropertyFile.cpp
|
||||||
|
TimeFormat.cpp
|
||||||
UnicodeChar.cpp
|
UnicodeChar.cpp
|
||||||
: be $(TARGET_LIBSTDC++) libicu-common.so libicu-i18n.so
|
: be $(TARGET_LIBSTDC++) libicu-common.so libicu-i18n.so
|
||||||
;
|
;
|
||||||
|
|||||||
@@ -602,18 +602,15 @@ BLocaleRoster::LoadCatalog(const char *signature, const char *language,
|
|||||||
catalog = info->fInstantiateFunc(signature, lang, fingerprint);
|
catalog = info->fInstantiateFunc(signature, lang, fingerprint);
|
||||||
if (catalog) {
|
if (catalog) {
|
||||||
info->fLoadedCatalogs.AddItem(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
|
// Chain-load catalogs for languages that depend on
|
||||||
// other languages.
|
// other languages.
|
||||||
// The current implementation uses the filename in order to
|
// The current implementation uses the filename in order to
|
||||||
// detect dependencies (parenthood) between languages (it
|
// detect dependencies (parenthood) between languages (it
|
||||||
// traverses from "english-british-oxford" to "english-british"
|
// traverses from "english-british-oxford" to "english-british"
|
||||||
// to "english"):
|
// 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;
|
int32 pos;
|
||||||
BString langName(lang);
|
BString langName(lang);
|
||||||
BCatalogAddOn *currCatalog=catalog, *nextCatalog;
|
BCatalogAddOn *currCatalog=catalog, *nextCatalog;
|
||||||
@@ -628,7 +625,6 @@ BLocaleRoster::LoadCatalog(const char *signature, const char *language,
|
|||||||
currCatalog = nextCatalog;
|
currCatalog = nextCatalog;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
return catalog;
|
return catalog;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,8 +21,8 @@ BNumberFormat::operator=(const BNumberFormat &other)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// constructor
|
// constructor
|
||||||
BNumberFormat::BNumberFormat(BNumberFormatImpl *impl)
|
BNumberFormat::BNumberFormat()
|
||||||
: BFormat(impl)
|
: BFormat()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010, Adrien Destugues, [email protected]
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
@@ -23,7 +23,7 @@ rule LocaleTest
|
|||||||
# LocaleTest <sources> ;
|
# LocaleTest <sources> ;
|
||||||
local sources = $(1) ;
|
local sources = $(1) ;
|
||||||
local name = $(sources[1]:B) ;
|
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 ;
|
LocaleTest catalogSpeed.cpp ;
|
||||||
@@ -32,6 +32,7 @@ LocaleTest collatorSpeed.cpp ;
|
|||||||
LocaleTest collatorTest.cpp ;
|
LocaleTest collatorTest.cpp ;
|
||||||
LocaleTest genericNumberFormatTest.cpp ;
|
LocaleTest genericNumberFormatTest.cpp ;
|
||||||
LocaleTest localeTest.cpp ;
|
LocaleTest localeTest.cpp ;
|
||||||
|
LocaleTest formatTest.cpp ;
|
||||||
|
|
||||||
Application ICUTest :
|
Application ICUTest :
|
||||||
ICUTest.cpp :
|
ICUTest.cpp :
|
||||||
|
|||||||
@@ -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();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user