scuffed-code/icu4c/source/io/locbund.c

140 lines
3.6 KiB
C
Raw Normal View History

1999-08-16 21:50:52 +00:00
/*
*******************************************************************************
*
* Copyright (C) 1998-2004, International Business Machines
* Corporation and others. All Rights Reserved.
*
1999-08-16 21:50:52 +00:00
*******************************************************************************
*
* File locbund.c
*
* Modification History:
*
* Date Name Description
* 11/18/98 stephen Creation.
* 12/10/1999 bobbyr@optiosoftware.com Fix for memory leak + string allocation bugs
1999-08-16 21:50:52 +00:00
*******************************************************************************
*/
#include <stdlib.h>
#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
1999-08-16 21:50:52 +00:00
#include "locbund.h"
#include "cmemory.h"
#include "unicode/ustring.h"
#include "unicode/uloc.h"
1999-08-16 21:50:52 +00:00
ULocaleBundle*
u_locbund_init(ULocaleBundle *result, const char *loc)
1999-08-16 21:50:52 +00:00
{
int32_t len;
1999-08-16 21:50:52 +00:00
if(result == 0)
return 0;
1999-08-16 21:50:52 +00:00
if (loc == NULL) {
loc = uloc_getDefault();
}
uprv_memset(result, 0, sizeof(ULocaleBundle));
len = (int32_t)strlen(loc);
result->fLocale = (char*) uprv_malloc(len + 1);
if(result->fLocale == 0) {
return 0;
}
strcpy(result->fLocale, loc);
return result;
1999-08-16 21:50:52 +00:00
}
/*ULocaleBundle*
u_locbund_new(const char *loc)
1999-08-16 21:50:52 +00:00
{
ULocaleBundle *result = (ULocaleBundle*) uprv_malloc(sizeof(ULocaleBundle));
return u_locbund_init(result, loc);
1999-08-16 21:50:52 +00:00
}
ULocaleBundle*
u_locbund_clone(const ULocaleBundle *bundle)
1999-08-16 21:50:52 +00:00
{
ULocaleBundle *result = (ULocaleBundle*)uprv_malloc(sizeof(ULocaleBundle));
UErrorCode status = U_ZERO_ERROR;
int32_t styleIdx;
if(result == 0)
return 0;
result->fLocale = (char*) uprv_malloc(strlen(bundle->fLocale) + 1);
if(result->fLocale == 0) {
uprv_free(result);
return 0;
}
strcpy(result->fLocale, bundle->fLocale );
for (styleIdx = 0; styleIdx < ULOCALEBUNDLE_NUMBERFORMAT_COUNT; styleIdx++) {
status = U_ZERO_ERROR;
if (result->fNumberFormat[styleIdx]) {
result->fNumberFormat[styleIdx] = unum_clone(bundle->fNumberFormat[styleIdx], &status);
if (U_FAILURE(status)) {
result->fNumberFormat[styleIdx] = NULL;
}
}
else {
result->fNumberFormat[styleIdx] = NULL;
}
}
result->fDateFormat = (bundle->fDateFormat == 0 ? 0 :
udat_clone(bundle->fDateFormat, &status));
result->fTimeFormat = (bundle->fTimeFormat == 0 ? 0 :
udat_clone(bundle->fTimeFormat, &status));
return result;
}*/
1999-08-16 21:50:52 +00:00
void
u_locbund_close(ULocaleBundle *bundle)
1999-08-16 21:50:52 +00:00
{
int32_t styleIdx;
1999-08-16 21:50:52 +00:00
uprv_free(bundle->fLocale);
1999-08-16 21:50:52 +00:00
for (styleIdx = 0; styleIdx < ULOCALEBUNDLE_NUMBERFORMAT_COUNT; styleIdx++) {
if (bundle->fNumberFormat[styleIdx]) {
unum_close(bundle->fNumberFormat[styleIdx]);
}
}
1999-08-16 21:50:52 +00:00
uprv_memset(bundle, 0, sizeof(ULocaleBundle));
/* uprv_free(bundle);*/
1999-08-16 21:50:52 +00:00
}
UNumberFormat*
u_locbund_getNumberFormat(ULocaleBundle *bundle, UNumberFormatStyle style)
1999-08-16 21:50:52 +00:00
{
UNumberFormat *formatAlias = NULL;
if (style >= UNUM_IGNORE) {
formatAlias = bundle->fNumberFormat[style-1];
if (formatAlias == NULL) {
UErrorCode status = U_ZERO_ERROR;
formatAlias = unum_open(style, NULL, 0, bundle->fLocale, NULL, &status);
if (U_FAILURE(status)) {
unum_close(formatAlias);
formatAlias = NULL;
}
else {
bundle->fNumberFormat[style-1] = formatAlias;
}
}
}
return formatAlias;
1999-08-16 21:50:52 +00:00
}
#endif /* #if !UCONFIG_NO_FORMATTING */