]> git.saurik.com Git - apple/icu.git/blame - icuSources/io/locbund.c
ICU-6.2.22.tar.gz
[apple/icu.git] / icuSources / io / locbund.c
CommitLineData
374ca955
A
1/*
2*******************************************************************************
3*
4* Copyright (C) 1998-2004, International Business Machines
5* Corporation and others. All Rights Reserved.
6*
7*******************************************************************************
8*
9* File locbund.c
10*
11* Modification History:
12*
13* Date Name Description
14* 11/18/98 stephen Creation.
15* 12/10/1999 bobbyr@optiosoftware.com Fix for memory leak + string allocation bugs
16*******************************************************************************
17*/
18
19#include <stdlib.h>
20
21#include "unicode/utypes.h"
22
23#if !UCONFIG_NO_FORMATTING
24
25#include "locbund.h"
26
27#include "cmemory.h"
28#include "unicode/ustring.h"
29#include "unicode/uloc.h"
30
31ULocaleBundle*
32u_locbund_init(ULocaleBundle *result, const char *loc)
33{
34 int32_t len;
35
36 if(result == 0)
37 return 0;
38
39 if (loc == NULL) {
40 loc = uloc_getDefault();
41 }
42
43 uprv_memset(result, 0, sizeof(ULocaleBundle));
44
45 len = (int32_t)strlen(loc);
46 result->fLocale = (char*) uprv_malloc(len + 1);
47 if(result->fLocale == 0) {
48 return 0;
49 }
50
51 strcpy(result->fLocale, loc);
52
53 return result;
54}
55
56/*ULocaleBundle*
57u_locbund_new(const char *loc)
58{
59 ULocaleBundle *result = (ULocaleBundle*) uprv_malloc(sizeof(ULocaleBundle));
60 return u_locbund_init(result, loc);
61}
62
63ULocaleBundle*
64u_locbund_clone(const ULocaleBundle *bundle)
65{
66 ULocaleBundle *result = (ULocaleBundle*)uprv_malloc(sizeof(ULocaleBundle));
67 UErrorCode status = U_ZERO_ERROR;
68 int32_t styleIdx;
69
70 if(result == 0)
71 return 0;
72
73 result->fLocale = (char*) uprv_malloc(strlen(bundle->fLocale) + 1);
74 if(result->fLocale == 0) {
75 uprv_free(result);
76 return 0;
77 }
78
79 strcpy(result->fLocale, bundle->fLocale );
80
81 for (styleIdx = 0; styleIdx < ULOCALEBUNDLE_NUMBERFORMAT_COUNT; styleIdx++) {
82 status = U_ZERO_ERROR;
83 if (result->fNumberFormat[styleIdx]) {
84 result->fNumberFormat[styleIdx] = unum_clone(bundle->fNumberFormat[styleIdx], &status);
85 if (U_FAILURE(status)) {
86 result->fNumberFormat[styleIdx] = NULL;
87 }
88 }
89 else {
90 result->fNumberFormat[styleIdx] = NULL;
91 }
92 }
93 result->fDateFormat = (bundle->fDateFormat == 0 ? 0 :
94 udat_clone(bundle->fDateFormat, &status));
95 result->fTimeFormat = (bundle->fTimeFormat == 0 ? 0 :
96 udat_clone(bundle->fTimeFormat, &status));
97
98 return result;
99}*/
100
101void
102u_locbund_close(ULocaleBundle *bundle)
103{
104 int32_t styleIdx;
105
106 uprv_free(bundle->fLocale);
107
108 for (styleIdx = 0; styleIdx < ULOCALEBUNDLE_NUMBERFORMAT_COUNT; styleIdx++) {
109 if (bundle->fNumberFormat[styleIdx]) {
110 unum_close(bundle->fNumberFormat[styleIdx]);
111 }
112 }
113
114 uprv_memset(bundle, 0, sizeof(ULocaleBundle));
115/* uprv_free(bundle);*/
116}
117
118UNumberFormat*
119u_locbund_getNumberFormat(ULocaleBundle *bundle, UNumberFormatStyle style)
120{
121 UNumberFormat *formatAlias = NULL;
122 if (style >= UNUM_IGNORE) {
123 formatAlias = bundle->fNumberFormat[style-1];
124 if (formatAlias == NULL) {
125 UErrorCode status = U_ZERO_ERROR;
126 formatAlias = unum_open(style, NULL, 0, bundle->fLocale, NULL, &status);
127 if (U_FAILURE(status)) {
128 unum_close(formatAlias);
129 formatAlias = NULL;
130 }
131 else {
132 bundle->fNumberFormat[style-1] = formatAlias;
133 }
134 }
135 }
136 return formatAlias;
137}
138
139#endif /* #if !UCONFIG_NO_FORMATTING */