unicode.c 2.4 KB

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