2 *******************************************************************************
3 * Copyright (C) 1997-2011, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 *******************************************************************************
9 * Modification History:
11 * Date Name Description
12 * 02/18/97 aliu Converted from OpenClass. Added DONE.
13 * 01/13/2000 helena Added UErrorCode parameter to createXXXInstance methods.
14 *****************************************************************************************
17 // *****************************************************************************
18 // This file was generated from the java source file BreakIterator.java
19 // *****************************************************************************
21 #include "unicode/utypes.h"
23 #if !UCONFIG_NO_BREAK_ITERATION
25 #include "unicode/rbbi.h"
26 #include "unicode/brkiter.h"
27 #include "unicode/udata.h"
28 #include "unicode/ures.h"
29 #include "unicode/ustring.h"
39 // *****************************************************************************
40 // class BreakIterator
41 // This class implements methods for finding the location of boundaries in text.
42 // Instances of BreakIterator maintain a current position and scan over text
43 // returning the index of characters where boundaries occur.
44 // *****************************************************************************
48 // -------------------------------------
51 BreakIterator::buildInstance(const Locale
& loc
, const char *type
, int32_t kind
, UErrorCode
&status
)
55 char actualLocale
[ULOC_FULLNAME_CAPACITY
];
57 const UChar
* brkfname
= NULL
;
58 UResourceBundle brkRulesStack
;
59 UResourceBundle brkNameStack
;
60 UResourceBundle
*brkRules
= &brkRulesStack
;
61 UResourceBundle
*brkName
= &brkNameStack
;
62 RuleBasedBreakIterator
*result
= NULL
;
64 if (U_FAILURE(status
))
67 ures_initStackObject(brkRules
);
68 ures_initStackObject(brkName
);
71 UResourceBundle
*b
= ures_open(U_ICUDATA_BRKITR
, loc
.getName(), &status
);
72 /* this is a hack for now. Should be fixed when the data is fetched from
74 if(status
==U_USING_DEFAULT_WARNING
){
76 ures_openFillIn(b
, U_ICUDATA_BRKITR
, "", &status
);
79 // Get the "boundaries" array.
80 if (U_SUCCESS(status
)) {
81 brkRules
= ures_getByKeyWithFallback(b
, "boundaries", brkRules
, &status
);
82 // Get the string object naming the rules file
83 brkName
= ures_getByKeyWithFallback(brkRules
, type
, brkName
, &status
);
84 // Get the actual string
85 brkfname
= ures_getString(brkName
, &size
, &status
);
86 U_ASSERT((size_t)size
<sizeof(fnbuff
));
87 if ((size_t)size
>=sizeof(fnbuff
)) {
89 if (U_SUCCESS(status
)) {
90 status
= U_BUFFER_OVERFLOW_ERROR
;
94 // Use the string if we found it
95 if (U_SUCCESS(status
) && brkfname
) {
96 uprv_strncpy(actualLocale
,
97 ures_getLocaleInternal(brkName
, &status
),
98 sizeof(actualLocale
)/sizeof(actualLocale
[0]));
100 UChar
* extStart
=u_strchr(brkfname
, 0x002e);
103 len
= (int)(extStart
-brkfname
);
104 u_UCharsToChars(extStart
+1, ext
, sizeof(ext
)); // nul terminates the buff
105 u_UCharsToChars(brkfname
, fnbuff
, len
);
107 fnbuff
[len
]=0; // nul terminate
111 ures_close(brkRules
);
114 UDataMemory
* file
= udata_open(U_ICUDATA_BRKITR
, ext
, fnbuff
, &status
);
115 if (U_FAILURE(status
)) {
120 // Create a RuleBasedBreakIterator
121 result
= new RuleBasedBreakIterator(file
, status
);
123 // If there is a result, set the valid locale and actual locale, and the kind
124 if (U_SUCCESS(status
) && result
!= NULL
) {
125 U_LOCALE_BASED(locBased
, *(BreakIterator
*)result
);
126 locBased
.setLocaleIDs(ures_getLocaleByType(b
, ULOC_VALID_LOCALE
, &status
), actualLocale
);
127 result
->setBreakType(kind
);
132 if (U_FAILURE(status
) && result
!= NULL
) { // Sometimes redundant check, but simple
137 if (result
== NULL
) {
139 if (U_SUCCESS(status
)) {
140 status
= U_MEMORY_ALLOCATION_ERROR
;
147 // Creates a break iterator for word breaks.
148 BreakIterator
* U_EXPORT2
149 BreakIterator::createWordInstance(const Locale
& key
, UErrorCode
& status
)
151 return createInstance(key
, UBRK_WORD
, status
);
154 // -------------------------------------
156 // Creates a break iterator for line breaks.
157 BreakIterator
* U_EXPORT2
158 BreakIterator::createLineInstance(const Locale
& key
, UErrorCode
& status
)
160 return createInstance(key
, UBRK_LINE
, status
);
163 // -------------------------------------
165 // Creates a break iterator for character breaks.
166 BreakIterator
* U_EXPORT2
167 BreakIterator::createCharacterInstance(const Locale
& key
, UErrorCode
& status
)
169 return createInstance(key
, UBRK_CHARACTER
, status
);
172 // -------------------------------------
174 // Creates a break iterator for sentence breaks.
175 BreakIterator
* U_EXPORT2
176 BreakIterator::createSentenceInstance(const Locale
& key
, UErrorCode
& status
)
178 return createInstance(key
, UBRK_SENTENCE
, status
);
181 // -------------------------------------
183 // Creates a break iterator for title casing breaks.
184 BreakIterator
* U_EXPORT2
185 BreakIterator::createTitleInstance(const Locale
& key
, UErrorCode
& status
)
187 return createInstance(key
, UBRK_TITLE
, status
);
190 // -------------------------------------
192 // Gets all the available locales that has localized text boundary data.
193 const Locale
* U_EXPORT2
194 BreakIterator::getAvailableLocales(int32_t& count
)
196 return Locale::getAvailableLocales(count
);
199 // ------------------------------------------
201 // Default constructor and destructor
203 //-------------------------------------------
205 BreakIterator::BreakIterator()
207 fBufferClone
= FALSE
;
208 *validLocale
= *actualLocale
= 0;
211 BreakIterator::~BreakIterator()
215 // ------------------------------------------
219 //-------------------------------------------
220 #if !UCONFIG_NO_SERVICE
222 // -------------------------------------
224 class ICUBreakIteratorFactory
: public ICUResourceBundleFactory
{
226 virtual ~ICUBreakIteratorFactory();
228 virtual UObject
* handleCreate(const Locale
& loc
, int32_t kind
, const ICUService
* /*service*/, UErrorCode
& status
) const {
229 return BreakIterator::makeInstance(loc
, kind
, status
);
233 ICUBreakIteratorFactory::~ICUBreakIteratorFactory() {}
235 // -------------------------------------
237 class ICUBreakIteratorService
: public ICULocaleService
{
239 ICUBreakIteratorService()
240 : ICULocaleService(UNICODE_STRING("Break Iterator", 14))
242 UErrorCode status
= U_ZERO_ERROR
;
243 registerFactory(new ICUBreakIteratorFactory(), status
);
246 virtual ~ICUBreakIteratorService();
248 virtual UObject
* cloneInstance(UObject
* instance
) const {
249 return ((BreakIterator
*)instance
)->clone();
252 virtual UObject
* handleDefault(const ICUServiceKey
& key
, UnicodeString
* /*actualID*/, UErrorCode
& status
) const {
253 LocaleKey
& lkey
= (LocaleKey
&)key
;
254 int32_t kind
= lkey
.kind();
256 lkey
.currentLocale(loc
);
257 return BreakIterator::makeInstance(loc
, kind
, status
);
260 virtual UBool
isDefault() const {
261 return countFactories() == 1;
265 ICUBreakIteratorService::~ICUBreakIteratorService() {}
267 // -------------------------------------
271 // defined in ucln_cmn.h
273 static icu::ICULocaleService
* gService
= NULL
;
276 * Release all static memory held by breakiterator.
279 static UBool U_CALLCONV
breakiterator_cleanup(void) {
280 #if !UCONFIG_NO_SERVICE
291 static ICULocaleService
*
295 UMTX_CHECK(NULL
, (UBool
)(gService
== NULL
), needsInit
);
298 ICULocaleService
*tService
= new ICUBreakIteratorService();
300 if (gService
== NULL
) {
303 ucln_common_registerCleanup(UCLN_COMMON_BREAKITERATOR
, breakiterator_cleanup
);
311 // -------------------------------------
317 UMTX_CHECK(NULL
, gService
!= NULL
, retVal
);
321 // -------------------------------------
323 URegistryKey U_EXPORT2
324 BreakIterator::registerInstance(BreakIterator
* toAdopt
, const Locale
& locale
, UBreakIteratorType kind
, UErrorCode
& status
)
326 ICULocaleService
*service
= getService();
327 if (service
== NULL
) {
328 status
= U_MEMORY_ALLOCATION_ERROR
;
331 return service
->registerInstance(toAdopt
, locale
, kind
, status
);
334 // -------------------------------------
337 BreakIterator::unregister(URegistryKey key
, UErrorCode
& status
)
339 if (U_SUCCESS(status
)) {
341 return gService
->unregister(key
, status
);
343 status
= U_MEMORY_ALLOCATION_ERROR
;
348 // -------------------------------------
350 StringEnumeration
* U_EXPORT2
351 BreakIterator::getAvailableLocales(void)
353 ICULocaleService
*service
= getService();
354 if (service
== NULL
) {
357 return service
->getAvailableLocales();
359 #endif /* UCONFIG_NO_SERVICE */
361 // -------------------------------------
364 BreakIterator::createInstance(const Locale
& loc
, int32_t kind
, UErrorCode
& status
)
366 if (U_FAILURE(status
)) {
370 #if !UCONFIG_NO_SERVICE
372 Locale
actualLoc("");
373 BreakIterator
*result
= (BreakIterator
*)gService
->get(loc
, kind
, &actualLoc
, status
);
374 // TODO: The way the service code works in ICU 2.8 is that if
375 // there is a real registered break iterator, the actualLoc
376 // will be populated, but if the handleDefault path is taken
377 // (because nothing is registered that can handle the
378 // requested locale) then the actualLoc comes back empty. In
379 // that case, the returned object already has its actual/valid
380 // locale data populated (by makeInstance, which is what
381 // handleDefault calls), so we don't touch it. YES, A COMMENT
382 // THIS LONG is a sign of bad code -- so the action item is to
383 // revisit this in ICU 3.0 and clean it up/fix it/remove it.
384 if (U_SUCCESS(status
) && (result
!= NULL
) && *actualLoc
.getName() != 0) {
385 U_LOCALE_BASED(locBased
, *result
);
386 locBased
.setLocaleIDs(actualLoc
.getName(), actualLoc
.getName());
393 return makeInstance(loc
, kind
, status
);
397 // -------------------------------------
400 BreakIterator::makeInstance(const Locale
& loc
, int32_t kind
, UErrorCode
& status
)
403 if (U_FAILURE(status
)) {
407 BreakIterator
*result
= NULL
;
410 result
= BreakIterator::buildInstance(loc
, "grapheme", kind
, status
);
413 result
= BreakIterator::buildInstance(loc
, "word", kind
, status
);
416 result
= BreakIterator::buildInstance(loc
, "line", kind
, status
);
419 result
= BreakIterator::buildInstance(loc
, "sentence", kind
, status
);
422 result
= BreakIterator::buildInstance(loc
, "title", kind
, status
);
425 status
= U_ILLEGAL_ARGUMENT_ERROR
;
428 if (U_FAILURE(status
)) {
436 BreakIterator::getLocale(ULocDataLocaleType type
, UErrorCode
& status
) const {
437 U_LOCALE_BASED(locBased
, *this);
438 return locBased
.getLocale(type
, status
);
442 BreakIterator::getLocaleID(ULocDataLocaleType type
, UErrorCode
& status
) const {
443 U_LOCALE_BASED(locBased
, *this);
444 return locBased
.getLocaleID(type
, status
);
449 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */