Back to index

php5  5.3.10
intl_convert.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | This source file is subject to version 3.01 of the PHP license,      |
00006    | that is bundled with this package in the file LICENSE, and is        |
00007    | available through the world-wide-web at the following url:           |
00008    | http://www.php.net/license/3_01.txt                                  |
00009    | If you did not receive a copy of the PHP license and are unable to   |
00010    | obtain it through the world-wide-web, please send a note to          |
00011    | license@php.net so we can mail you a copy immediately.               |
00012    +----------------------------------------------------------------------+
00013    | Authors: Vadim Savchuk <vsavchuk@productengine.com>                  |
00014    |          Dmitry Lakhtyuk <dlakhtyuk@productengine.com>               |
00015    +----------------------------------------------------------------------+
00016  */
00017 
00018 #ifdef HAVE_CONFIG_H
00019 #include "config.h"
00020 #endif
00021 
00022 #include <php.h>
00023 
00024 #include "intl_common.h"
00025 #include "intl_convert.h"
00026 
00027 /* {{{ intl_convert_utf8_to_utf16
00028  * Convert given string from UTF-8 to UTF-16 to *target buffer.
00029  *
00030  * It *target is NULL then we allocate a large enough buffer,
00031  * store the converted string into it, and make target point to it.
00032  *
00033  * Otherwise, if *target is non-NULL, we assume that it points to a
00034  * dynamically allocated buffer of *target_len bytes length.
00035  * In this case the buffer will be used to store the converted string to,
00036  * and may be resized (made larger) if needed.
00037  *
00038  * @param target      Where to place the result.
00039  * @param target_len  Result length.
00040  * @param source      String to convert.
00041  * @param source_len  Length of the source string.
00042  * @param status      Conversion status.
00043  *
00044  * @return void       This function does not return anything.
00045  */
00046 void intl_convert_utf8_to_utf16(
00047        UChar**     target, int* target_len,
00048        const char* src,    int  src_len,
00049        UErrorCode* status )
00050 {
00051        UChar*      dst_buf = NULL;
00052        int32_t     dst_len = 0;
00053 
00054        /* If *target is NULL determine required destination buffer size (pre-flighting).
00055         * Otherwise, attempt to convert source string; if *target buffer is not large enough
00056         * it will be resized appropriately.
00057         */
00058        *status = U_ZERO_ERROR;
00059 
00060        u_strFromUTF8( *target, *target_len, &dst_len, src, src_len, status );
00061 
00062        if( *status == U_ZERO_ERROR )
00063        {
00064               /* String is converted successfuly */
00065               (*target)[dst_len] = 0;
00066               *target_len = dst_len;
00067               return;
00068        }
00069 
00070        /* Bail out if an unexpected error occured.
00071         * (U_BUFFER_OVERFLOW_ERROR means that *target buffer is not large enough).
00072         * (U_STRING_NOT_TERMINATED_WARNING usually means that the input string is empty).
00073         */
00074        if( *status != U_BUFFER_OVERFLOW_ERROR && *status != U_STRING_NOT_TERMINATED_WARNING )
00075               return;
00076 
00077        /* Allocate memory for the destination buffer (it will be zero-terminated). */
00078        dst_buf = eumalloc( dst_len + 1 );
00079 
00080        /* Convert source string from UTF-8 to UTF-16. */
00081        *status = U_ZERO_ERROR;
00082        u_strFromUTF8( dst_buf, dst_len+1, NULL, src, src_len, status );
00083        if( U_FAILURE( *status ) )
00084        {
00085               efree( dst_buf );
00086               return;
00087        }
00088 
00089        dst_buf[dst_len] = 0;
00090 
00091        if( *target )
00092               efree( *target );
00093 
00094        *target     = dst_buf;
00095        *target_len = dst_len;
00096 }
00097 /* }}} */
00098 
00099 /* {{{ intl_convert_utf16_to_utf8
00100  * Convert given string from UTF-16 to UTF-8.
00101  *
00102  * @param target      Where to place the result.
00103  * @param target_len  Result length.
00104  * @param source      String to convert.
00105  * @param source_len  Length of the source string.
00106  * @param status      Conversion status.
00107  *
00108  * @return void       This function does not return anything.
00109  */
00110 void intl_convert_utf16_to_utf8(
00111        char**       target, int* target_len,
00112        const UChar* src,    int  src_len,
00113        UErrorCode*  status )
00114 {
00115        char*       dst_buf = NULL;
00116        int32_t     dst_len;
00117 
00118        /* Determine required destination buffer size (pre-flighting). */
00119        *status = U_ZERO_ERROR;
00120        u_strToUTF8( NULL, 0, &dst_len, src, src_len, status );
00121 
00122        /* Bail out if an unexpected error occured.
00123         * (U_BUFFER_OVERFLOW_ERROR means that *target buffer is not large enough).
00124         * (U_STRING_NOT_TERMINATED_WARNING usually means that the input string is empty).
00125         */
00126        if( *status != U_BUFFER_OVERFLOW_ERROR && *status != U_STRING_NOT_TERMINATED_WARNING )
00127               return;
00128 
00129        /* Allocate memory for the destination buffer (it will be zero-terminated). */
00130        dst_buf = emalloc( dst_len+1 );
00131 
00132        /* Convert source string from UTF-8 to UTF-16. */
00133        *status = U_ZERO_ERROR;
00134        u_strToUTF8( dst_buf, dst_len, NULL, src, src_len, status );
00135        if( U_FAILURE( *status ) )
00136        {
00137               efree( dst_buf );
00138               return;
00139        }
00140 
00141        /* U_STRING_NOT_TERMINATED_WARNING is OK for us => reset 'status'. */
00142        *status = U_ZERO_ERROR;
00143 
00144        dst_buf[dst_len] = 0;
00145        *target     = dst_buf;
00146        *target_len = dst_len;
00147 }
00148 /* }}} */
00149 
00150 /*
00151  * Local variables:
00152  * tab-width: 4
00153  * c-basic-offset: 4
00154  * End:
00155  * vim600: noet sw=4 ts=4 fdm=marker
00156  * vim<600: noet sw=4 ts=4
00157  */