mswsp: Remove null check (CID1355407)
[jlayton/wireshark.git] / wsutil / unicode-utils.h
1 /* unicode-utils.h
2  * Unicode utility definitions
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 2006 Gerald Combs
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #ifndef __UNICODEUTIL_H__
24 #define __UNICODEUTIL_H__
25
26 #include <config.h>
27
28 #include "ws_symbol_export.h"
29
30 #include <glib.h>
31
32 /**
33  * @file
34  * Unicode convenience routines.
35  */
36
37 #ifdef  __cplusplus
38 extern "C" {
39 #endif
40
41 WS_DLL_PUBLIC
42 int ws_utf8_char_len(guint8 ch);
43
44 #ifdef _WIN32
45
46 #include <windows.h>
47 #include <tchar.h>
48 #include <wchar.h>
49
50 /** Given a UTF-8 string, convert it to UTF-16.  This is meant to be used
51  * to convert between GTK+ 2.x (UTF-8) to Windows (UTF-16).
52  *
53  * @param utf8str The string to convert.  May be NULL.
54  * @return The string converted to UTF-16.  If utf8str is NULL, returns
55  * NULL.  The return value should NOT be freed by the caller.
56  */
57 WS_DLL_PUBLIC
58 const wchar_t * utf_8to16(const char *utf8str);
59
60 /** Create a UTF-16 string (in place) according to the format string.
61  *
62  * @param utf16buf The buffer to return the UTF-16 string in.
63  * @param utf16buf_len The size of the 'utf16buf' parameter
64  * @param fmt A standard g_printf() format string
65  */
66 WS_DLL_PUBLIC
67 void utf_8to16_snprintf(TCHAR *utf16buf, gint utf16buf_len, const gchar* fmt,
68         ...) G_GNUC_PRINTF(3, 4);
69
70 /** Given a UTF-16 string, convert it to UTF-8.  This is meant to be used
71  * to convert between GTK+ 2.x (UTF-8) to Windows (UTF-16).
72  *
73  * @param utf16str The string to convert.  May be NULL.
74  * @return The string converted to UTF-8.  If utf16str is NULL, returns
75  * NULL.  The return value should NOT be freed by the caller.
76  */
77 WS_DLL_PUBLIC
78 gchar * utf_16to8(const wchar_t *utf16str);
79
80 /** Convert the program argument list from UTF-16 to UTF-8 and
81  * store it in the supplied array. This is intended to be used
82  * to normalize command line arguments at program startup.
83  *
84  * @param argc The number of arguments. You should simply pass the
85  * first argument from main().
86  * @param argv The argument values (vector). You should simply pass
87  * the second argument from main().
88  */
89 WS_DLL_PUBLIC
90 void arg_list_utf_16to8(int argc, char *argv[]);
91
92
93 #endif /* _WIN32 */
94
95 /*
96  * defines for helping with UTF-16 surrogate pairs
97  */
98
99 #define IS_LEAD_SURROGATE(uchar2) \
100         ((uchar2) >= 0xd800 && (uchar2) < 0xdc00)
101 #define IS_TRAIL_SURROGATE(uchar2) \
102         ((uchar2) >= 0xdc00 && (uchar2) < 0xe000)
103 #define SURROGATE_VALUE(lead, trail) \
104         (((((lead) - 0xd800) << 10) | ((trail) - 0xdc00)) + 0x100000)
105
106 #ifdef  __cplusplus
107 }
108 #endif
109
110 #endif /* __UNICODEUTIL_H__ */