summaryrefslogtreecommitdiffstats
path: root/lib/libsi18n/getstrmem.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libsi18n/getstrmem.c')
-rw-r--r--lib/libsi18n/getstrmem.c123
1 files changed, 123 insertions, 0 deletions
diff --git a/lib/libsi18n/getstrmem.c b/lib/libsi18n/getstrmem.c
new file mode 100644
index 00000000..b49b4cc6
--- /dev/null
+++ b/lib/libsi18n/getstrmem.c
@@ -0,0 +1,123 @@
+/** BEGIN COPYRIGHT BLOCK
+ * Copyright 2001 Sun Microsystems, Inc.
+ * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
+ * All rights reserved.
+ * END COPYRIGHT BLOCK **/
+
+#include <stdio.h>
+#include <string.h>
+
+#define COMPILE_STRINGS_IN_MEMORY
+
+static char emptyString[] = "";
+
+#ifdef COMPILE_STRINGS_IN_MEMORY
+
+typedef struct DATABIN {
+ char* pLibraryName;
+ char** pArrayOfLibraryStrings;
+ unsigned numberOfStringsInLibrary;
+ } DATABIN;
+
+#include "getstrmem.h"
+
+#endif
+
+#if 0 /* Example of getstrmem.h */
+ /* It is intended that this header file be generated by program dblink */
+
+ #define NUM_BUCKETS 2 /* must be power of 2 */
+
+ /* strings in library libalpha */
+ static char* libalpha[] = {
+ "",
+ "libalpha string 1",
+ "libalpha string 2",
+ "libalpha string 3",
+ "libalpha string 4",
+ emptyString };
+
+ /* strings in library libbeta */
+ static char* libbeta[] = {
+ "",
+ "libbeta string 1",
+ "libbeta string 2",
+ emptyString };
+
+ /* libraries in bucket for hashKey==0 */
+ static struct DATABIN bucket0[] = {
+ {emptyString, NULL, 0}};
+
+ /* libraries in bucket for hashKey==1 */
+ static struct DATABIN bucket1[] = {
+ {"libalpha", libalpha, 5},
+ {"libbeta", libbeta, 3},
+ {emptyString, NULL, 0}};
+
+ /* array of buckets */
+ static struct DATABIN* buckets[NUM_BUCKETS] = {
+ bucket0,
+ bucket1 };
+
+#endif /* end of example getstrmem.h */
+
+#define BUCKET_MASK NUM_BUCKETS-1
+
+char*
+XP_GetStringFromMemory(char* strLibraryName,int iToken)
+{
+ /*
+ * In memory model called by XP_GetStringFromDatabase
+ * does not use database (nsres, et al.).
+ *
+ * This function uses hash table for library lookup
+ * and direct lookup for string.
+ *
+ * This function is thread safe.
+ */
+
+#ifdef COMPILE_STRINGS_IN_MEMORY
+
+ unsigned hashKey;
+ int found = 0;
+ unsigned uToken = iToken;
+ char* cPtr;
+ DATABIN* pBucket;
+
+ /* calculate hash key */
+ hashKey = 0;
+ cPtr = strLibraryName;
+ while (*cPtr) {
+ hashKey += *(cPtr++);
+ }
+ hashKey &= BUCKET_MASK;
+
+ /* get bucket for this hash key */
+ pBucket = buckets[hashKey];
+
+ /* search overflow buckets */
+ while (*(pBucket->pLibraryName)!='\0') {
+ if (strcmp(pBucket->pLibraryName,strLibraryName)==0) {
+ found = 1;
+ break;
+ }
+ pBucket++;
+ }
+
+ if (!found) {
+ return emptyString;
+ }
+
+ if (uToken<=pBucket->numberOfStringsInLibrary) {
+ return pBucket->pArrayOfLibraryStrings[uToken];
+ } else {
+ /* string token out of range */
+ return emptyString;
+ }
+
+#else
+
+ return emptyString;
+
+#endif
+}