File: character.c

package info (click to toggle)
m17n-lib 1.6.1-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 4,532 kB
  • ctags: 4,388
  • sloc: ansic: 46,837; sh: 11,312; makefile: 646; yacc: 288; xml: 148; sed: 16
file content (659 lines) | stat: -rw-r--r-- 18,917 bytes parent folder | download | duplicates (2)
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
/* character.c -- character module.
   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
     National Institute of Advanced Industrial Science and Technology (AIST)
     Registration Number H15PRO112

   This file is part of the m17n library.

   The m17n library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public License
   as published by the Free Software Foundation; either version 2.1 of
   the License, or (at your option) any later version.

   The m17n library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the m17n library; if not, write to the Free
   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   02111-1307, USA.  */

/***en
    @addtogroup m17nCharacter
    @brief Character objects and API for them.

    The m17n library represents a @e character by a character code (an
    integer).  The minimum character code is @c 0.  The maximum
    character code is defined by the macro #MCHAR_MAX.  It is
    assured that #MCHAR_MAX is not smaller than @c 0x3FFFFF (22
    bits).

    Characters @c 0 to @c 0x10FFFF are equivalent to the Unicode
    characters of the same code values.

    A character can have zero or more properties called @e character
    @e properties.  A character property consists of a @e key and a
    @e value, where key is a symbol and value is anything that can be
    cast to <tt>(void *)</tt>.  "The character property that belongs
    to character C and whose key is K" may be shortened to "the K
    property of C".  */

/***ja
    @addtogroup m17nCharacter
    @brief ʸ֥ȤȤ˴ؤ API.

    m17n 饤֥ @e ʸ ʸɡˤɽ롣
    Ǿʸɤ @c 0 Ǥꡢʸɤϥޥ #MCHAR_MAX 
    ˤäƤ롣#MCHAR_MAX  @c 0x3FFFFF22ӥåȡ
    ʾǤ뤳ȤݾڤƤ롣

    @c 0  @c 0x10FFFF ޤǤʸϡƱͤ Unicode 
    ʸ˳ƤƤ롣

    ʸ @e ʸץѥƥ ȸƤ֥ץѥƥ 0 İʾĤȤǤ롣
    ʸץѥƥ @e   @e  ʤ롣
    ϥܥǤꡢͤ <tt>(void *)</tt> ˥㥹ȤǤΤʤ鲿Ǥ褤 
    ʸ C ʸץѥƥΤ K ǤΡפñˡʸ C 
     K ץѥƥפȸƤ֤Ȥ롣  */
/*=*/

#if !defined (FOR_DOXYGEN) || defined (DOXYGEN_INTERNAL_MODULE)
/*** @addtogroup m17nInternal
     @{ */

#include <config.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <ctype.h>
#include <stdio.h>

#include "m17n-core.h"
#include "m17n-misc.h"
#include "internal.h"

typedef struct
{
  MSymbol type;
  void *mdb;
  MCharTable *table;
} MCharPropRecord;

static MPlist *char_prop_list;

static void
free_string (int from, int to, void *str, void *arg)
{
  free (str);
}


/* Internal API */

int
mchar__init ()
{
  Mname = msymbol ("name");
  Mcategory = msymbol ("category");
  Mcombining_class = msymbol ("combining-class");
  Mbidi_category = msymbol ("bidirectional-category");
  Msimple_case_folding = msymbol ("simple-case-folding");
  Mcomplicated_case_folding = msymbol ("complicated-case-folding");
  Mcased = msymbol ("cased");
  Msoft_dotted = msymbol ("soft-dotted");
  Mcase_mapping = msymbol ("case-mapping");
  Mblock = msymbol ("block");
  Mscript = msymbol ("script");

  return 0;
}

void
mchar__fini (void)
{
  MPlist *p;

  if (char_prop_list)
    {
      for (p = char_prop_list; mplist_key (p) != Mnil; p = mplist_next (p))
	{
	  MCharPropRecord *record = mplist_value (p);

	  if (record->table)
	    {
	      if (record->type == Mstring)
		mchartable_map (record->table, NULL, free_string, NULL);
	      M17N_OBJECT_UNREF (record->table);
	    }
	  free (record);
	}
      M17N_OBJECT_UNREF (char_prop_list);
    }
}

void
mchar__define_prop (MSymbol key, MSymbol type, void *mdb)
{
  MCharPropRecord *record;

  if (char_prop_list)
    record = mplist_get (char_prop_list, key);
  else
    char_prop_list = mplist (), record = NULL;
  if (record)
    {
      if (record->table)
	M17N_OBJECT_UNREF (record->table);
    }
  else
    {
      MSTRUCT_CALLOC (record, MERROR_CHAR);
      mplist_put (char_prop_list, key, record);
    }

  record->type = type;
  record->mdb = mdb;
  if (mdb)
    {
      record->table = NULL;
    }
  else
    {
      void *default_value = NULL;

      if (type == Minteger)
	default_value = (void *) -1;
      record->table = mchartable (type, default_value);
    }
}


/*** @} */
#endif /* !FOR_DOXYGEN || DOXYGEN_INTERNAL_MODULE */

/* External API */

/*** @addtogroup m17nCharacter
     @{ */
/*=*/

#ifdef FOR_DOXYGEN
/***en
    @brief Maximum character code.

    The macro #MCHAR_MAX gives the maximum character code.  */

/***ja
    @brief ʸɤκ.

    ޥ #MCHAR_MAX ʸɤκͤɽ  */

#define MCHAR_MAX
/*=*/
#endif /* FOR_DOXYGEN */

/***en
    @name Variables: Keys of character properties

    These symbols are used as keys of character properties.  */

/***ja
    @ingroup m17nCharacter
    @name ѿ: ʸץѥƥΥ

    ΥܥʸץѥƥΥȤƻȤ롣*/

/*=*/
/*** @{ */

/***en
    @brief Key for script.

    The symbol #Mscript has the name <tt>"script"</tt> and is used as the key
    of a character property.  The value of such a property is a symbol
    representing the script to which the character belongs.

    Each symbol that represents a script has one of the names listed in
    the <em>Unicode Technical Report #24</em>.  */

/***ja
    @brief ץȤɽ魯.

    ܥ #Mscript  <tt>"script"</tt> 
    Ȥ̾ʸץѥƥΥȤƻȤ롣
    Υץѥƥͤϡʸ°륹ץȤɽ魯ܥǤ롣

    ץȤɽ魯ܥ̾ϡ<em>Unicode Technical Report
    #24</em> ˥ꥹȤƤΤΤ줫Ǥ롣  */

MSymbol Mscript;

/*=*/

/***en
    @brief Key for character name.

    The symbol #Mname has the name <tt>"name"</tt> and is used as
    the key of a character property.  The value of such a property is a
    C-string representing the name of the character.  */

/***ja
    @brief ̾ɽ魯.

    ܥ #Mname  <tt>"name"</tt> 
    Ȥ̾ʸץѥƥΥȤƻȤ롣
    ΥץѥƥͤϤʸ̾ɽ魯 C ʸǤ롣  */

MSymbol Mname;

/*=*/

/***en
    @brief Key for general category.

    The symbol #Mcategory has the name <tt>"category"</tt> and is
    used as the key of a character property.  The value of such a
    property is a symbol representing the <em>general category</em> of
    the character.

    Each symbol that represents a general category has one of the
    names listed as abbreviations for <em>General Category</em> in
    Unicode.  */

/***ja
    @brief ̥ƥɽ魯.

    ܥ #Mcategory  <tt>"category"</tt> 
    Ȥ̾ʸץѥƥΥȤƻȤ롣
    Υץѥƥͤϡб <em>̥ƥ</em> ɽ魯ܥǤ롣

    ̥ƥɽ魯ܥ̾ϡ<em>General Category</em> 
    ξάȤ Unicode ƤΤǤ롣  */

MSymbol Mcategory;

/*=*/

/***en
    @brief Key for canonical combining class.

    The symbol #Mcombining_class has the name
    <tt>"combining-class"</tt> and is used as the key of a character
    property.  The value of such a property is an integer that
    represents the <em>canonical combining class</em> of the character.

    The meaning of each integer that represents a canonical combining
    class is identical to the one defined in Unicode.  */

/***ja
    @brief ɸ祯饹ɽ魯.

    ܥ #Mcombining_class  <tt>"combining-class"</tt> 
    Ȥ̾ʸץѥƥΥȤƻȤ롣
    Υץѥƥͤϡб @e ɸ祯饹 ɽ魯Ǥ롣

    ɸ祯饹ɽ魯ΰ̣ϡUnicode 
    ƤΤƱǤ롣  */

MSymbol Mcombining_class;
/*=*/

/***en
    @brief Key for bidi category.

    The symbol #Mbidi_category has the name <tt>"bidi-category"</tt>
    and is used as the key of a character property.  The value of such
    a property is a symbol that represents the <em>bidirectional
    category</em> of the character.

    Each symbol that represents a bidirectional category has one of
    the names listed as types of <em>Bidirectional Category</em> in
    Unicode.  */

/***ja
    @brief ƥɽ魯.

    ܥ #Mbidi_category  <tt>"bidi-category"</tt> 
    Ȥ̾ʸץѥƥΥȤƻȤ롣
    Υץѥƥͤϡб @e ƥ ɽ魯ܥǤ롣

    ƥɽ魯ܥ̾ϡ<em>Bidirectional
    Category</em> ηȤ Unicode ƤΤǤ롣  */

MSymbol Mbidi_category;
/*=*/

/***en
    @brief Key for corresponding single lowercase character.

    The symbol #Msimple_case_folding has the name
    <tt>"simple-case-folding"</tt> and is used as the key of a
    character property.  The value of such a property is the
    corresponding single lowercase character that is used when
    comparing M-texts ignoring cases.

    If a character requires a complicated comparison (i.e. cannot be
    compared by simply mapping to another single character), the value
    of such a property is @c 0xFFFF.  In this case, the character has
    another property whose key is #Mcomplicated_case_folding.  */

/***ja
    @brief б뾮ʸʸɽ魯.

    ܥ #Msimple_case_folding  <tt>"simple-case-folding"</tt> 
    Ȥ̾ʸץѥƥΥȤƻȤ롣
    Υץѥƥͤϡб뾮ʸʸǤꡢʸʸζ̵̤뤷ʸӤκݤ˻Ȥ롣

    ʣˡɬפȤʸǤä
    ̤ΰʸбդ뤳ȤˤäӤǤʤˡΥץѥƥͤ 
    @c 0xFFFF ˤʤ롣ξ礽ʸϡ#Mcomplicated_case_folding
    Ȥʸץѥƥġ  */

MSymbol Msimple_case_folding;
/*=*/

/***en
    @brief Key for corresponding multiple lowercase characters.

    The symbol #Mcomplicated_case_folding has the name
    <tt>"complicated-case-folding"</tt> and is used as the key of a
    character property.  The value of such a property is the
    corresponding M-text that contains a sequence of lowercase
    characters to be used for comparing M-texts ignoring case.  */

/***ja
    @brief б뾮ʸɽ魯.

    ܥ #Mcomplicated_case_folding  
    <tt>"complicated-case-folding"</tt> 
    Ȥ̾ʸץѥƥΥȤƻȤ롣
    Υץѥƥͤϡб뾮ʸ󤫤ʤ M-text Ǥꡢʸʸζ̵̤뤷ʸӤκݤ˻
      */

MSymbol Mcomplicated_case_folding;
/*=*/

/***en
    @brief Key for values used in case operation.

    The symbol #Mcased has the name <tt>"cased"</tt> and is used as
    the key of charater property.  The value of such a property is an
    integer value 1, 2, or 3 representing "cased", "case-ignorable",
    and both of them respective.  See the Unicode Standard 5.0
    (Section 3.13 Default Case Algorithm) for the detail.
 */

/***ja
    @brief Case ѤͤΥ.

    ܥ #Mcased ϡ<tt>"cased"</tt> Ȥ̾ʸץ
    ƥΥȤƻȤ롣Υץѥƥͤ 1, 2, 3 Τ
    줫Ǥꡢ줾 "cased", "case-ignorable", ξ̣롣
    ܺ٤ˤĤƤϡthe Unicode Standard 5.0 (Section 3.13 Default
    Case Algorithm) ȡ
 */
MSymbol Mcased;

/*=*/
/***en
    @brief Key for values used in case operation.

    The symbol #Msoft_dotted has the name <tt>"soft-dotted"</tt> and
    is used as the key of charater property.  The value of such a
    property is #Mt if a character has "Soft_Dotted" property, and
    #Mnil otherwise.  See the Unicode Standard 5.0 (Section 3.13
    Default Case Algorithm) for the detail.  */

/***ja
    @brief Case ѤͤΥ.

    ܥ #Msoft_dotted ϡ<tt>"soft-dotted"</tt> Ȥ̾
    ʸץѥƥΥȤƻȤ롣Υץѥƥͤϡʸ
    "Soft_Dotted"ץѥƥľˤ #Mt, Ǥʤ #Mnil 
    롣 ܺ٤ˤĤƤϡthe Unicode Standard 5.0 (Section 3.13
    Default Case Algorithm) ȡ
 */
MSymbol Msoft_dotted;

/*=*/
/***en 
    @brief Key for values used in case operation.

    The symbol #Mcase_mapping has the name <tt>"case-mapping"</tt> and
    is used as the key of charater property.  The value of such a
    property is a plist of three M-Texts; lower, title, and upper of
    the corresponding character.  See the Unicode Standard 5.0
    (Section 5.18 Case Mappings) for the detail.  */

/***ja
    @brief Case ѤͤΥ.

    ܥ #Mcase_mapping ϡ<tt>"case-mapping"</tt> Ȥ̾
    ʸץѥƥΥȤƻȤ롣Υץѥƥͤϡ
     M-textʤʸ lower, title,  upperʤ plist
    Ǥ롣 ܺ٤ˤĤƤϡthe Unicode Standard 5.0 (Section 3.13
    Default Case Algorithm) ȡ
*/
MSymbol Mcase_mapping;

/*=*/
/***en 
    @brief Key for script block name.

    The symbol #Mblock the name <tt>"block"</tt> and is used as the
    key of charater property.  The value of such a property is a
    symbol representing a script block of the corresponding
    character.  */
/***ja
    @brief ץȥ֥å̾ɽ.

    ܥ #Mblock ϡ<tt>"block"</tt> Ȥ̾ʸץ
    ƥΥȤƻȤ롣ΥץѥƥͤϡʸΥ
    ȥ֥å̾ɽܥǤ롣*/
MSymbol Mblock;

/*** @} */
/*=*/

/***en
    @brief Define a character property.

    The mchar_define_property () function searches the m17n database
    for a data whose tags are \<#Mchar_table, $TYPE, $SYM \>.
    Here, $SYM is a symbol whose name is $NAME.  $TYPE must be
    #Mstring, #Mtext, #Msymbol, #Minteger, or #Mplist.

    @return
    If the operation was successful, mchar_define_property () returns
    $SYM.  Otherwise it returns #Mnil.  */

/***ja
    @brief ʸץѥƥ.

    ؿ mchar_define_property () ϡ \<#Mchar_table, $TYPE, $SYM \>
    Ȥäǡ١ m17n ١õ  
     $SYM  $NAME Ȥ̾ΥܥǤ롣$TYPE #Mstring,
    #Mtext, #Msymbol, #Minteger, #Mplist Τ줫ǤʤФʤʤ

    @return
     mchar_define_property () $SYM ֤
    Ԥ #Mnil ֤  */

/***
    @errors
    @c MERROR_DB

    @seealso
    mchar_get_prop (), mchar_put_prop ()  */

MSymbol
mchar_define_property (const char *name, MSymbol type)
{
  MSymbol key = msymbol (name);
  void *mdb;

  mdb = mdatabase_find (Mchar_table, type, key, Mnil);
  if (! mdb)
    return Mnil;
  mchar__define_prop (key, type, mdb);
  return key;
}

/*=*/

/***en
    @brief Get the value of a character property.

    The mchar_get_prop () function searches character $C for the
    character property whose key is $KEY.

    @return
    If the operation was successful, mchar_get_prop () returns the
    value of the character property.  Otherwise it returns @c
    NULL.  */

/***ja
    @brief ʸץѥƥͤ.

    ؿ mchar_get_prop () ϡʸ $C ʸץѥƥΤ 
    $KEY ǤΤõ

    @return
     mchar_get_prop () ϸĤäץѥƥ֤ͤ
    Ԥ @c NULL ֤

    @latexonly \IPAlabel{mchar_get_prop} @endlatexonly
*/
/***
    @errors
    @c MERROR_SYMBOL, @c MERROR_DB

    @seealso
    mchar_define_property (), mchar_put_prop ()  */

void *
mchar_get_prop (int c, MSymbol key)
{
  MCharPropRecord *record;

  if (! char_prop_list)
    return NULL;
  record = mplist_get (char_prop_list, key);
  if (! record)
    return NULL;
  if (record->mdb)
    {
      record->table = mdatabase_load (record->mdb);
      if (! record->table)
	MERROR (MERROR_DB, NULL);
      record->mdb = NULL;
    }
  return mchartable_lookup (record->table, c);
}

/*=*/

/***en
    @brief Set the value of a character property.

    The mchar_put_prop () function searches character $C for the
    character property whose key is $KEY and assigns $VAL to the value
    of the found property.

    @return
    If the operation was successful, mchar_put_prop () returns 0.
    Otherwise, it returns -1.  */
/***ja
    @brief ʸץѥƥͤꤹ.

    ؿ mchar_put_prop () ϡʸ $C ʸץѥƥΤ $KEY 
    ǤΤõͤȤ $VAL ꤹ롣

    @return
     mchar_put_prop () 0֤Ԥ-1֤  */
/***
    @errors
    @c MERROR_SYMBOL, @c MERROR_DB

    @seealso
    mchar_define_property (), mchar_get_prop ()   */

int
mchar_put_prop (int c, MSymbol key, void *val)
{
  MCharPropRecord *record;

  if (! char_prop_list)
    MERROR (MERROR_CHAR, -1);
  record = mplist_get (char_prop_list, key);
  if (! record)
    return -1;
  if (record->mdb)
    {
      record->table = mdatabase_load (record->mdb);
      if (! record->table)
	MERROR (MERROR_DB, -1);
      record->mdb = NULL;
    }
  return mchartable_set (record->table, c, val);
}

/*=*/

/***en
    @brief Get the char-table for a character property.

    The mchar_get_prop_table () function returns a char-table that
    contains the character property whose key is $KEY.  If $TYPE is
    not NULL, this function stores the type of the property in the
    place pointed by $TYPE.  See mchar_define_property () for types of
    character property.

    @return
    If $KEY is a valid character property key, this function returns a
    char-table.  Otherwise NULL is retuned.  */

/***ja
    @brief ʸץѥƥʸơ֥.

    ؿ mchar_get_prop_table () ϡ $KEY Ǥʸץѥƥ
    ޤʸơ֥֤⤷ $TYPE  NULL ǤʤС $TYPE 
    ؤˤʸΥץѥƥǼ롣ʸץѥƥμ
    ˴ؤƤ mchar_define_property () 򸫤衣

    @return
    ⤷ $KEY ʸץѥƥΥǤСʸơ֥뤬֤
    롣Ǥʤ NULL ֤롣  */

MCharTable *
mchar_get_prop_table (MSymbol key, MSymbol *type)
{
  MCharPropRecord *record;

  if (! char_prop_list)
    return NULL;
  record = mplist_get (char_prop_list, key);
  if (! record)
    return NULL;
  if (record->mdb)
    {
      record->table = mdatabase_load (record->mdb);
      if (! record->table)
	MERROR (MERROR_DB, NULL);
      record->mdb = NULL;
    }
  if (type)
    *type = record->type;
  return record->table;
}

/*** @} */

/*
  Local Variables:
  coding: euc-japan
  End:
*/