Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/unicode/ulocbuilder.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // © 2023 and later: Unicode, Inc. and others.
0002 // License & terms of use: http://www.unicode.org/copyright.html
0003 #ifndef __ULOCBUILDER_H__
0004 #define __ULOCBUILDER_H__
0005 
0006 #include "unicode/localpointer.h"
0007 #include "unicode/ulocale.h"
0008 #include "unicode/utypes.h"
0009 
0010 /**
0011  * \file
0012  * \brief C API: Builder API for Locale
0013  */
0014 
0015 #ifndef U_HIDE_DRAFT_API
0016 
0017 /**
0018  * Opaque C service object type for the locale builder API
0019  * @draft ICU 74
0020  */
0021 struct ULocaleBuilder;
0022 
0023 /**
0024  * C typedef for struct ULocaleBuilder.
0025  * @draft ICU 74
0026  */
0027 typedef struct ULocaleBuilder ULocaleBuilder;
0028 
0029 /**
0030  * <code>ULocaleBuilder</code> is used to build valid <code>locale</code> id
0031  * string or IETF BCP 47 language tag from values configured by the setters.
0032  * The <code>ULocaleBuilder</code> checks if a value configured by a
0033  * setter satisfies the syntax requirements defined by the <code>Locale</code>
0034  * class.  A string of Locale created by a <code>ULocaleBuilder</code> is
0035  * well-formed and can be transformed to a well-formed IETF BCP 47 language tag
0036  * without losing information.
0037  *
0038  * <p>The following example shows how to create a <code>locale</code> string
0039  * with the <code>ULocaleBuilder</code>.
0040  * <blockquote>
0041  * <pre>
0042  *     UErrorCode err = U_ZERO_ERROR;
0043  *     char buffer[ULOC_FULLNAME_CAPACITY];
0044  *     ULocaleBuilder* builder = ulocbld_open();
0045  *     ulocbld_setLanguage(builder, "sr", -1);
0046  *     ulocbld_setScript(builder, "Latn", -1);
0047  *     ulocbld_setRegion(builder, "RS", -1);
0048  *     int32_t length = ulocbld_buildLocaleID(
0049  *         builder, buffer, ULOC_FULLNAME_CAPACITY, &error);
0050  *     ulocbld_close(builder);
0051  * </pre>
0052  * </blockquote>
0053  *
0054  * <p>ULocaleBuilders can be reused; <code>ulocbld_clear()</code> resets all
0055  * fields to their default values.
0056  *
0057  * <p>ULocaleBuilder tracks errors in an internal UErrorCode. For all setters,
0058  * except ulocbld_setLanguageTag and ulocbld_setLocale, ULocaleBuilder will return immediately
0059  * if the internal UErrorCode is in error state.
0060  * To reset internal state and error code, call clear method.
0061  * The ulocbld_setLanguageTag and setLocale method will first clear the internal
0062  * UErrorCode, then track the error of the validation of the input parameter
0063  * into the internal UErrorCode.
0064  *
0065  * @draft ICU 74
0066  */
0067 
0068 /**
0069  * Constructs an empty ULocaleBuilder. The default value of all
0070  * fields, extensions, and private use information is the
0071  * empty string. The created builder should be destroyed by calling
0072  * ulocbld_close();
0073  *
0074  * @draft ICU 74
0075  */
0076 U_CAPI ULocaleBuilder* U_EXPORT2
0077 ulocbld_open();
0078 
0079 /**
0080  * Close the builder and destroy it's internal states.
0081  * @param builder the builder
0082  * @draft ICU 74
0083  */
0084 U_CAPI void U_EXPORT2
0085 ulocbld_close(ULocaleBuilder* builder);
0086 
0087 /**
0088  * Resets the <code>ULocaleBuilder</code> to match the provided
0089  * <code>locale</code>.  Existing state is discarded.
0090  *
0091  * <p>All fields of the locale must be well-formed.
0092  * <p>This method clears the internal UErrorCode.
0093  *
0094  * @param builder the builder
0095  * @param locale the locale, a const char * pointer (need not be terminated when
0096  *               the length is non-negative)
0097  * @param length the length of the locale; if negative, then the locale need to be
0098  *               null terminated,
0099  *
0100  * @draft ICU 74
0101  */
0102 U_CAPI void U_EXPORT2
0103 ulocbld_setLocale(ULocaleBuilder* builder, const char* locale, int32_t length);
0104 
0105 /**
0106  * Resets the <code>ULocaleBuilder</code> to match the provided
0107  * <code>ULocale</code>. Existing state is discarded.
0108  *
0109  * <p>The locale must be not bogus.
0110  * <p>This method clears the internal UErrorCode.
0111  *
0112  * @param builder the builder.
0113  * @param locale the locale, a ULocale* pointer. The builder adopts the locale
0114  *               after the call and the client must not delete it.
0115  *
0116  * @draft ICU 74
0117  */
0118 U_CAPI void U_EXPORT2
0119 ulocbld_adoptULocale(ULocaleBuilder* builder, ULocale* locale);
0120 
0121 /**
0122  * Resets the ULocaleBuilder to match the provided IETF BCP 47 language tag.
0123  * Discards the existing state.
0124  * The empty string causes the builder to be reset, like {@link #ulocbld_clear}.
0125  * Legacy language tags (marked as “Type: grandfathered” in BCP 47)
0126  * are converted to their canonical form before being processed.
0127  * Otherwise, the <code>language tag</code> must be well-formed,
0128  * or else the ulocbld_buildLocaleID() and ulocbld_buildLanguageTag() methods
0129  * will later report an U_ILLEGAL_ARGUMENT_ERROR.
0130  *
0131  * <p>This method clears the internal UErrorCode.
0132  *
0133  * @param builder the builder
0134  * @param tag the language tag, defined as IETF BCP 47 language tag, a
0135  *               const char * pointer (need not be terminated when
0136  *               the length is non-negative)
0137  * @param length the length of the tag; if negative, then the tag need to be
0138  *               null terminated,
0139  * @draft ICU 74
0140  */
0141 U_CAPI void U_EXPORT2
0142 ulocbld_setLanguageTag(ULocaleBuilder* builder, const char* tag, int32_t length);
0143 
0144 /**
0145  * Sets the language.  If <code>language</code> is the empty string, the
0146  * language in this <code>ULocaleBuilder</code> is removed. Otherwise, the
0147  * <code>language</code> must be well-formed, or else the ulocbld_buildLocaleID()
0148  * and ulocbld_buildLanguageTag() methods will
0149  * later report an U_ILLEGAL_ARGUMENT_ERROR.
0150  *
0151  * <p>The syntax of language value is defined as
0152  * [unicode_language_subtag](http://www.unicode.org/reports/tr35/tr35.html#unicode_language_subtag).
0153  *
0154  * @param builder the builder
0155  * @param language the language, a const char * pointer (need not be terminated when
0156  *               the length is non-negative)
0157  * @param length the length of the language; if negative, then the language need to be
0158  *               null terminated,
0159  * @draft ICU 74
0160  */
0161 U_CAPI void U_EXPORT2
0162 ulocbld_setLanguage(ULocaleBuilder* builder, const char* language, int32_t length);
0163 
0164 /**
0165  * Sets the script. If <code>script</code> is the empty string, the script in
0166  * this <code>ULocaleBuilder</code> is removed.
0167  * Otherwise, the <code>script</code> must be well-formed, or else the
0168  * ulocbld_buildLocaleID() and ulocbld_buildLanguageTag() methods will later
0169  * report an U_ILLEGAL_ARGUMENT_ERROR.
0170  *
0171  * <p>The script value is a four-letter script code as
0172  * [unicode_script_subtag](http://www.unicode.org/reports/tr35/tr35.html#unicode_script_subtag)
0173  * defined by ISO 15924
0174  *
0175  * @param builder the builder
0176  * @param script the script, a const char * pointer (need not be terminated when
0177  *               the length is non-negative)
0178  * @param length the length of the script; if negative, then the script need to be
0179  *               null terminated,
0180  * @draft ICU 74
0181  */
0182 U_CAPI void U_EXPORT2
0183 ulocbld_setScript(ULocaleBuilder* builder, const char* script, int32_t length);
0184 
0185 /**
0186  * Sets the region.  If region is the empty string, the region in this
0187  * <code>ULocaleBuilder</code> is removed. Otherwise, the <code>region</code>
0188  * must be well-formed, or else the ulocbld_buildLocaleID() and
0189  * ulocbld_buildLanguageTag() methods will later report an
0190  * U_ILLEGAL_ARGUMENT_ERROR.
0191  *
0192  * <p>The region value is defined by
0193  *  [unicode_region_subtag](http://www.unicode.org/reports/tr35/tr35.html#unicode_region_subtag)
0194  * as a two-letter ISO 3166 code or a three-digit UN M.49 area code.
0195  *
0196  * <p>The region value in the <code>Locale</code> created by the
0197  * <code>ULocaleBuilder</code> is always normalized to upper case.
0198  *
0199  * @param builder the builder
0200  * @param region the region, a const char * pointer (need not be terminated when
0201  *               the length is non-negative)
0202  * @param length the length of the region; if negative, then the region need to be
0203  *               null terminated,
0204  * @draft ICU 74
0205  */
0206 U_CAPI void U_EXPORT2
0207 ulocbld_setRegion(ULocaleBuilder* builder, const char* region, int32_t length);
0208 
0209 /**
0210  * Sets the variant.  If variant is the empty string, the variant in this
0211  * <code>ULocaleBuilder</code> is removed.  Otherwise, the <code>variant</code>
0212  * must be well-formed, or else the ulocbld_buildLocaleID() and
0213  * ulocbld_buildLanguageTag() methods will later report an
0214  * U_ILLEGAL_ARGUMENT_ERROR.
0215  *
0216  * <p><b>Note:</b> This method checks if <code>variant</code>
0217  * satisfies the
0218  * [unicode_variant_subtag](http://www.unicode.org/reports/tr35/tr35.html#unicode_variant_subtag)
0219  * syntax requirements, and normalizes the value to lowercase letters. However,
0220  * the <code>Locale</code> class does not impose any syntactic
0221  * restriction on variant. To set an ill-formed variant, use a Locale constructor.
0222  * If there are multiple unicode_variant_subtag, the caller must concatenate
0223  * them with '-' as separator (ex: "foobar-fibar").
0224  *
0225  * @param builder the builder
0226  * @param variant the variant, a const char * pointer (need not be terminated when
0227  *               the length is non-negative)
0228  * @param length the length of the variant; if negative, then the variant need to be
0229  *               null terminated,
0230  * @draft ICU 74
0231  */
0232 U_CAPI void U_EXPORT2
0233 ulocbld_setVariant(ULocaleBuilder* builder, const char* variant, int32_t length);
0234 
0235 /**
0236  * Sets the extension for the given key. If the value is the empty string,
0237  * the extension is removed.  Otherwise, the <code>key</code> and
0238  * <code>value</code> must be well-formed, or else the ulocbld_buildLocaleID()
0239  * and ulocbld_buildLanguageTag() methods will
0240  * later report an U_ILLEGAL_ARGUMENT_ERROR.
0241  *
0242  * <p><b>Note:</b> The key ('u') is used for the Unicode locale extension.
0243  * Setting a value for this key replaces any existing Unicode locale key/type
0244  * pairs with those defined in the extension.
0245  *
0246  * <p><b>Note:</b> The key ('x') is used for the private use code. To be
0247  * well-formed, the value for this key needs only to have subtags of one to
0248  * eight alphanumeric characters, not two to eight as in the general case.
0249  *
0250  * @param builder the builder
0251  * @param key the extension key
0252  * @param value the value, a const char * pointer (need not be terminated when
0253  *               the length is non-negative)
0254  * @param length the length of the value; if negative, then the value need to be
0255  *               null terminated,
0256  * @draft ICU 74
0257  */
0258 U_CAPI void U_EXPORT2
0259 ulocbld_setExtension(ULocaleBuilder* builder, char key, const char* value, int32_t length);
0260 
0261 /**
0262  * Sets the Unicode locale keyword type for the given key. If the type
0263  * StringPiece is constructed with a nullptr, the keyword is removed.
0264  * If the type is the empty string, the keyword is set without type subtags.
0265  * Otherwise, the key and type must be well-formed, or else the
0266  * ulocbld_buildLocaleID() and ulocbld_buildLanguageTag() methods will later
0267  * report an U_ILLEGAL_ARGUMENT_ERROR.
0268  *
0269  * <p>Keys and types are converted to lower case.
0270  *
0271  * <p><b>Note</b>:Setting the 'u' extension via {@link #ulocbld_setExtension}
0272  * replaces all Unicode locale keywords with those defined in the
0273  * extension.
0274  *
0275  * @param builder the builder
0276  * @param key the Unicode locale key, a const char * pointer (need not be
0277  *               terminated when the length is non-negative)
0278  * @param keyLength the length of the key; if negative, then the key need to be
0279  *               null terminated,
0280  * @param type the Unicode locale type, a const char * pointer (need not be
0281  *               terminated when the length is non-negative)
0282  * @param typeLength the length of the type; if negative, then the type need to
0283  *               be null terminated,
0284  * @return This builder.
0285  * @draft ICU 74
0286  */
0287 U_CAPI void U_EXPORT2
0288 ulocbld_setUnicodeLocaleKeyword(ULocaleBuilder* builder,
0289         const char* key, int32_t keyLength, const char* type, int32_t typeLength);
0290 
0291 /**
0292  * Adds a unicode locale attribute, if not already present, otherwise
0293  * has no effect.  The attribute must not be empty string and must be
0294  * well-formed or U_ILLEGAL_ARGUMENT_ERROR will be set to status
0295  * during the ulocbld_buildLocaleID() and ulocbld_buildLanguageTag() calls.
0296  *
0297  * @param builder the builder
0298  * @param attribute the attribute, a const char * pointer (need not be
0299  *               terminated when the length is non-negative)
0300  * @param length the length of the attribute; if negative, then the attribute
0301  *               need to be null terminated,
0302  * @draft ICU 74
0303  */
0304 U_CAPI void U_EXPORT2
0305 ulocbld_addUnicodeLocaleAttribute(
0306     ULocaleBuilder* builder, const char* attribute, int32_t length);
0307 
0308 /**
0309  * Removes a unicode locale attribute, if present, otherwise has no
0310  * effect.  The attribute must not be empty string and must be well-formed
0311  * or U_ILLEGAL_ARGUMENT_ERROR will be set to status during the ulocbld_buildLocaleID()
0312  * and ulocbld_buildLanguageTag() calls.
0313  *
0314  * <p>Attribute comparison for removal is case-insensitive.
0315  *
0316  * @param builder the builder
0317  * @param attribute the attribute, a const char * pointer (need not be
0318  *               terminated when the length is non-negative)
0319  * @param length the length of the attribute; if negative, then the attribute
0320  *               need to be null terminated,
0321  * @draft ICU 74
0322  */
0323 U_CAPI void U_EXPORT2
0324 ulocbld_removeUnicodeLocaleAttribute(
0325     ULocaleBuilder* builder, const char* attribute, int32_t length);
0326 
0327 /**
0328  * Resets the builder to its initial, empty state.
0329  * <p>This method clears the internal UErrorCode.
0330  *
0331  * @param builder the builder
0332  * @draft ICU 74
0333  */
0334 U_CAPI void U_EXPORT2
0335 ulocbld_clear(ULocaleBuilder* builder);
0336 
0337 /**
0338  * Resets the extensions to their initial, empty state.
0339  * Language, script, region and variant are unchanged.
0340  *
0341  * @param builder the builder
0342  * @draft ICU 74
0343  */
0344 U_CAPI void U_EXPORT2
0345 ulocbld_clearExtensions(ULocaleBuilder* builder);
0346 
0347 /**
0348  * Build the LocaleID string from the fields set on this builder.
0349  * If any set methods or during the ulocbld_buildLocaleID() call require memory
0350  * allocation but fail U_MEMORY_ALLOCATION_ERROR will be set to status.
0351  * If any of the fields set by the setters are not well-formed, the status
0352  * will be set to U_ILLEGAL_ARGUMENT_ERROR. The state of the builder will
0353  * not change after the ulocbld_buildLocaleID() call and the caller is
0354  * free to keep using the same builder to build more locales.
0355  *
0356  * @param builder the builder
0357  * @param locale the locale id
0358  * @param localeCapacity the size of the locale buffer to store the locale id
0359  * @param err the error code
0360  * @return the length of the locale id in buffer
0361  * @draft ICU 74
0362  */
0363 U_CAPI int32_t U_EXPORT2
0364 ulocbld_buildLocaleID(ULocaleBuilder* builder, char* locale,
0365                       int32_t localeCapacity, UErrorCode* err);
0366 
0367 /**
0368  * Build the ULocale object from the fields set on this builder.
0369  * If any set methods or during the ulocbld_buildULocale() call require memory
0370  * allocation but fail U_MEMORY_ALLOCATION_ERROR will be set to status.
0371  * If any of the fields set by the setters are not well-formed, the status
0372  * will be set to U_ILLEGAL_ARGUMENT_ERROR. The state of the builder will
0373  * not change after the ulocbld_buildULocale() call and the caller is
0374  * free to keep using the same builder to build more locales.
0375  *
0376  * @param builder the builder.
0377  * @param err the error code.
0378  * @return the locale, a ULocale* pointer. The created ULocale must be
0379  *          destroyed by calling {@link ulocale_close}.
0380  * @draft ICU 74
0381  */
0382 U_CAPI ULocale* U_EXPORT2
0383 ulocbld_buildULocale(ULocaleBuilder* builder, UErrorCode* err);
0384 
0385 /**
0386  * Build the IETF BCP 47 language tag string from the fields set on this builder.
0387  * If any set methods or during the ulocbld_buildLanguageTag() call require memory
0388  * allocation but fail U_MEMORY_ALLOCATION_ERROR will be set to status.
0389  * If any of the fields set by the setters are not well-formed, the status
0390  * will be set to U_ILLEGAL_ARGUMENT_ERROR. The state of the builder will
0391  * not change after the ulocbld_buildLanguageTag() call and the caller is free
0392  * to keep using the same builder to build more locales.
0393  *
0394  * @param builder the builder
0395  * @param language the language tag
0396  * @param languageCapacity the size of the language buffer to store the language
0397  * tag
0398  * @param err the error code
0399  * @return the length of the language tag in buffer
0400  * @draft ICU 74
0401  */
0402 U_CAPI int32_t U_EXPORT2
0403 ulocbld_buildLanguageTag(ULocaleBuilder* builder, char* language,
0404                       int32_t languageCapacity, UErrorCode* err);
0405 
0406 /**
0407  * Sets the UErrorCode if an error occurred while recording sets.
0408  * Preserves older error codes in the outErrorCode.
0409  *
0410  * @param builder the builder
0411  * @param outErrorCode Set to an error code that occurred while setting subtags.
0412  *                  Unchanged if there is no such error or if outErrorCode
0413  *                  already contained an error.
0414  * @return true if U_FAILURE(*outErrorCode)
0415  * @draft ICU 74
0416  */
0417 U_CAPI UBool U_EXPORT2
0418 ulocbld_copyErrorTo(const ULocaleBuilder* builder, UErrorCode *outErrorCode);
0419 
0420 #if U_SHOW_CPLUSPLUS_API
0421 
0422 U_NAMESPACE_BEGIN
0423 
0424 /**
0425  * \class LocalULocaleBuilderPointer
0426  * "Smart pointer" class, closes a ULocaleBuilder via ulocbld_close().
0427  * For most methods see the LocalPointerBase base class.
0428  *
0429  * @see LocalPointerBase
0430  * @see LocalPointer
0431  * @draft ICU 74
0432  */
0433 U_DEFINE_LOCAL_OPEN_POINTER(LocalULocaleBuilderPointer, ULocaleBuilder, ulocbld_close);
0434 
0435 U_NAMESPACE_END
0436 
0437 #endif  /* U_SHOW_CPLUSPLUS_API */
0438 
0439 #endif  /* U_HIDE_DRAFT_API */
0440 
0441 #endif  // __ULOCBUILDER_H__