/** BEGIN COPYRIGHT BLOCK * Copyright 2001 Sun Microsystems, Inc. * Portions copyright 1999, 2001-2003 Netscape Communications Corporation. * All rights reserved. * END COPYRIGHT BLOCK **/ #include #include #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 }