🛠️🐜 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.

96 lines
2.5 KiB

  1. /*
  2. Copyright (C) 1997-2023 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 main(int argc, char *argv[])
  28. {
  29. const char *formats[] = {
  30. "UTF8",
  31. "UTF-8",
  32. "UTF16BE",
  33. "UTF-16BE",
  34. "UTF16LE",
  35. "UTF-16LE",
  36. "UTF32BE",
  37. "UTF-32BE",
  38. "UTF32LE",
  39. "UTF-32LE",
  40. "UCS4",
  41. "UCS-4",
  42. };
  43. char *fname;
  44. char buffer[BUFSIZ];
  45. char *ucs4;
  46. char *test[2];
  47. int i;
  48. FILE *file;
  49. int errors = 0;
  50. /* Enable standard application logging */
  51. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  52. fname = GetResourceFilename(argc > 1 ? argv[1] : NULL, "utf8.txt");
  53. file = fopen(fname, "rb");
  54. if (file == NULL) {
  55. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to open %s\n", fname);
  56. return 1;
  57. }
  58. SDL_free(fname);
  59. while (fgets(buffer, sizeof(buffer), file)) {
  60. /* Convert to UCS-4 */
  61. size_t len;
  62. ucs4 =
  63. SDL_iconv_string("UCS-4", "UTF-8", buffer,
  64. SDL_strlen(buffer) + 1);
  65. len = (widelen(ucs4) + 1) * 4;
  66. for (i = 0; i < SDL_arraysize(formats); ++i) {
  67. test[0] = SDL_iconv_string(formats[i], "UCS-4", ucs4, len);
  68. test[1] = SDL_iconv_string("UCS-4", formats[i], test[0], len);
  69. if (!test[1] || SDL_memcmp(test[1], ucs4, len) != 0) {
  70. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "FAIL: %s\n", formats[i]);
  71. ++errors;
  72. }
  73. SDL_free(test[0]);
  74. SDL_free(test[1]);
  75. }
  76. test[0] = SDL_iconv_string("UTF-8", "UCS-4", ucs4, len);
  77. SDL_free(ucs4);
  78. (void)fputs(test[0], stdout);
  79. SDL_free(test[0]);
  80. }
  81. (void)fclose(file);
  82. SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Total errors: %d\n", errors);
  83. return errors ? errors + 1 : 0;
  84. }