]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/common/uresimp.h
ICU-66108.tar.gz
[apple/icu.git] / icuSources / common / uresimp.h
index 9c5fe665136fce342c41c3dfcdd84c58a39b4272..13097df46c73b649ab4fc335635d8a3f1c54acb3 100644 (file)
@@ -1,6 +1,8 @@
+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
 /*
 **********************************************************************
-*   Copyright (C) 2000-2004, International Business Machines
+*   Copyright (C) 2000-2016, International Business Machines
 *   Corporation and others.  All Rights Reserved.
 **********************************************************************
 */
 #define URESIMP_H
 
 #include "unicode/ures.h"
+#include "unicode/utypes.h"
 
 #include "uresdata.h"
 
 #define kRootLocaleName         "root"
+#define kPoolBundleName         "pool"
 
 /*
  The default minor version and the version separator must be exactly one
 #define MAGIC2 19641227
 
 #define URES_MAX_ALIAS_LEVEL 256
+#define URES_MAX_BUFFER_SIZE 256
 
-/*
-enum UResEntryType {
-    ENTRY_OK = 0,
-    ENTRY_GOTO_ROOT = 1,
-    ENTRY_GOTO_DEFAULT = 2,
-    ENTRY_INVALID = 3
-};
-
-typedef enum UResEntryType UResEntryType;
-*/
+#define EMPTY_SET 0x2205
 
 struct UResourceDataEntry;
 typedef struct UResourceDataEntry UResourceDataEntry;
 
+/*
+ * Note: If we wanted to make this structure smaller, then we could try
+ * to use one UResourceDataEntry pointer for fAlias and fPool, with a separate
+ * flag to distinguish whether this struct is for a real bundle with a pool,
+ * or for an alias entry for which we won't use the pool after loading.
+ */
 struct UResourceDataEntry {
     char *fName; /* name of the locale for bundle - still to decide whether it is original or fallback */
     char *fPath; /* path to bundle - used for distinguishing between resources with the same name */
-    uint32_t fCountExisting; /* how much is this resource used */
-    ResourceData fData; /* data for low level access */
     UResourceDataEntry *fParent; /*next resource in fallback chain*/
-/*    UResEntryType fStatus;*/
+    UResourceDataEntry *fAlias;
+    UResourceDataEntry *fPool;
+    ResourceData fData; /* data for low level access */
+    char fNameBuffer[3]; /* A small buffer of free space for fName. The free space is due to struct padding. */
+    uint32_t fCountExisting; /* how much is this resource used */
     UErrorCode fBogus;
-    int32_t fHashKey; /* for faster access in the hashtable */
+    /* int32_t fHashKey;*/ /* for faster access in the hashtable */
 };
 
 #define RES_BUFSIZE 64
@@ -61,31 +65,105 @@ struct UResourceBundle {
     const char *fKey; /*tag*/
     UResourceDataEntry *fData; /*for low-level access*/
     char *fVersion;
+    UResourceDataEntry *fTopLevelData; /* for getting the valid locale */
     char *fResPath; /* full path to the resource: "zh_TW/CollationElements/Sequence" */
+    // TODO(ICU-20769): Try to change the by-value fResData into a pointer,
+    // with the struct in only one place for each bundle.
+    // Also replace class ResourceDataValue.resData with a pResData pointer again.
+    ResourceData fResData;
     char fResBuf[RES_BUFSIZE];
     int32_t fResPathLen;
+    Resource fRes;
     UBool fHasFallback;
     UBool fIsTopLevel;
     uint32_t fMagic1;   /* For determining if it's a stack object */
     uint32_t fMagic2;   /* For determining if it's a stack object */
     int32_t fIndex;
     int32_t fSize;
-    ResourceData fResData;
-    Resource fRes;
-
-    UResourceDataEntry *fTopLevelData; /* for getting the valid locale */
-    const UResourceBundle *fParentRes; /* needed to get the actual locale for a child resource */
 
+    /*const UResourceBundle *fParentRes;*/ /* needed to get the actual locale for a child resource */
 };
 
 U_CAPI void U_EXPORT2 ures_initStackObject(UResourceBundle* resB);
 
+#ifdef __cplusplus
+
+U_NAMESPACE_BEGIN
+
+/**
+ * \class StackUResourceBundle
+ * "Smart pointer" like class, closes a UResourceBundle via ures_close().
+ *
+ * This code:
+ *
+ *   StackUResourceBundle bundle;
+ *   foo(bundle.getAlias());
+ *
+ * Is equivalent to this code:
+ *
+ *   UResourceBundle bundle;
+ *   ures_initStackObject(&bundle);
+ *   foo(&bundle);
+ *   ures_close(&bundle);
+ *
+ * @see LocalUResourceBundlePointer
+ * @internal
+ */
+class U_COMMON_API StackUResourceBundle {
+public:
+    // No heap allocation. Use only on the stack.
+    static void* U_EXPORT2 operator new(size_t) U_NOEXCEPT = delete;
+    static void* U_EXPORT2 operator new[](size_t) U_NOEXCEPT = delete;
+#if U_HAVE_PLACEMENT_NEW
+    static void* U_EXPORT2 operator new(size_t, void*) U_NOEXCEPT = delete;
+#endif
+
+    StackUResourceBundle();
+    ~StackUResourceBundle();
+
+    UResourceBundle* getAlias() { return &bundle; }
+
+    UResourceBundle& ref() { return bundle; }
+    const UResourceBundle& ref() const { return bundle; }
+
+    StackUResourceBundle(const StackUResourceBundle&) = delete;
+    StackUResourceBundle& operator=(const StackUResourceBundle&) = delete;
+
+    StackUResourceBundle(StackUResourceBundle&&) = delete;
+    StackUResourceBundle& operator=(StackUResourceBundle&&) = delete;
+
+private:
+    UResourceBundle bundle;
+};
+
+U_NAMESPACE_END
+
+#endif  /* __cplusplus */
+
+/**
+ * Opens a resource bundle for the locale;
+ * if there is not even a base language bundle, then loads the root bundle;
+ * never falls back to the default locale.
+ *
+ * This is used for algorithms that have good pan-Unicode default behavior,
+ * such as case mappings, collation, and segmentation (BreakIterator).
+ */
+U_CAPI UResourceBundle* U_EXPORT2
+ures_openNoDefault(const char* path, const char* localeID, UErrorCode* status);
+
 /* Some getters used by the copy constructor */
 U_CFUNC const char* ures_getName(const UResourceBundle* resB);
+#ifdef URES_DEBUG
 U_CFUNC const char* ures_getPath(const UResourceBundle* resB);
-U_CFUNC void ures_appendResPath(UResourceBundle *resB, const char* toAdd, int32_t lenToAdd);
+/**
+ * If anything was in the RB cache, dump it to the screen.
+ * @return TRUE if there was anything into the cache
+ */
+U_CAPI UBool U_EXPORT2 ures_dumpCacheContents(void);
+#endif
+/*U_CFUNC void ures_appendResPath(UResourceBundle *resB, const char* toAdd, int32_t lenToAdd);*/
 /*U_CFUNC void ures_setResPath(UResourceBundle *resB, const char* toAdd);*/
-U_CFUNC void ures_freeResPath(UResourceBundle *resB);
+/*U_CFUNC void ures_freeResPath(UResourceBundle *resB);*/
 
 /* Candidates for export */
 U_CFUNC UResourceBundle *ures_copyResb(UResourceBundle *r, const UResourceBundle *original, UErrorCode *status);
@@ -104,7 +182,6 @@ U_CFUNC UResourceBundle *ures_copyResb(UResourceBundle *r, const UResourceBundle
  *                          Alternatively, you can supply a struct to be filled by this function.
  * @param status            fills in the outgoing error code.
  * @return                  a pointer to a UResourceBundle struct. If fill in param was NULL, caller must delete it
- * @draft ICU 2.2
  */
 U_CAPI UResourceBundle* U_EXPORT2
 ures_findResource(const char* pathToResource, 
@@ -124,7 +201,6 @@ ures_findResource(const char* pathToResource,
  *                          Alternatively, you can supply a struct to be filled by this function.
  * @param status            fills in the outgoing error code.
  * @return                  a pointer to a UResourceBundle struct. If fill in param was NULL, caller must delete it
- * @draft ICU 2.2
  */
 U_CAPI UResourceBundle* U_EXPORT2
 ures_findSubResource(const UResourceBundle *resB, 
@@ -146,9 +222,8 @@ ures_findSubResource(const UResourceBundle *resB,
  * @param status error code
  * @return  the actual buffer size needed for the full locale.  If it's greater 
  * than resultCapacity, the returned full name will be truncated and an error code will be returned.
- * @internal ICU 3.0
  */
-U_INTERNAL int32_t U_EXPORT2
+U_CAPI int32_t U_EXPORT2
 ures_getFunctionalEquivalent(char *result, int32_t resultCapacity, 
                              const char *path, const char *resName, const char *keyword, const char *locid,
                              UBool *isAvailable, UBool omitDefault, UErrorCode *status);
@@ -159,9 +234,182 @@ ures_getFunctionalEquivalent(char *result, int32_t resultCapacity,
  * @param keyword a particular keyword to consider, must match a top level resource name 
  * within the tree.
  * @param status error code
- * @internal ICU 3.0
  */
-U_INTERNAL UEnumeration* U_EXPORT2
+U_CAPI UEnumeration* U_EXPORT2
 ures_getKeywordValues(const char *path, const char *keyword, UErrorCode *status);
 
+
+/**
+ * Get a resource with multi-level fallback. Normally only the top level resources will
+ * fallback to its parent. This performs fallback on subresources. For example, when a table
+ * is defined in a resource bundle and a parent resource bundle, normally no fallback occurs
+ * on the sub-resources because the table is defined in the current resource bundle, but this
+ * function can perform fallback on the sub-resources of the table.
+ * @param resB              a resource
+ * @param inKey             a key associated with the requested resource
+ * @param fillIn            if NULL a new UResourceBundle struct is allocated and must be deleted by the caller.
+ *                          Alternatively, you can supply a struct to be filled by this function.
+ * @param status: fills in the outgoing error code
+ *                could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
+ *                could be a non-failing error 
+ *                e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
+ * @return                  a pointer to a UResourceBundle struct. If fill in param was NULL, caller must delete it
+ */
+U_CAPI UResourceBundle* U_EXPORT2 
+ures_getByKeyWithFallback(const UResourceBundle *resB, 
+                          const char* inKey, 
+                          UResourceBundle *fillIn, 
+                          UErrorCode *status);
+
+
+/**
+ * Get a String with multi-level fallback. Normally only the top level resources will
+ * fallback to its parent. This performs fallback on subresources. For example, when a table
+ * is defined in a resource bundle and a parent resource bundle, normally no fallback occurs
+ * on the sub-resources because the table is defined in the current resource bundle, but this
+ * function can perform fallback on the sub-resources of the table.
+ * @param resB              a resource
+ * @param inKey             a key associated with the requested resource
+ * @param status: fills in the outgoing error code
+ *                could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
+ *                could be a non-failing error 
+ *                e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
+ * @return                  a pointer to a UResourceBundle struct. If fill in param was NULL, caller must delete it
+ */
+U_CAPI const UChar* U_EXPORT2 
+ures_getStringByKeyWithFallback(const UResourceBundle *resB, 
+                          const char* inKey,  
+                          int32_t* len,
+                          UErrorCode *status);
+
+#ifdef __cplusplus
+
+U_CAPI void U_EXPORT2
+ures_getValueWithFallback(const UResourceBundle *bundle, const char *path,
+                          UResourceBundle *tempFillIn,
+                          icu::ResourceDataValue &value, UErrorCode &errorCode);
+
+U_CAPI void U_EXPORT2
+ures_getAllItemsWithFallback(const UResourceBundle *bundle, const char *path,
+                             icu::ResourceSink &sink, UErrorCode &errorCode);
+
+#endif  /* __cplusplus */
+
+/**
+ * Get a version number by key
+ * @param resB bundle containing version number
+ * @param key the key for the version number
+ * @param ver fillin for the version number
+ * @param status error code
+ */
+U_CAPI void U_EXPORT2
+ures_getVersionByKey(const UResourceBundle *resB,
+                     const char *key,
+                     UVersionInfo ver,
+                     UErrorCode *status);
+
+
+/**
+ * Internal function.
+ * Return the version number associated with this ResourceBundle as a string.
+ *
+ * @param resourceBundle The resource bundle for which the version is checked.
+ * @return  A version number string as specified in the resource bundle or its parent.
+ *          The caller does not own this string.
+ * @see ures_getVersion
+ */
+U_CAPI const char* U_EXPORT2 
+ures_getVersionNumberInternal(const UResourceBundle *resourceBundle);
+
+/**
+ * Return the name of the Locale associated with this ResourceBundle. This API allows
+ * you to query for the real locale of the resource. For example, if you requested 
+ * "en_US_CALIFORNIA" and only "en_US" bundle exists, "en_US" will be returned. 
+ * For subresources, the locale where this resource comes from will be returned.
+ * If fallback has occured, getLocale will reflect this.
+ *
+ * This internal version avoids deprecated-warnings in ICU code.
+ *
+ * @param resourceBundle resource bundle in question
+ * @param status just for catching illegal arguments
+ * @return  A Locale name
+ */
+U_CAPI const char* U_EXPORT2 
+ures_getLocaleInternal(const UResourceBundle* resourceBundle, 
+               UErrorCode* status);
+
+/**
+ * Same as ures_openDirect() but uses the fill-in parameter instead of allocating a new bundle.
+ * 
+ * @param r The existing UResourceBundle to fill in. If NULL then status will be
+ *          set to U_ILLEGAL_ARGUMENT_ERROR.
+ * @param packageName   The packageName and locale together point to an ICU udata object,
+ *                      as defined by <code> udata_open( packageName, "res", locale, err) </code>
+ *                      or equivalent.  Typically, packageName will refer to a (.dat) file, or to
+ *                      a package registered with udata_setAppData(). Using a full file or directory
+ *                      pathname for packageName is deprecated. If NULL, ICU data will be used.
+ * @param locale  specifies the locale for which we want to open the resource
+ *                if NULL, the default locale will be used. If strlen(locale) == 0
+ *                root locale will be used.
+ * @param status The error code.
+ * @see ures_openDirect
+ * @internal
+ */
+U_CAPI void U_EXPORT2
+ures_openDirectFillIn(UResourceBundle *r,
+                      const char *packageName,
+                      const char *locale,
+                      UErrorCode *status);
+
+/**
+ * Same as ures_open(), except that if no resource bundle for the specified package name and locale exists,
+ * and the incoming locale specifies a country, this will fall back to the resource bundle for the specified country
+ * and the specified country's default language.  For example, if caller asks for fr_JP and no bundle for fr_JP exists,
+ * ures_open() will fall back to fr and this function will fall back to ja_JP.
+ * @param packageName   The packageName and locale together point to an ICU udata object,
+ *                      as defined by <code> udata_open( packageName, "res", locale, err) </code>
+ *                      or equivalent.  Typically, packageName will refer to a (.dat) file, or to
+ *                      a package registered with udata_setAppData(). Using a full file or directory
+ *                      pathname for packageName is deprecated. If NULL, ICU data will be used.
+ * @param locale  specifies the locale for which we want to open the resource
+ *                if NULL, the default locale will be used. If strlen(locale) == 0
+ *                root locale will be used.
+ * @param didFallBackByCountry If not NULL, filled in with a Boolean value that indicates whether we actually
+ *                            did fall back by country.  If this value is FALSE, the result of calling this function
+ *                            is the same as the caller would have gotten from ures_open().
+ * @param status  fills in the outgoing error code.
+ * @return      a newly allocated resource bundle.
+ * @see ures_open
+ * @internal
+ */
+U_CAPI UResourceBundle*  U_EXPORT2
+ures_openWithCountryFallback(const char*  packageName,
+                             const char*  locale,
+                             UBool*       didFallBackByCountry,
+                             UErrorCode*  status);
+
+/**
+ * If localeID has a parent in parentLocale data, return that.
+ * Otherwise return empty string.
+ *
+ * A more general version of this might then itself call
+ * uloc_getParent, but that is not what we want for ualoc usage.
+ *
+ * Some version of this (perhaps more general) should eventually move
+ * (with new name) to ulocimp.h, or perhaps uloc.h.
+ *
+ * @param localeID Input locale ID string.
+ * @param parent   Output string buffer for the parent locale ID.
+ * @param parentCapacity Size of the output buffer.
+ * @param err A UErrorCode value.
+ * @return The length of the parent locale ID.
+ * @see uloc_getParent
+ * @internal Apple <rdar://problem/63880069>
+ */
+U_CAPI int32_t U_EXPORT2
+ures_getLocParent(const char* localeID,
+                 char* parent,
+                 int32_t parentCapacity,
+                 UErrorCode* err);
+
 #endif /*URESIMP_H*/