2
0

MsgCat.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*
  24. * (c) Copyright 1995 Digital Equipment Corporation.
  25. * (c) Copyright 1995 Hewlett-Packard Company.
  26. * (c) Copyright 1995 International Business Machines Corp.
  27. * (c) Copyright 1995 Sun Microsystems, Inc.
  28. * (c) Copyright 1995 Novell, Inc.
  29. * (c) Copyright 1995 FUJITSU LIMITED.
  30. * (c) Copyright 1995 Hitachi.
  31. *
  32. * MsgCat.c - public interfaces for the Cached Message Catalog Service
  33. *
  34. * $TOG: MsgCat.c /main/4 1999/07/02 14:02:03 mgreess $
  35. *
  36. */
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <nl_types.h>
  40. #include <X11/Intrinsic.h>
  41. #include <Dt/MsgCatP.h>
  42. #include <DtSvcLock.h>
  43. typedef struct _dt_msg_cache
  44. {
  45. char ***cached_msgs;
  46. int nmsgs_per_set;
  47. int nsets;
  48. nl_catd catd;
  49. struct _dt_msg_cache *next;
  50. } _DtMsgCache;
  51. static _DtMsgCache *catalog_message_caches = NULL;
  52. static _DtMsgCache *get_msg_cache(nl_catd catd)
  53. {
  54. #define INITIAL_NMSGS_PER_SET 300
  55. #define INITIAL_NSETS 50
  56. _DtMsgCache *c;
  57. for (c=catalog_message_caches; NULL!=c; c=c->next)
  58. if (catd == c->catd) return c;
  59. c = (_DtMsgCache*) XtMalloc(sizeof(_DtMsgCache));
  60. c->cached_msgs = NULL;
  61. c->nmsgs_per_set = INITIAL_NMSGS_PER_SET;
  62. c->nsets = INITIAL_NSETS;
  63. c->catd = catd;
  64. c->next = catalog_message_caches;
  65. catalog_message_caches = c;
  66. return c;
  67. }
  68. /*
  69. * Wrapper around catgets -- this makes sure the message string is saved
  70. * in a safe location; so repeated calls to catgets() do not overwrite
  71. * the catgets() internal buffer. This has been a problem on HP systems.
  72. */
  73. char *_DtCatgetsCached(nl_catd catd, int set, int num, char *dflt)
  74. {
  75. char *message;
  76. #if !defined(hpV4)
  77. message = catgets(catd, set, num, dflt);
  78. #else
  79. _DtMsgCache *c;
  80. char **setptr;
  81. int i, multiplier;
  82. int size;
  83. /* convert to a zero based index */
  84. int setIdx = set - 1;
  85. int numIdx = num - 1;
  86. _DtSvcProcessLock();
  87. c = get_msg_cache(catd);
  88. if (NULL == c)
  89. {
  90. message = catgets(catd, set, num, dflt);
  91. return message;
  92. }
  93. if (NULL == c->cached_msgs)
  94. {
  95. size = sizeof(char**) * c->nsets;
  96. c->cached_msgs = (char***) XtMalloc(size);
  97. memset((char*) c->cached_msgs, 0, size);
  98. }
  99. else if (setIdx >= c->nsets)
  100. {
  101. for (multiplier=2; setIdx > multiplier*c->nsets; multiplier++) {}
  102. size = sizeof(char**) * c->nsets;
  103. c->cached_msgs =
  104. (char***) XtRealloc((char*) c->cached_msgs, multiplier*size);
  105. memset((char*) (c->cached_msgs + size), 0, multiplier*size);
  106. c->nsets *= multiplier;
  107. }
  108. if (NULL == c->cached_msgs[setIdx])
  109. {
  110. size = sizeof(char*) * c->nmsgs_per_set;
  111. c->cached_msgs[setIdx] = (char**) XtMalloc(size);
  112. memset((char*) c->cached_msgs[setIdx], 0, size);
  113. }
  114. else if (numIdx >= c->nmsgs_per_set)
  115. {
  116. for (multiplier=2; numIdx > multiplier*c->nsets; multiplier++) {}
  117. size = sizeof(char*) * c->nmsgs_per_set;
  118. for (i=0; i<c->nmsgs_per_set; i++)
  119. {
  120. if (NULL != c->cached_msgs[i])
  121. {
  122. c->cached_msgs[i] =
  123. (char**) XtRealloc((char*)c->cached_msgs[i], multiplier*size);
  124. memset((char*) (c->cached_msgs[i] + size), 0, multiplier*size);
  125. }
  126. }
  127. c->nmsgs_per_set *= multiplier;
  128. }
  129. setptr = c->cached_msgs[setIdx];
  130. if (NULL == setptr[numIdx])
  131. setptr[numIdx] = strdup(catgets(catd, set, num, dflt));
  132. message = setptr[numIdx];
  133. _DtSvcProcessUnlock();
  134. #endif /* hpV4 */
  135. return message;
  136. }