MessageTable.C 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. /* $XConsortium: MessageTable.C /main/1 1996/07/29 16:57:45 cde-hp $ */
  24. // Copyright (c) 1996 James Clark
  25. // See the file COPYING for copying permission.
  26. #ifdef __GNUG__
  27. #pragma implementation
  28. #endif
  29. #include "splib.h"
  30. #include "MessageTable.h"
  31. #ifdef SP_NO_MESSAGE_TEXT
  32. // Windows only
  33. #define STRICT
  34. #include "windows.h"
  35. static HINSTANCE dllInstanceHandle = NULL;
  36. #ifdef SP_NAMESPACE
  37. namespace SP_NAMESPACE {
  38. #endif
  39. class WinMessageTable : public MessageTable {
  40. public:
  41. Boolean getText(const MessageFragment &,
  42. String<SP_TCHAR> &) const;
  43. };
  44. Boolean WinMessageTable::getText(const MessageFragment &frag,
  45. String<SP_TCHAR> &str) const
  46. {
  47. static const int bufSize = 4096;
  48. SP_TCHAR buf[bufSize];
  49. int len = LoadString(frag.module() == MessageFragment::libModule
  50. ? dllInstanceHandle
  51. : 0,
  52. frag.number(),
  53. buf,
  54. bufSize);
  55. if (len == 0) {
  56. // The resource might be empty.
  57. if (GetLastError() != 0)
  58. return 0;
  59. else {
  60. str.resize(0);
  61. return 1;
  62. }
  63. }
  64. str.assign(buf, len);
  65. return 1;
  66. }
  67. const MessageTable *MessageTable::instance()
  68. {
  69. if (!instance_)
  70. instance_ = new WinMessageTable;
  71. return instance_;
  72. }
  73. #ifdef SP_NAMESPACE
  74. }
  75. #endif
  76. #ifdef SP_USE_DLL
  77. extern "C"
  78. BOOL WINAPI DllMain(HINSTANCE inst, ULONG reason, LPVOID)
  79. {
  80. if (reason == DLL_PROCESS_ATTACH)
  81. dllInstanceHandle = inst;
  82. return TRUE;
  83. }
  84. #endif
  85. #else /* not SP_NO_MESSAGE_TEXT */
  86. #ifdef SP_HAVE_GETTEXT
  87. extern "C" {
  88. extern char *dgettext(const char *, const char *);
  89. extern char *gettext(const char *);
  90. extern char *textdomain(const char *);
  91. extern char *bindtextdomain(const char *, const char *);
  92. }
  93. #include <stdlib.h>
  94. #ifndef MESSAGE_DOMAIN
  95. #define MESSAGE_DOMAIN "sp"
  96. #endif
  97. #ifdef SP_NAMESPACE
  98. namespace SP_NAMESPACE {
  99. #endif
  100. class GettextMessageTable : public MessageTable {
  101. public:
  102. GettextMessageTable();
  103. Boolean getText(const MessageFragment &, String<SP_TCHAR> &) const;
  104. };
  105. GettextMessageTable::GettextMessageTable()
  106. {
  107. const char *dir = getenv("TEXTDOMAINDIR");
  108. if (dir)
  109. bindtextdomain(MESSAGE_DOMAIN, dir);
  110. }
  111. Boolean GettextMessageTable::getText(const MessageFragment &frag,
  112. String<SP_TCHAR> &str) const
  113. {
  114. const char *s = frag.text();
  115. if (!s)
  116. return 0;
  117. s = dgettext(MESSAGE_DOMAIN, s);
  118. if (!s)
  119. return 0;
  120. str.assign(s, strlen(s));
  121. return 1;
  122. }
  123. const MessageTable *MessageTable::instance()
  124. {
  125. if (!instance_)
  126. instance_ = new GettextMessageTable;
  127. return instance_;
  128. }
  129. #ifdef SP_NAMESPACE
  130. }
  131. #endif
  132. #else /* not SP_HAVE_GETTEXT */
  133. #ifdef SP_NAMESPACE
  134. namespace SP_NAMESPACE {
  135. #endif
  136. class DefaultMessageTable : public MessageTable {
  137. public:
  138. Boolean getText(const MessageFragment &, String<SP_TCHAR> &) const;
  139. };
  140. Boolean DefaultMessageTable::getText(const MessageFragment &frag,
  141. String<SP_TCHAR> &str) const
  142. {
  143. if (!frag.text())
  144. return 0;
  145. str.resize(0);
  146. for (const char *s = frag.text(); *s; s++)
  147. str += SP_TCHAR((unsigned char)*s);
  148. return 1;
  149. }
  150. const MessageTable *MessageTable::instance()
  151. {
  152. if (!instance_)
  153. instance_ = new DefaultMessageTable;
  154. return instance_;
  155. }
  156. #ifdef SP_NAMESPACE
  157. }
  158. #endif
  159. #endif /* not SP_HAVE_GETTEXT */
  160. #endif /* not SP_NO_MESSAGE_TEXT */
  161. #ifdef SP_NAMESPACE
  162. namespace SP_NAMESPACE {
  163. #endif
  164. MessageTable *MessageTable::instance_ = 0;
  165. #ifdef SP_NAMESPACE
  166. }
  167. #endif