test-scm-to-latin1-string.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* Copyright 2011,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 <stdlib.h>
  19. #include <string.h>
  20. #include <libguile.h>
  21. /*
  22. This outputs:
  23. dhansen@localhorst ~/tmp $ ./a.out
  24. foo,bar
  25. bar
  26. */
  27. #define TEST(x) \
  28. if (!(x)) abort()
  29. static void
  30. inner_main (void *data, int argc, char **argv)
  31. {
  32. char *cstr;
  33. SCM string, tokens, tok;
  34. string = scm_from_latin1_string ("foo,bar");
  35. tokens = scm_string_split (string, SCM_MAKE_CHAR (','));
  36. TEST (scm_is_pair (tokens));
  37. tok = scm_car (tokens);
  38. TEST (scm_is_string (tok));
  39. cstr = scm_to_latin1_string (tok);
  40. TEST (strcmp (cstr, "foo") == 0);
  41. free (cstr);
  42. tokens = scm_cdr (tokens);
  43. TEST (scm_is_pair (tokens));
  44. tok = scm_car (tokens);
  45. TEST (scm_is_string (tok));
  46. cstr = scm_to_latin1_string (tok);
  47. TEST (strcmp (cstr, "bar") == 0);
  48. free (cstr);
  49. tokens = scm_cdr (tokens);
  50. TEST (scm_is_null (tokens));
  51. }
  52. int
  53. main (int argc, char **argv)
  54. {
  55. scm_boot_guile (argc, argv, inner_main, NULL);
  56. return EXIT_SUCCESS;
  57. }
  58. /* Local Variables: */
  59. /* compile-command: "gcc `pkg-config --cflags --libs guile-2.0` main.c" */
  60. /* End: */