ICU-5427 Fix some memory leaks, simplify initialization, and follow desired policy stated in ticket 2796.
X-SVN-Rev: 21743
This commit is contained in:
parent
e58b1d39de
commit
0da8673589
@ -28,8 +28,8 @@ U_NAMESPACE_BEGIN
|
||||
*/
|
||||
struct URelativeString {
|
||||
int32_t offset; /** offset of this item, such as, the relative date **/
|
||||
const UChar* string; /** string, or NULL if not set **/
|
||||
int32_t len; /** length of the string **/
|
||||
const UChar* string; /** string, or NULL if not set **/
|
||||
};
|
||||
|
||||
|
||||
@ -38,13 +38,19 @@ UOBJECT_DEFINE_RTTI_IMPLEMENTATION(RelativeDateFormat)
|
||||
RelativeDateFormat::RelativeDateFormat(const RelativeDateFormat& other) :
|
||||
DateFormat(other), fDateFormat(NULL), fTimeFormat(NULL), fCombinedFormat(NULL),
|
||||
fDateStyle(other.fDateStyle), fTimeStyle(other.fTimeStyle), fLocale(other.fLocale),
|
||||
fCalData(NULL), fStrings(NULL), fDatesLen(0), fDates(NULL)
|
||||
fDatesLen(other.fDatesLen), fDayMin(other.fDayMin), fDayMax(other.fDayMax),
|
||||
fDates(NULL)
|
||||
{
|
||||
if(other.fDateFormat != NULL) {
|
||||
fDateFormat = (DateFormat*)other.fDateFormat->clone();
|
||||
} else {
|
||||
fDateFormat = NULL;
|
||||
}
|
||||
if (fDatesLen > 0) {
|
||||
fDates = (URelativeString*) uprv_malloc(sizeof(fDates[0])*fDatesLen);
|
||||
uprv_memcpy(fDates, other.fDates, sizeof(fDates[0])*fDatesLen);
|
||||
}
|
||||
//fCalendar = other.fCalendar->clone();
|
||||
/*
|
||||
if(other.fTimeFormat != NULL) {
|
||||
fTimeFormat = (DateFormat*)other.fTimeFormat->clone();
|
||||
@ -56,7 +62,7 @@ fCalData(NULL), fStrings(NULL), fDatesLen(0), fDates(NULL)
|
||||
|
||||
RelativeDateFormat::RelativeDateFormat( UDateFormatStyle timeStyle, UDateFormatStyle dateStyle, const Locale& locale, UErrorCode& status)
|
||||
: DateFormat(), fDateFormat(NULL), fTimeFormat(NULL), fCombinedFormat(NULL),
|
||||
fDateStyle(dateStyle), fTimeStyle(timeStyle), fCalData(NULL), fStrings(NULL), fDatesLen(0), fDates(NULL)
|
||||
fDateStyle(dateStyle), fTimeStyle(timeStyle), fDatesLen(0), fDates(NULL)
|
||||
{
|
||||
if(U_FAILURE(status) ) {
|
||||
return;
|
||||
@ -75,6 +81,7 @@ fDateStyle(dateStyle), fTimeStyle(timeStyle), fCalData(NULL), fStrings(NULL), fD
|
||||
|
||||
// Initialize the parent fCalendar, so that parse() works correctly.
|
||||
initializeCalendar(NULL, locale, status);
|
||||
loadDates(status);
|
||||
}
|
||||
|
||||
RelativeDateFormat::~RelativeDateFormat() {
|
||||
@ -82,14 +89,11 @@ RelativeDateFormat::~RelativeDateFormat() {
|
||||
delete fTimeFormat;
|
||||
delete fCombinedFormat;
|
||||
uprv_free(fDates);
|
||||
// do NOT: delete fStrings - as they are loaded from mapped memory, all owned by fCalData.
|
||||
delete fCalData;
|
||||
}
|
||||
|
||||
|
||||
Format* RelativeDateFormat::clone(void) const {
|
||||
RelativeDateFormat *other = new RelativeDateFormat(*this);
|
||||
return other;
|
||||
return new RelativeDateFormat(*this);
|
||||
}
|
||||
|
||||
UBool RelativeDateFormat::operator==(const Format& other) const {
|
||||
@ -202,42 +206,11 @@ RelativeDateFormat::parse(const UnicodeString& text, UErrorCode& status) const
|
||||
}
|
||||
|
||||
|
||||
UResourceBundle *RelativeDateFormat::getStrings(UErrorCode& status) const {
|
||||
if(fCalData == NULL) {
|
||||
// fCalData owns the subsequent strings
|
||||
((RelativeDateFormat*)this)->fCalData = new CalendarData(fLocale, "gregorian", status);
|
||||
}
|
||||
|
||||
if(fStrings == NULL) {
|
||||
// load the string object
|
||||
UResourceBundle *theStrings = fCalData->getByKey3("fields", "day", "relative", status);
|
||||
|
||||
if (U_FAILURE(status)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
((RelativeDateFormat*)this)->fStrings = theStrings; // cast away const
|
||||
}
|
||||
return fStrings;
|
||||
}
|
||||
|
||||
const UChar *RelativeDateFormat::getStringForDay(int32_t day, int32_t &len, UErrorCode &status) const {
|
||||
if(U_FAILURE(status)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(fDates == NULL) {
|
||||
loadDates(status);
|
||||
if(U_FAILURE(status)) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
// no strings.
|
||||
if(fDatesLen == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Is it outside the resource bundle's range?
|
||||
if(day < fDayMin || day > fDayMax) {
|
||||
return NULL; // don't have it.
|
||||
@ -254,22 +227,21 @@ const UChar *RelativeDateFormat::getStringForDay(int32_t day, int32_t &len, UErr
|
||||
return NULL; // not found.
|
||||
}
|
||||
|
||||
void RelativeDateFormat::loadDates(UErrorCode &status) const {
|
||||
UResourceBundle *strings = getStrings(status);
|
||||
void RelativeDateFormat::loadDates(UErrorCode &status) {
|
||||
CalendarData calData(fLocale, "gregorian", status);
|
||||
UResourceBundle *strings = calData.getByKey3("fields", "day", "relative", status);
|
||||
|
||||
RelativeDateFormat *nonConstThis = ((RelativeDateFormat*)this); // cast away const.
|
||||
|
||||
// set up min/max
|
||||
nonConstThis->fDayMin=-1;
|
||||
nonConstThis->fDayMax=1;
|
||||
fDayMin=-1;
|
||||
fDayMax=1;
|
||||
|
||||
if(U_FAILURE(status)) {
|
||||
nonConstThis->fDatesLen=0;
|
||||
fDatesLen=0;
|
||||
return;
|
||||
}
|
||||
|
||||
nonConstThis->fDatesLen = ures_getSize(strings);
|
||||
nonConstThis->fDates = (URelativeString*) uprv_malloc(sizeof(fDates[0])*fDatesLen);
|
||||
fDatesLen = ures_getSize(strings);
|
||||
fDates = (URelativeString*) uprv_malloc(sizeof(fDates[0])*fDatesLen);
|
||||
|
||||
// Load in each item into the array...
|
||||
int n = 0;
|
||||
@ -295,19 +267,20 @@ void RelativeDateFormat::loadDates(UErrorCode &status) const {
|
||||
|
||||
// set min/max
|
||||
if(offset < fDayMin) {
|
||||
nonConstThis->fDayMin = offset;
|
||||
fDayMin = offset;
|
||||
}
|
||||
if(offset > fDayMax) {
|
||||
nonConstThis->fDayMax = offset;
|
||||
fDayMax = offset;
|
||||
}
|
||||
|
||||
// copy the string pointer
|
||||
nonConstThis->fDates[n].offset = offset;
|
||||
nonConstThis->fDates[n].string = aString;
|
||||
nonConstThis->fDates[n].len = aLen;
|
||||
fDates[n].offset = offset;
|
||||
fDates[n].string = aString;
|
||||
fDates[n].len = aLen;
|
||||
|
||||
n++;
|
||||
}
|
||||
ures_close(subString);
|
||||
|
||||
// the fDates[] array could be sorted here, for direct access.
|
||||
}
|
||||
|
@ -22,7 +22,6 @@
|
||||
U_NAMESPACE_BEGIN
|
||||
|
||||
// forward declarations
|
||||
class CalendarData;
|
||||
class MessageFormat;
|
||||
|
||||
// internal structure used for caching strings
|
||||
@ -192,9 +191,6 @@ private:
|
||||
UDateFormatStyle fTimeStyle;
|
||||
Locale fLocale;
|
||||
|
||||
CalendarData *fCalData; // holds a reference to the resource data
|
||||
UResourceBundle *fStrings; // the array of strings
|
||||
|
||||
int32_t fDayMin; // day id of lowest #
|
||||
int32_t fDayMax; // day id of highest #
|
||||
int32_t fDatesLen; // Length of array
|
||||
@ -212,14 +208,8 @@ private:
|
||||
/**
|
||||
* Load the Date string array
|
||||
*/
|
||||
void loadDates(UErrorCode &status) const;
|
||||
void loadDates(UErrorCode &status);
|
||||
|
||||
/**
|
||||
* Load the Strings resource needed for relative dates. Returns an error and null if it could not be loaded.
|
||||
* Semantic const.
|
||||
*/
|
||||
UResourceBundle *getStrings(UErrorCode &status) const;
|
||||
|
||||
/**
|
||||
* @return the number of days in "until-now"
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user