🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.5 KiB

  1. /*
  2. Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. /* quiet windows compiler warnings */
  11. #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
  12. # define _CRT_SECURE_NO_WARNINGS
  13. #endif
  14. #include <stdio.h>
  15. #include "SDL.h"
  16. #include "testutils.h"
  17. static size_t
  18. widelen(char *data)
  19. {
  20. size_t len = 0;
  21. Uint32 *p = (Uint32 *) data;
  22. while (*p++) {
  23. ++len;
  24. }
  25. return len;
  26. }
  27. int
  28. main(int argc, char *argv[])
  29. {
  30. const char *formats[] = {
  31. "UTF8",
  32. "UTF-8",
  33. "UTF16BE",
  34. "UTF-16BE",
  35. "UTF16LE",
  36. "UTF-16LE",
  37. "UTF32BE",
  38. "UTF-32BE",
  39. "UTF32LE",
  40. "UTF-32LE",
  41. "UCS4",
  42. "UCS-4",
  43. };
  44. char * fname;
  45. char buffer[BUFSIZ];
  46. char *ucs4;
  47. char *test[2];
  48. int i;
  49. FILE *file;
  50. int errors = 0;
  51. /* Enable standard application logging */
  52. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  53. fname = GetResourceFilename(argc > 1 ? argv[1] : NULL, "utf8.txt");
  54. file = fopen(fname, "rb");
  55. if (!file) {
  56. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to open %s\n", fname);
  57. return (1);
  58. }
  59. SDL_free(fname);
  60. while (fgets(buffer, sizeof(buffer), file)) {
  61. /* Convert to UCS-4 */
  62. size_t len;
  63. ucs4 =
  64. SDL_iconv_string("UCS-4", "UTF-8", buffer,
  65. SDL_strlen(buffer) + 1);
  66. len = (widelen(ucs4) + 1) * 4;
  67. for (i = 0; i < SDL_arraysize(formats); ++i) {
  68. test[0] = SDL_iconv_string(formats[i], "UCS-4", ucs4, len);
  69. test[1] = SDL_iconv_string("UCS-4", formats[i], test[0], len);
  70. if (!test[1] || SDL_memcmp(test[1], ucs4, len) != 0) {
  71. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "FAIL: %s\n", formats[i]);
  72. ++errors;
  73. }
  74. SDL_free(test[0]);
  75. SDL_free(test[1]);
  76. }
  77. test[0] = SDL_iconv_string("UTF-8", "UCS-4", ucs4, len);
  78. SDL_free(ucs4);
  79. fputs(test[0], stdout);
  80. SDL_free(test[0]);
  81. }
  82. fclose(file);
  83. SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Total errors: %d\n", errors);
  84. return (errors ? errors + 1 : 0);
  85. }