Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // © 2016 and later: Unicode, Inc. and others.
0002 // License & terms of use: http://www.unicode.org/copyright.html
0003 /*
0004 *****************************************************************************************
0005 * Copyright (C) 2015-2016, International Business Machines
0006 * Corporation and others. All Rights Reserved.
0007 *****************************************************************************************
0008 */
0009 
0010 #ifndef ULISTFORMATTER_H
0011 #define ULISTFORMATTER_H
0012 
0013 #include "unicode/utypes.h"
0014 
0015 #if !UCONFIG_NO_FORMATTING
0016 
0017 #include "unicode/uformattedvalue.h"
0018 
0019 #if U_SHOW_CPLUSPLUS_API
0020 #include "unicode/localpointer.h"
0021 #endif   // U_SHOW_CPLUSPLUS_API
0022 
0023 /**
0024  * \file
0025  * \brief C API: Format a list in a locale-appropriate way.
0026  *
0027  * A UListFormatter is used to format a list of items in a locale-appropriate way, 
0028  * using data from CLDR.
0029  * Example: Input data ["Alice", "Bob", "Charlie", "Delta"] will be formatted
0030  * as "Alice, Bob, Charlie, and Delta" in English.
0031  */
0032 
0033 /**
0034  * Opaque UListFormatter object for use in C
0035  * @stable ICU 55
0036  */
0037 struct UListFormatter;
0038 typedef struct UListFormatter UListFormatter;  /**< C typedef for struct UListFormatter. @stable ICU 55 */
0039 
0040 struct UFormattedList;
0041 /**
0042  * Opaque struct to contain the results of a UListFormatter operation.
0043  * @stable ICU 64
0044  */
0045 typedef struct UFormattedList UFormattedList;
0046 
0047 /**
0048  * FieldPosition and UFieldPosition selectors for format fields
0049  * defined by ListFormatter.
0050  * @stable ICU 63
0051  */
0052 typedef enum UListFormatterField {
0053     /**
0054      * The literal text in the result which came from the resources.
0055      * @stable ICU 63
0056      */
0057     ULISTFMT_LITERAL_FIELD,
0058     /**
0059      * The element text in the result which came from the input strings.
0060      * @stable ICU 63
0061      */
0062     ULISTFMT_ELEMENT_FIELD
0063 } UListFormatterField;
0064 
0065 /**
0066  * Type of meaning expressed by the list.
0067  *
0068  * @stable ICU 67
0069  */
0070 typedef enum UListFormatterType {
0071     /**
0072      * Conjunction formatting, e.g. "Alice, Bob, Charlie, and Delta".
0073      *
0074      * @stable ICU 67
0075      */
0076     ULISTFMT_TYPE_AND,
0077 
0078     /**
0079      * Disjunction (or alternative, or simply one of) formatting, e.g.
0080      * "Alice, Bob, Charlie, or Delta".
0081      *
0082      * @stable ICU 67
0083      */
0084     ULISTFMT_TYPE_OR,
0085 
0086     /**
0087      * Formatting of a list of values with units, e.g. "5 pounds, 12 ounces".
0088      *
0089      * @stable ICU 67
0090      */
0091     ULISTFMT_TYPE_UNITS
0092 } UListFormatterType;
0093 
0094 /**
0095  * Verbosity level of the list patterns.
0096  *
0097  * @stable ICU 67
0098  */
0099 typedef enum UListFormatterWidth {
0100     /**
0101      * Use list formatting with full words (no abbreviations) when possible.
0102      *
0103      * @stable ICU 67
0104      */
0105     ULISTFMT_WIDTH_WIDE,
0106 
0107     /**
0108      * Use list formatting of typical length.
0109      * @stable ICU 67
0110      */
0111     ULISTFMT_WIDTH_SHORT,
0112 
0113     /**
0114      * Use list formatting of the shortest possible length.
0115      * @stable ICU 67
0116      */
0117     ULISTFMT_WIDTH_NARROW,
0118 } UListFormatterWidth;
0119 
0120 /**
0121  * Open a new UListFormatter object using the rules for a given locale.
0122  * The object will be initialized with AND type and WIDE width.
0123  *
0124  * @param locale
0125  *            The locale whose rules should be used; may be NULL for
0126  *            default locale.
0127  * @param status
0128  *            A pointer to a standard ICU UErrorCode (input/output parameter).
0129  *            Its input value must pass the U_SUCCESS() test, or else the
0130  *            function returns immediately. The caller should check its output
0131  *            value with U_FAILURE(), or use with function chaining (see User
0132  *            Guide for details).
0133  * @return
0134  *            A pointer to a UListFormatter object for the specified locale,
0135  *            or NULL if an error occurred.
0136  * @stable ICU 55
0137  */
0138 U_CAPI UListFormatter* U_EXPORT2
0139 ulistfmt_open(const char*  locale,
0140               UErrorCode*  status);
0141 
0142 /**
0143  * Open a new UListFormatter object appropriate for the given locale, list type,
0144  * and style.
0145  *
0146  * @param locale
0147  *            The locale whose rules should be used; may be NULL for
0148  *            default locale.
0149  * @param type
0150  *            The type of list formatting to use.
0151  * @param width
0152  *            The width of formatting to use.
0153  * @param status
0154  *            A pointer to a standard ICU UErrorCode (input/output parameter).
0155  *            Its input value must pass the U_SUCCESS() test, or else the
0156  *            function returns immediately. The caller should check its output
0157  *            value with U_FAILURE(), or use with function chaining (see User
0158  *            Guide for details).
0159  * @return
0160  *            A pointer to a UListFormatter object for the specified locale,
0161  *            or NULL if an error occurred.
0162  * @stable ICU 67
0163  */
0164 U_CAPI UListFormatter* U_EXPORT2
0165 ulistfmt_openForType(const char*  locale, UListFormatterType type,
0166                      UListFormatterWidth width, UErrorCode*  status);
0167 
0168 /**
0169  * Close a UListFormatter object. Once closed it may no longer be used.
0170  * @param listfmt
0171  *            The UListFormatter object to close.
0172  * @stable ICU 55
0173  */
0174 U_CAPI void U_EXPORT2
0175 ulistfmt_close(UListFormatter *listfmt);
0176 
0177 /**
0178  * Creates an object to hold the result of a UListFormatter
0179  * operation. The object can be used repeatedly; it is cleared whenever
0180  * passed to a format function.
0181  *
0182  * @param ec Set if an error occurs.
0183  * @return A pointer needing ownership.
0184  * @stable ICU 64
0185  */
0186 U_CAPI UFormattedList* U_EXPORT2
0187 ulistfmt_openResult(UErrorCode* ec);
0188 
0189 /**
0190  * Returns a representation of a UFormattedList as a UFormattedValue,
0191  * which can be subsequently passed to any API requiring that type.
0192  *
0193  * The returned object is owned by the UFormattedList and is valid
0194  * only as long as the UFormattedList is present and unchanged in memory.
0195  *
0196  * You can think of this method as a cast between types.
0197  *
0198  * When calling ufmtval_nextPosition():
0199  * The fields are returned from start to end. The special field category
0200  * UFIELD_CATEGORY_LIST_SPAN is used to indicate which argument
0201  * was inserted at the given position. The span category will
0202  * always occur before the corresponding instance of UFIELD_CATEGORY_LIST
0203  * in the ufmtval_nextPosition() iterator.
0204  *
0205  * @param uresult The object containing the formatted string.
0206  * @param ec Set if an error occurs.
0207  * @return A UFormattedValue owned by the input object.
0208  * @stable ICU 64
0209  */
0210 U_CAPI const UFormattedValue* U_EXPORT2
0211 ulistfmt_resultAsValue(const UFormattedList* uresult, UErrorCode* ec);
0212 
0213 /**
0214  * Releases the UFormattedList created by ulistfmt_openResult().
0215  *
0216  * @param uresult The object to release.
0217  * @stable ICU 64
0218  */
0219 U_CAPI void U_EXPORT2
0220 ulistfmt_closeResult(UFormattedList* uresult);
0221 
0222 
0223 #if U_SHOW_CPLUSPLUS_API
0224 
0225 U_NAMESPACE_BEGIN
0226 
0227 /**
0228  * \class LocalUListFormatterPointer
0229  * "Smart pointer" class, closes a UListFormatter via ulistfmt_close().
0230  * For most methods see the LocalPointerBase base class.
0231  *
0232  * @see LocalPointerBase
0233  * @see LocalPointer
0234  * @stable ICU 55
0235  */
0236 U_DEFINE_LOCAL_OPEN_POINTER(LocalUListFormatterPointer, UListFormatter, ulistfmt_close);
0237 
0238 /**
0239  * \class LocalUFormattedListPointer
0240  * "Smart pointer" class, closes a UFormattedList via ulistfmt_closeResult().
0241  * For most methods see the LocalPointerBase base class.
0242  *
0243  * @see LocalPointerBase
0244  * @see LocalPointer
0245  * @stable ICU 64
0246  */
0247 U_DEFINE_LOCAL_OPEN_POINTER(LocalUFormattedListPointer, UFormattedList, ulistfmt_closeResult);
0248 
0249 U_NAMESPACE_END
0250 
0251 #endif
0252 
0253 /**
0254  * Formats a list of strings using the conventions established for the
0255  * UListFormatter object.
0256  * @param listfmt
0257  *            The UListFormatter object specifying the list conventions.
0258  * @param strings
0259  *            An array of pointers to UChar strings; the array length is
0260  *            specified by stringCount. Must be non-NULL if stringCount > 0.
0261  * @param stringLengths
0262  *            An array of string lengths corresponding to the strings[]
0263  *            parameter; any individual length value may be negative to indicate
0264  *            that the corresponding strings[] entry is 0-terminated, or
0265  *            stringLengths itself may be NULL if all of the strings are
0266  *            0-terminated. If non-NULL, the stringLengths array must have
0267  *            stringCount entries.
0268  * @param stringCount
0269  *            the number of entries in strings[], and the number of entries
0270  *            in the stringLengths array if it is not NULL. Must be >= 0.
0271  * @param result
0272  *            A pointer to a buffer to receive the formatted list.
0273  * @param resultCapacity
0274  *            The maximum size of result.
0275  * @param status
0276  *            A pointer to a standard ICU UErrorCode (input/output parameter).
0277  *            Its input value must pass the U_SUCCESS() test, or else the
0278  *            function returns immediately. The caller should check its output
0279  *            value with U_FAILURE(), or use with function chaining (see User
0280  *            Guide for details).
0281  * @return
0282  *            The total buffer size needed; if greater than resultLength, the
0283  *            output was truncated. May be <=0 if unable to determine the
0284  *            total buffer size needed (e.g. for illegal arguments).
0285  * @stable ICU 55
0286  */
0287 U_CAPI int32_t U_EXPORT2
0288 ulistfmt_format(const UListFormatter* listfmt,
0289                 const UChar* const strings[],
0290                 const int32_t *    stringLengths,
0291                 int32_t            stringCount,
0292                 UChar*             result,
0293                 int32_t            resultCapacity,
0294                 UErrorCode*        status);
0295 
0296 /**
0297  * Formats a list of strings to a UFormattedList, which exposes more
0298  * information than the string exported by ulistfmt_format().
0299  *
0300  * @param listfmt
0301  *            The UListFormatter object specifying the list conventions.
0302  * @param strings
0303  *            An array of pointers to UChar strings; the array length is
0304  *            specified by stringCount. Must be non-NULL if stringCount > 0.
0305  * @param stringLengths
0306  *            An array of string lengths corresponding to the strings[]
0307  *            parameter; any individual length value may be negative to indicate
0308  *            that the corresponding strings[] entry is 0-terminated, or
0309  *            stringLengths itself may be NULL if all of the strings are
0310  *            0-terminated. If non-NULL, the stringLengths array must have
0311  *            stringCount entries.
0312  * @param stringCount
0313  *            the number of entries in strings[], and the number of entries
0314  *            in the stringLengths array if it is not NULL. Must be >= 0.
0315  * @param uresult
0316  *            The object in which to store the result of the list formatting
0317  *            operation. See ulistfmt_openResult().
0318  * @param status
0319  *            Error code set if an error occurred during formatting.
0320  * @stable ICU 64
0321  */
0322 U_CAPI void U_EXPORT2
0323 ulistfmt_formatStringsToResult(
0324                 const UListFormatter* listfmt,
0325                 const UChar* const strings[],
0326                 const int32_t *    stringLengths,
0327                 int32_t            stringCount,
0328                 UFormattedList*    uresult,
0329                 UErrorCode*        status);
0330 
0331 #endif /* #if !UCONFIG_NO_FORMATTING */
0332 
0333 #endif