summaryrefslogtreecommitdiffstats
path: root/lib/libsi18n/getstrmem.c
blob: 693cc0e356ed85300d985257ae3474d9e1a89b09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/** BEGIN COPYRIGHT BLOCK
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * 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
}