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