unicode.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 "version.h"
  29. #include "unicode.h"
  30. SCM_DEFINE (scm_char_to_formal_name, "char->formal-name", 1, 0, 0,
  31. (SCM ch),
  32. "Return the formal all-upper-case unicode name of @var{ch},\n"
  33. "as a string. If the character has no name, return @code{#f}.")
  34. #define FUNC_NAME s_scm_char_to_formal_name
  35. {
  36. char buf[UNINAME_MAX + 1];
  37. SCM_VALIDATE_CHAR (1, ch);
  38. memset(buf, 0, UNINAME_MAX + 1);
  39. if (unicode_character_name (SCM_CHAR (ch), buf))
  40. return scm_from_latin1_string (buf);
  41. return SCM_BOOL_F;
  42. }
  43. #undef FUNC_NAME
  44. SCM_DEFINE (scm_formal_name_to_char, "formal-name->char", 1, 0, 0,
  45. (SCM name),
  46. "Return the character whose formal all-upper-case unicode name is\n"
  47. "@var{name}, or @code{#f} if no such character is known.")
  48. #define FUNC_NAME s_scm_formal_name_to_char
  49. {
  50. char *c_name;
  51. scm_t_wchar ret;
  52. SCM_VALIDATE_STRING (1, name);
  53. c_name = scm_to_latin1_string (name);
  54. ret = unicode_name_character (c_name);
  55. free (c_name);
  56. return ret == UNINAME_INVALID ? SCM_BOOL_F : SCM_MAKE_CHAR (ret);
  57. }
  58. #undef FUNC_NAME
  59. static void
  60. scm_load_unicode (void)
  61. {
  62. #ifndef SCM_MAGIC_SNARFER
  63. #include "unicode.x"
  64. #endif
  65. }
  66. void
  67. scm_init_unicode (void)
  68. {
  69. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  70. "scm_init_unicode",
  71. (scm_t_extension_init_func)scm_load_unicode,
  72. NULL);
  73. }