1 #include "sysconfig.h"
2 #include "sysdeps.h"
3 
4 #include <string.h>
5 
6 // fs-uae uses only chars / UTF-8 internally, so TCHAR is typedefed to
7 // char (WinUAE uses wchar_t internally).
8 
ua(const TCHAR * s)9 char *ua (const TCHAR *s) {
10 	if (s == NULL) return NULL;
11 	return strdup (s);
12 }
13 
au(const TCHAR * s)14 char *au (const TCHAR *s) {
15 	if (s == NULL) return NULL;
16 	return strdup (s);
17 }
18 
utf8u(const char * s)19 TCHAR* utf8u (const char *s)
20 {
21 	if (s == NULL) return NULL;
22 	return ua (s);
23 }
24 
uutf8(const TCHAR * s)25 char* uutf8 (const TCHAR *s)
26 {
27 	if (s == NULL) return NULL;
28 	return ua (s);
29 }
30 
au_copy(TCHAR * dst,int maxlen,const char * src)31 TCHAR *au_copy (TCHAR *dst, int maxlen, const char *src)
32 {
33 	// this should match the WinUAE au_copy behavior, where either the
34 	// entire string is copied (and null-terminated), or the result is
35 	// an empty string
36 	if (uae_tcslcpy (dst, src, maxlen) >= maxlen) {
37 		dst[0] = '\0';
38 	}
39 	return dst;
40 }
41 
ua_copy(char * dst,int maxlen,const TCHAR * src)42 char *ua_copy (char *dst, int maxlen, const TCHAR *src)
43 {
44 	return au_copy (dst, maxlen, src);
45 }
46 
my_strdup_ansi(const char * src)47 TCHAR *my_strdup_ansi (const char *src)
48 {
49 	return strdup (src);
50 }
51 
52 #define NO_TRANSLATION
53 
au_fs(const char * src)54 TCHAR *au_fs (const char *src) {
55 #ifdef NO_TRANSLATION
56     if (src == NULL) return NULL;
57     return strdup(src);
58 #else
59     gsize read, written;
60     gchar *result = g_convert(src, -1, "UTF-8",
61             "ISO-8859-1", &read, &written, NULL);
62     if (result == NULL) {
63         write_log("WARNING: au_fs_copy failed to convert string %s", src);
64         return strdup("");
65     }
66     return result;
67 #endif
68 }
69 
ua_fs(const TCHAR * s,int defchar)70 char *ua_fs (const TCHAR *s, int defchar) {
71 #ifdef NO_TRANSLATION
72     if (s == NULL) return NULL;
73     return strdup(s);
74 #else
75     // we convert from fs-uae's internal encoding (UTF-8) to latin-1 here,
76     // so file names can be read properly in the amiga
77 
78     char def[] = "?";
79     if (defchar < 128) {
80         def[0] = defchar;
81     }
82 
83     gsize read, written;
84     gchar *result = g_convert_with_fallback(s, -1, "ISO-8859-1",
85             "UTF-8", def, &read, &written, NULL);
86     if (result == NULL) {
87         write_log("WARNING: ua_fs failed to convert string %s", s);
88         return strdup("");
89     }
90 
91     // duplicate with libc malloc
92     char *result_malloced = strdup(result);
93     free(result);
94     return result_malloced;
95 #endif
96 }
97 
au_fs_copy(TCHAR * dst,int maxlen,const char * src)98 TCHAR *au_fs_copy (TCHAR *dst, int maxlen, const char *src) {
99 #ifdef NO_TRANSLATION
100     dst[0] = 0;
101     strncpy(dst, src, maxlen);
102     return dst;
103 #else
104     gsize read, written;
105     gchar *result = g_convert(src, -1, "UTF-8",
106             "ISO-8859-1", &read, &written, NULL);
107     if (result == NULL) {
108         write_log("WARNING: au_fs_copy failed to convert string %s", src);
109         dst[0] = '\0';
110         return dst;
111     }
112 
113     strncpy(dst, result, maxlen);
114     free(result);
115     return dst;
116 #endif
117 }
118 
ua_fs_copy(char * dst,int maxlen,const TCHAR * src,int defchar)119 char *ua_fs_copy (char *dst, int maxlen, const TCHAR *src, int defchar) {
120 #ifdef NO_TRANSLATION
121     dst[0] = 0;
122     strncpy(dst, src, maxlen);
123     return dst;
124 #else
125     char def[] = "?";
126     if (defchar < 128) {
127         def[0] = defchar;
128     }
129 
130     gsize read, written;
131     gchar *result = g_convert_with_fallback(src, -1, "ISO-8859-1",
132             "UTF-8", def, &read, &written, NULL);
133     if (result == NULL) {
134         write_log("WARNING: ua_fs_copy failed to convert string %s", src);
135         dst[0] = '\0';
136         return dst;
137     }
138 
139     strncpy(dst, result, maxlen);
140     free(result);
141     return dst;
142 #endif
143 }
144