aboutsummaryrefslogtreecommitdiff
path: root/locale/src/locale.c
blob: b74a0e107d1043fa7834bf2d03f9bcb52724b681 (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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/* -*- Mode: C -*- */
/*=============================================================================*\
 * File: locale.c
 * Author: Bryan Jurish <moocow@ling.uni-potsdam.de>
 * Description: general directory access object
 *
 * Copyright (c) 2009 Bryan Jurish.
 *
 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
 * WARRANTIES, see the file, "LICENSE.txt," in this distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * See file LICENSE for further informations on licensing terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *=============================================================================*/

#include <string.h>
#include <stdlib.h>
#include <stddef.h>

#include <m_pd.h>

/* black magic */
#ifdef NT
#pragma warning( disable : 4244 )
#pragma warning( disable : 4305 )
#endif

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#ifdef HAVE_WCHAR_H
# include <wchar.h>
#endif
#ifdef HAVE_LOCALE_H
# include <locale.h>
#endif

/*--------------------------------------------------------------------
 * DEBUG
 *--------------------------------------------------------------------*/
/*#define LOCALE_DEBUG 1*/

/*--------------------------------------------------------------------
 * "unused" pragma
 */
#ifdef __GNUC__
# define MOO_UNUSED __attribute__((unused))
#else
# define MOO_UNUSED
#endif

/*=====================================================================
 * Constants
 *=====================================================================*/

/*=====================================================================
 * Structures and Types
 *=====================================================================*/

static char *locale_banner =
  "\nlocale: C99 locale support version " PACKAGE_VERSION " by Bryan Jurish\n"
    "locale: compiled by " PDMOOEXT_USER " on " PDMOOEXT_DATE ;

static t_class *locale_class;

typedef struct _locale
{
  t_object       x_obj;
  t_outlet      *x_outlet;   //-- data outlet
} t_locale;

/*=====================================================================
 * Constants
 *=====================================================================*/

/*-- LC_* constants defined in locale.h (debian libc6-dev 2.7-5)--

LC_ALL
LC_CTYPE
LC_NUMERIC
LC_TIME
LC_COLLATE
LC_MONETARY
LC_MESSAGES
LC_PAPER
LC_NAME
LC_ADDRESS
LC_TELEPHONE
LC_MEASUREMENT
LC_IDENTIFICATION

*/

//-- Constants: categories
#if HAVE_DECL_LC_ALL
 static t_symbol *sp_LC_ALL;
#endif
#if HAVE_DECL_LC_CTYPE
 static t_symbol *sp_LC_CTYPE;
#endif
#if HAVE_DECL_LC_NUMERIC
 static t_symbol *sp_LC_NUMERIC;
#endif
#if HAVE_DECL_LC_TIME
 static t_symbol *sp_LC_TIME;
#endif
#if HAVE_DECL_LC_COLLATE
 static t_symbol *sp_LC_COLLATE;
#endif
#if HAVE_DECL_LC_MONETARY
 static t_symbol *sp_LC_MONETARY;
#endif
#if HAVE_DECL_LC_MESSAGES
 static t_symbol *sp_LC_MESSAGES;
#endif
#if HAVE_DECL_LC_PAPER
 static t_symbol *sp_LC_PAPER;
#endif
#if HAVE_DECL_LC_NAME
 static t_symbol *sp_LC_NAME;
#endif
#if HAVE_DECL_LC_ADDRESS
 static t_symbol *sp_LC_ADDRESS;
#endif
#if HAVE_DECL_LC_TELEPHONE
 static t_symbol *sp_LC_TELEPHONE;
#endif
#if HAVE_DECL_LC_MEASUREMENT
 static t_symbol *sp_LC_MEASUREMENT;
#endif
#if HAVE_DECL_LC_IDENTIFICATION
 static t_symbol *sp_LC_IDENTIFICATION;
#endif

/*=====================================================================
 * Utilities
 *=====================================================================*/

/*--------------------------------------------------------------------
 * sym2cat(symbol)
 */
static int sym2cat(t_object *obj, t_symbol *sym)
{
  if (sym==sp_LC_ALL || sym==&s_) return LC_ALL;
#if HAVE_DECL_LC_CTYPE
  else if (sym==sp_LC_CTYPE) return LC_CTYPE;
#endif
#if HAVE_DECL_LC_NUMERIC
  else if (sym==sp_LC_NUMERIC) return LC_NUMERIC;
#endif
#if HAVE_DECL_LC_TIME
  else if (sym==sp_LC_TIME) return LC_TIME;
#endif
#if HAVE_DECL_LC_COLLATE
  else if (sym==sp_LC_COLLATE) return LC_COLLATE;
#endif
#if HAVE_DECL_LC_MONETARY
  else if (sym==sp_LC_MONETARY) return LC_MONETARY;
#endif
#if HAVE_DECL_LC_MESSAGES
  else if (sym==sp_LC_MESSAGES) return LC_MESSAGES;
#endif
#if HAVE_DECL_LC_PAPER
  else if (sym==sp_LC_PAPER) return LC_PAPER;
#endif
#if HAVE_DECL_LC_NAME
  else if (sym==sp_LC_NAME) return LC_NAME;
#endif
#if HAVE_DECL_LC_ADDRESS
  else if (sym==sp_LC_ADDRESS) return LC_ADDRESS;
#endif
#if HAVE_DECL_LC_TELEPHONE
  else if (sym==sp_LC_TELEPHONE) return LC_TELEPHONE;
#endif
#if HAVE_DECL_LC_MEASUREMENT
  else if (sym==sp_LC_MEASUREMENT) return LC_MEASUREMENT;
#endif
#if HAVE_DECL_LC_IDENTIFICATION
  else if (sym==sp_LC_IDENTIFICATION) return LC_IDENTIFICATION;
#endif
#if HAVE_DECL_LC_FOOBAR
  else if (sym==sp_LC_FOOBAR) return LC_FOOBAR;
#endif
  pd_error(obj, ": sym2cat() could not find locale category for symbol '%s', using LC_ALL", sym->s_name);
  return LC_ALL;
}

/*--------------------------------------------------------------------
 * cat2sym(cat)
 */
MOO_UNUSED
static t_symbol *cat2sym(t_object *obj, int cat)
{
  switch (cat) {
#if HAVE_DECL_LC_ALL
  case LC_ALL: return sp_LC_ALL; break;
#endif
#if HAVE_DECL_LC_CTYPE
  case LC_CTYPE: return sp_LC_CTYPE; break;
#endif
#if HAVE_DECL_LC_NUMERIC
  case LC_NUMERIC: return sp_LC_NUMERIC; break;
#endif
#if HAVE_DECL_LC_TIME
  case LC_TIME: return sp_LC_TIME; break;
#endif
#if HAVE_DECL_LC_COLLATE
  case LC_COLLATE: return sp_LC_COLLATE; break;
#endif
#if HAVE_DECL_LC_MONETARY
  case LC_MONETARY: return sp_LC_MONETARY; break;
#endif
#if HAVE_DECL_LC_MESSAGES
  case LC_MESSAGES: return sp_LC_MESSAGES; break;
#endif
#if HAVE_DECL_LC_PAPER
  case LC_PAPER: return sp_LC_PAPER; break;
#endif
#if HAVE_DECL_LC_NAME
  case LC_NAME: return sp_LC_NAME; break;
#endif
#if HAVE_DECL_LC_ADDRESS
  case LC_ADDRESS: return sp_LC_ADDRESS; break;
#endif
#if HAVE_DECL_LC_TELEPHONE
  case LC_TELEPHONE: return sp_LC_TELEPHONE; break;
#endif
#if HAVE_DECL_LC_MEASUREMENT
  case LC_MEASUREMENT: return sp_LC_MEASUREMENT; break;
#endif
#if HAVE_DECL_LC_IDENTIFICATION
  case LC_IDENTIFICATION: return sp_LC_IDENTIFICATION; break;
#endif
  default: break;
  }
  pd_error(obj, ": cat2sym() unknown locale category '%d'", cat);
  return &s_;
}

/*=====================================================================
 * Methods
 *=====================================================================*/

/*--------------------------------------------------------------------
 * bang
 */
static void locale_bang(t_locale *x)
{
  setlocale(LC_ALL,"");
}

/*--------------------------------------------------------------------
 * get , get CATEGORY
 */
static void locale_get(t_locale *x, t_symbol *catsym)
{
  int   cat = sym2cat((t_object*)x, catsym);
  char *val;
  t_atom valatom;
  val = setlocale(cat, NULL);
  if (val) {
    SETSYMBOL(&valatom,gensym(val));
  } else {
    SETSYMBOL(&valatom,&s_);
  }
  outlet_anything(x->x_outlet, catsym, 1, &valatom);
}


/*--------------------------------------------------------------------
 * set , set CATEGORY VALUE
 */
static void locale_set(t_locale *x, t_symbol *catsym, t_symbol *valsym)
{
  int cat;
  if (catsym==&s_ && valsym==&s_) { locale_bang(x); return; }
  cat = sym2cat((t_object*)x,catsym);
  setlocale(cat,valsym->s_name);
}

/*--------------------------------------------------------------------
 * reset -> set LC_ALL C
 */
static void locale_reset(t_locale *x)
{
  setlocale(LC_ALL,"C");
}

/*--------------------------------------------------------------------
 * which
 */
static void locale_which(t_locale *x)
{
#if HAVE_DECL_LC_ALL
  outlet_symbol(x->x_outlet,sp_LC_ALL);
#endif
#if HAVE_DECL_LC_CTYPE
  outlet_symbol(x->x_outlet,sp_LC_CTYPE);
#endif
#if HAVE_DECL_LC_NUMERIC
  outlet_symbol(x->x_outlet,sp_LC_NUMERIC);
#endif
#if HAVE_DECL_LC_TIME
  outlet_symbol(x->x_outlet,sp_LC_TIME);
#endif
#if HAVE_DECL_LC_COLLATE
  outlet_symbol(x->x_outlet,sp_LC_COLLATE);
#endif
#if HAVE_DECL_LC_MONETARY
  outlet_symbol(x->x_outlet,sp_LC_MONETARY);
#endif
#if HAVE_DECL_LC_MESSAGES
  outlet_symbol(x->x_outlet,sp_LC_MESSAGES);
#endif
#if HAVE_DECL_LC_PAPER
  outlet_symbol(x->x_outlet,sp_LC_PAPER);
#endif
#if HAVE_DECL_LC_NAME
  outlet_symbol(x->x_outlet,sp_LC_NAME);
#endif
#if HAVE_DECL_LC_ADDRESS
  outlet_symbol(x->x_outlet,sp_LC_ADDRESS);
#endif
#if HAVE_DECL_LC_TELEPHONE
  outlet_symbol(x->x_outlet,sp_LC_TELEPHONE);
#endif
#if HAVE_DECL_LC_MEASUREMENT
  outlet_symbol(x->x_outlet,sp_LC_MEASUREMENT);
#endif
#if HAVE_DECL_LC_IDENTIFICATION
  outlet_symbol(x->x_outlet,sp_LC_IDENTIFICATION);
#endif
}


/*--------------------------------------------------------------------
 * new
 */
static void *locale_new(void)
{
    t_locale *x = (t_locale *)pd_new(locale_class);
    x->x_outlet = outlet_new(&x->x_obj, &s_anything);
    return (void *)x;
}

/*--------------------------------------------------------------------
 * free
 */
static void locale_free(t_locale *x)
{
  outlet_free(x->x_outlet);
  return;
}

/*=====================================================================
 * Setup
 *=====================================================================*/

/*--------------------------------------------------------------------
 * setup: symbols
 */
static void locale_setup_constants(void)
{
#if HAVE_DECL_LC_ALL
  sp_LC_ALL = gensym("LC_ALL");
#endif
#if HAVE_DECL_LC_CTYPE
  sp_LC_CTYPE = gensym("LC_CTYPE");
#endif
#if HAVE_DECL_LC_NUMERIC
  sp_LC_NUMERIC = gensym("LC_NUMERIC");
#endif
#if HAVE_DECL_LC_TIME
  sp_LC_TIME = gensym("LC_TIME");
#endif
#if HAVE_DECL_LC_COLLATE
  sp_LC_COLLATE = gensym("LC_COLLATE");
#endif
#if HAVE_DECL_LC_MONETARY
  sp_LC_MONETARY = gensym("LC_MONETARY");
#endif
#if HAVE_DECL_LC_MESSAGES
  sp_LC_MESSAGES = gensym("LC_MESSAGES");
#endif
#if HAVE_DECL_LC_PAPER
  sp_LC_PAPER = gensym("LC_PAPER");
#endif
#if HAVE_DECL_LC_NAME
  sp_LC_NAME = gensym("LC_NAME");
#endif
#if HAVE_DECL_LC_ADDRESS
  sp_LC_ADDRESS = gensym("LC_ADDRESS");
#endif
#if HAVE_DECL_LC_TELEPHONE
  sp_LC_TELEPHONE = gensym("LC_TELEPHONE");
#endif
#if HAVE_DECL_LC_MEASUREMENT
  sp_LC_MEASUREMENT = gensym("LC_MEASUREMENT");
#endif
#if HAVE_DECL_LC_IDENTIFICATION
  sp_LC_IDENTIFICATION = gensym("LC_IDENTIFICATION");
#endif
}

/*--------------------------------------------------------------------
 * setup
 */
void locale_setup(void)
{
  post(locale_banner);
#ifdef LOCALE_DEBUG
  post("locale : debugging enabled");
#endif

  //-- constants
  locale_setup_constants();

  //-- class
  locale_class = class_new(gensym("locale"),
			   (t_newmethod)locale_new,
			   (t_method)locale_free,
			   sizeof(t_locale),
			   CLASS_DEFAULT,
			   0);
  
  //-- methods: get
  class_addmethod(locale_class, (t_method)locale_get,     gensym("get"), A_DEFSYMBOL, 0);
  class_addmethod(locale_class, (t_method)locale_which,   gensym("which"), 0);
  //
  //-- methods: set
  class_addmethod(locale_class, (t_method)locale_set,     gensym("set"), A_DEFSYMBOL, A_DEFSYMBOL, 0);
  class_addmethod(locale_class, (t_method)locale_bang,    &s_bang, 0);
  class_addmethod(locale_class, (t_method)locale_reset,   gensym("reset"), 0);


  //-- help symbol
  class_sethelpsymbol(locale_class, gensym("locale-help.pd"));
}