1 /* Copyright 2014,2018
2 Free Software Foundation, Inc.
3
4 This file is part of Guile.
5
6 Guile is free software: you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Guile is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with Guile. If not, see
18 <https://www.gnu.org/licenses/>. */
19
20
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <ctype.h>
27 #include <limits.h>
28 #include <string.h>
29 #include <unicase.h>
30 #include <unictype.h>
31 #include <uniname.h>
32
33 #include "chars.h"
34 #include "extensions.h"
35 #include "gsubr.h"
36 #include "strings.h"
37 #include "version.h"
38
39 #include "unicode.h"
40
41
42
43
44 SCM_DEFINE (scm_char_to_formal_name, "char->formal-name", 1, 0, 0,
45 (SCM ch),
46 "Return the formal all-upper-case unicode name of @var{ch},\n"
47 "as a string. If the character has no name, return @code{#f}.")
48 #define FUNC_NAME s_scm_char_to_formal_name
49 {
50 char buf[UNINAME_MAX + 1];
51
52 SCM_VALIDATE_CHAR (1, ch);
53
54 memset(buf, 0, UNINAME_MAX + 1);
55
56 if (unicode_character_name (SCM_CHAR (ch), buf))
57 return scm_from_latin1_string (buf);
58
59 return SCM_BOOL_F;
60 }
61 #undef FUNC_NAME
62
63 SCM_DEFINE (scm_formal_name_to_char, "formal-name->char", 1, 0, 0,
64 (SCM name),
65 "Return the character whose formal all-upper-case unicode name is\n"
66 "@var{name}, or @code{#f} if no such character is known.")
67 #define FUNC_NAME s_scm_formal_name_to_char
68 {
69 char *c_name;
70 scm_t_wchar ret;
71
72 SCM_VALIDATE_STRING (1, name);
73
74 c_name = scm_to_latin1_string (name);
75 ret = unicode_name_character (c_name);
76 free (c_name);
77
78 return ret == UNINAME_INVALID ? SCM_BOOL_F : SCM_MAKE_CHAR (ret);
79 }
80 #undef FUNC_NAME
81
82 static void
scm_load_unicode(void)83 scm_load_unicode (void)
84 {
85 #ifndef SCM_MAGIC_SNARFER
86 #include "unicode.x"
87 #endif
88 }
89
90 void
scm_init_unicode(void)91 scm_init_unicode (void)
92 {
93 scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
94 "scm_init_unicode",
95 (scm_t_extension_init_func)scm_load_unicode,
96 NULL);
97 }
98