[SDL_Tizen] Modify SDL-Tizen docs
[platform/upstream/SDL.git] / test / testiconv.c
1 /*
2   Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
3
4   This software is provided 'as-is', without any express or implied
5   warranty.  In no event will the authors be held liable for any damages
6   arising from the use of this software.
7
8   Permission is granted to anyone to use this software for any purpose,
9   including commercial applications, and to alter it and redistribute it
10   freely.
11 */
12
13 #include <stdio.h>
14
15 #include "SDL.h"
16
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 SDL_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 buffer[BUFSIZ];
45     char *ucs4;
46     char *test[2];
47     int i;
48     FILE *file;
49     int errors = 0;
50
51     /* Enable standard application logging */
52     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
53
54     if (!argv[1]) {
55         argv[1] = "res/utf8.txt";
56     }
57     file = fopen(argv[1], "rb");
58     if (!file) {
59         SDLTest_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to open %s\n", argv[1]);
60         return (1);
61     }
62
63     while (fgets(buffer, sizeof(buffer), file)) {
64         /* Convert to UCS-4 */
65         size_t len;
66         ucs4 =
67             SDL_iconv_string("UCS-4", "UTF-8", buffer,
68                              SDL_strlen(buffer) + 1);
69         len = (widelen(ucs4) + 1) * 4;
70         for (i = 0; i < SDL_arraysize(formats); ++i) {
71             test[0] = SDL_iconv_string(formats[i], "UCS-4", ucs4, len);
72             test[1] = SDL_iconv_string("UCS-4", formats[i], test[0], len);
73             if (!test[1] || SDL_memcmp(test[1], ucs4, len) != 0) {
74                 SDLTest_LogError(SDL_LOG_CATEGORY_APPLICATION, "FAIL: %s\n", formats[i]);
75                 ++errors;
76             }
77             SDL_free(test[0]);
78             SDL_free(test[1]);
79         }
80         test[0] = SDL_iconv_string("UTF-8", "UCS-4", ucs4, len);
81         SDL_free(ucs4);
82         fputs(test[0], stdout);
83         SDL_free(test[0]);
84     }
85     fclose(file);
86     return (errors ? errors + 1 : 0);
87 }