getstrprop.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include "i18n.h"
  16. #include "getstrmem.h"
  17. static char*
  18. XP_GetStringFromMemory(const char* strLibraryName,int iToken)
  19. {
  20. /*
  21. * In memory model called by XP_GetStringFromDatabase
  22. * does not use database (nsres, et al.).
  23. *
  24. * This function uses hash table for library lookup
  25. * and direct lookup for string.
  26. *
  27. * This function is thread safe.
  28. */
  29. unsigned hashKey;
  30. int found = 0;
  31. unsigned uToken = iToken;
  32. const char* cPtr;
  33. DATABIN* pBucket;
  34. /* calculate hash key */
  35. hashKey = 0;
  36. cPtr = strLibraryName;
  37. while (*cPtr) {
  38. hashKey += *(cPtr++);
  39. }
  40. hashKey &= BUCKET_MASK;
  41. /* get bucket for this hash key */
  42. pBucket = buckets[hashKey];
  43. /* search overflow buckets */
  44. while (*(pBucket->pLibraryName)!='\0') {
  45. if (strcmp(pBucket->pLibraryName,strLibraryName)==0) {
  46. found = 1;
  47. break;
  48. }
  49. pBucket++;
  50. }
  51. if (!found) {
  52. return emptyString;
  53. }
  54. if (uToken<=pBucket->numberOfStringsInLibrary) {
  55. return pBucket->pArrayOfLibraryStrings[uToken];
  56. } else {
  57. /* string token out of range */
  58. return emptyString;
  59. }
  60. }
  61. const char*
  62. XP_GetStringFromDatabase(const char* strLibraryName,
  63. const char* strLanguage,
  64. int key)
  65. {
  66. const char *result = NULL;
  67. /* we use memory strings only in ds. */
  68. if (result == NULL)
  69. result = XP_GetStringFromMemory(strLibraryName,key);
  70. return result;
  71. }