summaryrefslogtreecommitdiff
path: root/magick/locale.c
blob: 6037e43d6d6282772fbe41e70450e002755921f8 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
% Copyright (C) 2003, 2004 GraphicsMagick Group
%
% This program is covered by multiple licenses, which are described in
% Copyright.txt. You should have received a copy of Copyright.txt with this
% package; otherwise see http://www.graphicsmagick.org/www/Copyright.html.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%                  L       OOO    CCCC   AAA   L      EEEEE                   %
%                  L      O   O  C      A   A  L      E                       %
%                  L      O   O  C      AAAAA  L      EEE                     %
%                  L      O   O  C      A   A  L      E                       %
%                  LLLLL   OOO    CCCC  A   A  LLLLL  EEEEE                   %
%                                                                             %
%                                                                             %
%                   GraphicsMagick Locale Message Methods                     %
%                                                                             %
%                                                                             %
%                              Software Design                                %
%                            William T. Radcliffe                             %
%                                 July 2003                                   %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
*/
 
/*
  Include declarations.
*/
#include "studio.h"
#include "utility.h"
#define _INCLUDE_CATEGORYMAP_TABLE_
#define _INCLUDE_SEVERITYMAP_TABLE_
#define _INCLUDE_TAGMAP_TABLE_
#if defined(MAGICK_WINDOWS_MESSAGE_TABLES)
#  include "spinlock.h"
#else
#  define _INCLUDE_MESSAGE_TABLE_
#endif
#include "locale_c.h"
 
/*
  Static declaractions.
*/
#if defined(MAGICK_WINDOWS_MESSAGE_TABLES)
#define MAX_CACHE_SIZE 32

static char
  cache[MaxTextExtent * MAX_CACHE_SIZE];

static const char *
AllocateManagedString(const char *s)
{
  char
    *cs;

  static int
    index = -1;

  SPINLOCK_WAIT;
  index++;
  if (index >= MAX_CACHE_SIZE)
    index=0;
  cs=&cache[MaxTextExtent * index];
  (void) strlcpy(cs,s,MaxTextExtent);
  SPINLOCK_RELEASE;
  return cs;
}

static const char *
NTFormatMessage(DWORD id, ...)
{
  va_list
    args;

  const char
    *result;

  char
    temp[MaxTextExtent];

  HMODULE
    handle;

  int
    status;

  LPVOID
    buffer;

  va_start( args, id );

  buffer = (LPVOID) NULL; /* stop compiler from complaining */
  FormatString(temp,"%.1024s%.1024s%.1024s",SetClientPath((char *) NULL),
               DirectorySeparator,SetClientFilename((char *) NULL));
  if (IsAccessibleNoLogging(temp))
    handle=GetModuleHandle(temp);
  else
    handle=GetModuleHandle(0);
  if (handle)
    {
      /*
        Sample of how to change threads locale explicitly to English.
        you can use this same API call to switch locales on a thread
        by thread basis and extract the correct localized message
      */
      /*
        SetThreadLocale( MAKELCID( MAKELANGID( 0x0409, SUBLANG_NEUTRAL
        ), SORT_DEFAULT ) );
      */
      status=FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
                           FORMAT_MESSAGE_FROM_HMODULE,
                           handle,
                           id,
                           MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
                           (LPTSTR) &buffer,
                           0,
                           &args
                           );
      if (!status)
        {
          FormatString(temp,"Undefined message %ld (0x%08lx)",(long) id,
                       (long) id);
          result=AllocateManagedString(temp);
        }
      else
        {
          char
            *s;

          int
            len;

          s = buffer;
          len = strlen(s);
          if (len > 1)
            {
              /*
                The string that is stored in the resource has a CR LF
                on the end, so we need to zap this off before
                returning it
              */
              s[len-1]='\0';
              s[len-2]='\0';
            }
          result=AllocateManagedString(s);
          LocalFree(buffer);
        }
    }
  else
    {
      FormatString(temp,"Undefined message %ld (0x%08lx)",(long) id, (long) id);
      result=AllocateManagedString(temp);
    }
  return(result);
}
#endif /* defined(MAGICK_WINDOWS_MESSAGE_TABLES) */

static void
ChopLocaleComponents(char *path,const unsigned long components)
{
  unsigned long
    count;

  register char
    *p;

  if (*path == '\0')
    return;
  p=path+strlen(path)-1;
  if (*p == '/')
    *p='\0';
  for (count=0; (count < components) && (p > path); p--)
    if (*p == '/')
      {
        *p='\0';
        count++;
      }
}

static const char *
GetLocaleMessageFromTag(const char *tag)
{
#if defined(_INCLUDE_TAGMAP_TABLE_)
  char
    category[MaxTextExtent],
    severity[MaxTextExtent];

  register unsigned int
    i,
    j,
    k;

  (void) strlcpy(category,tag,MaxTextExtent);
  ChopLocaleComponents(category,2);
  for (k=0; k < ArraySize(category_map); k++)
    {
      if (LocaleCompare(category,category_map[k].name) == 0)
        {
          (void) strlcpy(severity,tag,MaxTextExtent);
          ChopLocaleComponents(severity,1);
          for (j=category_map[k].offset; j < category_map[k+1].offset; j++)
            {
              if (LocaleCompare(severity,severity_map[j].name) == 0)
                {
                  for (i=severity_map[j].offset; i < severity_map[j+1].offset; i++)
                    {
                      size_t
                        prefix,
                        taglen;

                      prefix=strlen(severity);
                      taglen=strlen(tag);
                      if ((prefix > 0) && (prefix < taglen) &&
                          LocaleCompare(&tag[prefix+1],message_map[i].name) == 0)
                        {
#if defined(_INCLUDE_MESSAGE_TABLE_) /* &message_dat[message_dat_offsets[id]] */
                          return &message_dat[message_dat_offsets[message_map[i].messageid]];
#else
#  if defined(MAGICK_WINDOWS_MESSAGE_TABLES)
                          return &NTFormatMessage(message_dat_offsets[message_map[i].messageid]);
#  else
                          return tag;
#  endif /* defined(MAGICK_WINDOWS_MESSAGE_TABLES) */
#endif /* defined(_INCLUDE_MESSAGE_TABLE_) */
                        }
                    }
                }
            }
        }
    }
#endif
  return tag;
}

/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   G e t L o c a l e M e s s a g e                                           %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  GetLocaleMessage() returns a message in the current locale that matches the
%  supplied tag.
%
%  The format of the GetLocaleMessage method is:
%
%      const char *GetLocaleMessage(const char *tag)
%
%  A description of each parameter follows:
%
%    o tag: Return a message that matches this tag in the current locale.
%
%
*/
MagickExport const char *
GetLocaleMessage(const char *tag)
{
  return GetLocaleMessageFromTag(tag);
}

/*
  This routine translates a severity code to it's string value. It is
  slow but the idea is to eventually move away from using strings to
  lookup any messages and use binary codes instead.
 */
#if 0 /* not currently used */
static const char *
SeverityToTag(const ExceptionType severity)
{
  register unsigned int
    i;

  for (i=0; (i < ArraySize(severity_map)) && (severity_map[i].name != 0); i++)
  {
    if (severity == severity_map[i].severityid)
      return severity_map[i].name;
  }
  return("");
}

/*
  This routine is intended to be a replacement for
  GetLocaleExceptionMessage in the code, but using the data from the
  locale MGK files to do it's job rather then some hard coded case
  statement.
 */
static const char *
GetLocaleMessageFromSeverityAndTag(const ExceptionType severity,
                                   const char *tag)
{
  char
    message[MaxTextExtent];

  const char
    *locale_message;

  /*
    This is a hack that depends on the fact that tag can never have
    spaces in them. If a space is found then it means we are being
    asked to translate a message that has already been translated. A
    big waste of time. The reason this happens is that messages are
    translated at the point of an exception and then again when the
    exception is caught and processed via the default error and
    warning handlers
  */
  if (strrchr(tag, ' '))
    return(tag);
  FormatString(message,"%.1024s%.1024s",SeverityToTag(severity),tag);
  locale_message=GetLocaleMessage(message);
  if (locale_message == message)
    return(tag);
  return(locale_message);
}
#endif /* not currently used */

/*
  This routine is used to lookup a message directly from an id pulled
  from the header file generated by the coders\local.c header file
  coder.
 */
MagickExport const char *
GetLocaleMessageFromID(const int id)
{
  if ((id > 0) && (id <= MAX_LOCALE_MSGS))
    {
#if defined(MAGICK_WINDOWS_MESSAGE_TABLES)
      return NTFormatMessage(id);
#else
      return &message_dat[message_dat_offsets[id]];
#endif
    }
  return (const char *) NULL;
}