]> wimlib.net Git - wimlib/blob - include/wimlib/encoding.h
header cleanups
[wimlib] / include / wimlib / encoding.h
1 #ifndef _WIMLIB_ENCODING_H
2 #define _WIMLIB_ENCODING_H
3
4 #include <string.h>
5
6 #include "wimlib/error.h"
7 #include "wimlib/util.h"
8 #include "wimlib/types.h"
9
10 extern void
11 iconv_global_init(void);
12
13 extern void
14 iconv_global_cleanup(void);
15
16 extern void
17 init_upcase(void);
18
19 extern bool wimlib_mbs_is_utf8;
20
21 #define DECLARE_CHAR_CONVERSION_FUNCTIONS(varname1, varname2,           \
22                                           chartype1, chartype2)         \
23                                                                         \
24 extern int                                                              \
25 varname1##_to_##varname2(const chartype1 *in, size_t in_nbytes,         \
26                          chartype2 **out_ret,                           \
27                          size_t *out_nbytes_ret);                       \
28                                                                         \
29 extern int                                                              \
30 varname1##_to_##varname2##_nbytes(const chartype1 *in, size_t in_nbytes,\
31                                   size_t *out_nbytes_ret);              \
32                                                                         \
33 extern int                                                              \
34 varname1##_to_##varname2##_buf(const chartype1 *in, size_t in_nbytes,   \
35                                chartype2 *out);
36
37 extern utf16lechar *
38 utf16le_dupz(const void *ustr, size_t usize);
39
40 #if !TCHAR_IS_UTF16LE
41 DECLARE_CHAR_CONVERSION_FUNCTIONS(utf16le, tstr, utf16lechar, tchar);
42 DECLARE_CHAR_CONVERSION_FUNCTIONS(tstr, utf16le, tchar, utf16lechar);
43 #else
44
45 static inline int
46 tstr_to_utf16le(const tchar *tstr, size_t tsize,
47                 utf16lechar **ustr_ret, size_t *usize_ret)
48 {
49         utf16lechar *ustr = utf16le_dupz(tstr, tsize);
50         if (!ustr)
51                 return WIMLIB_ERR_NOMEM;
52         *ustr_ret = ustr;
53         *usize_ret = tsize;
54         return 0;
55 }
56
57 #define utf16le_to_tstr tstr_to_utf16le
58
59 #endif
60
61 DECLARE_CHAR_CONVERSION_FUNCTIONS(utf8, tstr, char, tchar);
62 DECLARE_CHAR_CONVERSION_FUNCTIONS(tstr, utf8, tchar, char);
63
64 extern int
65 utf8_to_tstr_simple(const char *utf8str, tchar **out);
66
67 extern int
68 tstr_to_utf8_simple(const tchar *tstr, char **out);
69
70 extern int
71 cmp_utf16le_strings(const utf16lechar *s1, size_t n1,
72                     const utf16lechar *s2, size_t n2,
73                     bool ignore_case);
74
75 /* Convert a string in the platform-dependent encoding to UTF-16LE, but if both
76  * encodings are UTF-16LE, simply re-use the string.  Release with
77  * tstr_put_utf16le() when done.  */
78 static inline int
79 tstr_get_utf16le_and_len(const tchar *tstr,
80                          const utf16lechar **ustr_ret, size_t *usize_ret)
81 {
82         size_t tsize = tstrlen(tstr) * sizeof(tchar);
83 #if TCHAR_IS_UTF16LE
84         /* No conversion or copy needed  */
85         *ustr_ret = tstr;
86         *usize_ret = tsize;
87         return 0;
88 #else
89         return tstr_to_utf16le(tstr, tsize, (utf16lechar **)ustr_ret, usize_ret);
90 #endif
91 }
92
93 /* Convert a string in the platform-dependent encoding to UTF-16LE, but if both
94  * encodings are UTF-16LE, simply re-use the string.  Release with
95  * tstr_put_utf16le() when done.  */
96 static inline int
97 tstr_get_utf16le(const tchar *tstr, const utf16lechar **ustr_ret)
98 {
99 #if TCHAR_IS_UTF16LE
100         /* No conversion or copy needed  */
101         *ustr_ret = tstr;
102         return 0;
103 #else
104         size_t tsize = tstrlen(tstr) * sizeof(tchar);
105         size_t dummy;
106         return tstr_to_utf16le(tstr, tsize, (utf16lechar **)ustr_ret, &dummy);
107 #endif
108 }
109
110 /* Release a string acquired with tstr_get_utf16le() or
111  * tstr_get_utf16le_and_len().  */
112 static inline void
113 tstr_put_utf16le(const utf16lechar *ustr)
114 {
115 #if !TCHAR_IS_UTF16LE
116         FREE((void *)ustr);
117 #endif
118 }
119
120 /* Convert a UTF16-LE string to the platform-dependent encoding, but if both
121  * encodings are UTF-16LE, simply re-use the string.  Release with
122  * utf16le_put_tstr() when done.  */
123 static inline int
124 utf16le_get_tstr(const utf16lechar *ustr, size_t usize,
125                  const tchar **tstr_ret, size_t *tsize_ret)
126 {
127 #if TCHAR_IS_UTF16LE
128         /* No conversion or copy needed  */
129         *tstr_ret = ustr;
130         *tsize_ret = usize;
131         return 0;
132 #else
133         return utf16le_to_tstr(ustr, usize, (tchar **)tstr_ret, tsize_ret);
134 #endif
135 }
136
137 /* Release a string acquired with utf16le_get_tstr().  */
138 static inline void
139 utf16le_put_tstr(const tchar *tstr)
140 {
141 #if !TCHAR_IS_UTF16LE
142         FREE((void *)tstr);
143 #endif
144 }
145
146 #endif /* _WIMLIB_ENCODING_H */