Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | #ifndef __PJ_UNICODE_H__ |
| 20 | #define __PJ_UNICODE_H__ |
| 21 | |
| 22 | #include <pj/types.h> |
| 23 | |
Benny Prijono | 6139641 | 2006-02-14 21:03:15 +0000 | [diff] [blame] | 24 | |
Benny Prijono | 294c253 | 2006-06-16 16:52:51 +0000 | [diff] [blame] | 25 | /** |
| 26 | * @defgroup PJ_UNICODE Unicode Support |
| 27 | * @ingroup PJ_MISC |
| 28 | * @{ |
| 29 | */ |
| 30 | |
Benny Prijono | 6139641 | 2006-02-14 21:03:15 +0000 | [diff] [blame] | 31 | PJ_BEGIN_DECL |
| 32 | |
| 33 | |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame] | 34 | /** |
| 35 | * @file unicode.h |
| 36 | * @brief Provides Unicode conversion for Unicode OSes |
| 37 | */ |
| 38 | |
| 39 | /** |
| 40 | * Convert ANSI strings to Unicode strings. |
| 41 | * |
| 42 | * @param str The ANSI string to be converted. |
| 43 | * @param len The length of the input string. |
| 44 | * @param wbuf Buffer to hold the Unicode string output. |
| 45 | * @param wbuf_count Buffer size, in number of elements (not bytes). |
| 46 | * |
| 47 | * @return The Unicode string, NULL terminated. |
| 48 | */ |
| 49 | PJ_DECL(wchar_t*) pj_ansi_to_unicode(const char *str, pj_size_t len, |
| 50 | wchar_t *wbuf, pj_size_t wbuf_count); |
| 51 | |
| 52 | |
| 53 | /** |
| 54 | * Convert Unicode string to ANSI string. |
| 55 | * |
| 56 | * @param wstr The Unicode string to be converted. |
| 57 | * @param len The length of the input string. |
| 58 | * @param buf Buffer to hold the ANSI string output. |
| 59 | * @param buf_size Size of the output buffer. |
| 60 | * |
| 61 | * @return The ANSI string, NULL terminated. |
| 62 | */ |
| 63 | PJ_DECL(char*) pj_unicode_to_ansi(const wchar_t *wstr, pj_size_t len, |
| 64 | char *buf, pj_size_t buf_size); |
| 65 | |
| 66 | |
| 67 | #if defined(PJ_NATIVE_STRING_IS_UNICODE) && PJ_NATIVE_STRING_IS_UNICODE!=0 |
| 68 | |
Benny Prijono | 294c253 | 2006-06-16 16:52:51 +0000 | [diff] [blame] | 69 | /** |
| 70 | * This macro is used to declare temporary Unicode buffer for ANSI to |
| 71 | * Unicode conversion, and should be put in declaration section of a block. |
| 72 | * When PJ_NATIVE_STRING_IS_UNICODE macro is not defined, this |
| 73 | * macro will expand to nothing. |
| 74 | */ |
Benny Prijono | 6139641 | 2006-02-14 21:03:15 +0000 | [diff] [blame] | 75 | # define PJ_DECL_UNICODE_TEMP_BUF(buf,size) wchar_t buf[size]; |
Benny Prijono | 294c253 | 2006-06-16 16:52:51 +0000 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * This macro will convert ANSI string to native, when the platform's |
| 79 | * native string is Unicode (PJ_NATIVE_STRING_IS_UNICODE is non-zero). |
| 80 | */ |
Benny Prijono | 6139641 | 2006-02-14 21:03:15 +0000 | [diff] [blame] | 81 | # define PJ_STRING_TO_NATIVE(s,buf,max) pj_ansi_to_unicode( \ |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame] | 82 | s, strlen(s), \ |
Benny Prijono | 6139641 | 2006-02-14 21:03:15 +0000 | [diff] [blame] | 83 | buf, max) |
Benny Prijono | 294c253 | 2006-06-16 16:52:51 +0000 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * This macro is used to declare temporary ANSI buffer for Unicode to |
| 87 | * ANSI conversion, and should be put in declaration section of a block. |
| 88 | * When PJ_NATIVE_STRING_IS_UNICODE macro is not defined, this |
| 89 | * macro will expand to nothing. |
| 90 | */ |
Benny Prijono | 6139641 | 2006-02-14 21:03:15 +0000 | [diff] [blame] | 91 | # define PJ_DECL_ANSI_TEMP_BUF(buf,size) char buf[size]; |
Benny Prijono | 294c253 | 2006-06-16 16:52:51 +0000 | [diff] [blame] | 92 | |
| 93 | |
| 94 | /** |
| 95 | * This macro will convert Unicode string to ANSI, when the platform's |
| 96 | * native string is Unicode (PJ_NATIVE_STRING_IS_UNICODE is non-zero). |
| 97 | */ |
Benny Prijono | 6139641 | 2006-02-14 21:03:15 +0000 | [diff] [blame] | 98 | # define PJ_NATIVE_TO_STRING(cs,buf,max) pj_unicode_to_ansi( \ |
| 99 | cs, wcslen(cs), \ |
| 100 | buf, max) |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame] | 101 | |
| 102 | #else |
| 103 | |
Benny Prijono | 294c253 | 2006-06-16 16:52:51 +0000 | [diff] [blame] | 104 | /** |
| 105 | * This macro is used to declare temporary Unicode buffer for ANSI to |
| 106 | * Unicode conversion, and should be put in declaration section of a block. |
| 107 | * When PJ_NATIVE_STRING_IS_UNICODE macro is not defined, this |
| 108 | * macro will expand to nothing. |
| 109 | */ |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame] | 110 | # define PJ_DECL_UNICODE_TEMP_BUF(var,size) |
Benny Prijono | 294c253 | 2006-06-16 16:52:51 +0000 | [diff] [blame] | 111 | /** |
| 112 | * This macro will convert ANSI string to native, when the platform's |
| 113 | * native string is Unicode (PJ_NATIVE_STRING_IS_UNICODE is non-zero). |
| 114 | */ |
Benny Prijono | 6139641 | 2006-02-14 21:03:15 +0000 | [diff] [blame] | 115 | # define PJ_STRING_TO_NATIVE(s,buf,max) ((char*)s) |
Benny Prijono | 294c253 | 2006-06-16 16:52:51 +0000 | [diff] [blame] | 116 | /** |
| 117 | * This macro is used to declare temporary ANSI buffer for Unicode to |
| 118 | * ANSI conversion, and should be put in declaration section of a block. |
| 119 | * When PJ_NATIVE_STRING_IS_UNICODE macro is not defined, this |
| 120 | * macro will expand to nothing. |
| 121 | */ |
Benny Prijono | 6139641 | 2006-02-14 21:03:15 +0000 | [diff] [blame] | 122 | # define PJ_DECL_ANSI_TEMP_BUF(buf,size) |
Benny Prijono | 294c253 | 2006-06-16 16:52:51 +0000 | [diff] [blame] | 123 | /** |
| 124 | * This macro will convert Unicode string to ANSI, when the platform's |
| 125 | * native string is Unicode (PJ_NATIVE_STRING_IS_UNICODE is non-zero). |
| 126 | */ |
Benny Prijono | 6139641 | 2006-02-14 21:03:15 +0000 | [diff] [blame] | 127 | # define PJ_NATIVE_TO_STRING(cs,buf,max) ((char*)(const char*)cs) |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame] | 128 | |
| 129 | #endif |
| 130 | |
| 131 | |
| 132 | |
Benny Prijono | 6139641 | 2006-02-14 21:03:15 +0000 | [diff] [blame] | 133 | PJ_END_DECL |
| 134 | |
Benny Prijono | 294c253 | 2006-06-16 16:52:51 +0000 | [diff] [blame] | 135 | /* |
| 136 | * @} |
| 137 | */ |
| 138 | |
Benny Prijono | 6139641 | 2006-02-14 21:03:15 +0000 | [diff] [blame] | 139 | |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame] | 140 | #endif /* __PJ_UNICODE_H__ */ |