2012-03-20 03:17:37 +01:00
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
2019-05-01 12:30:17 +02:00
|
|
|
* of the License, or (at your option) any later version.
|
2012-03-20 03:17:37 +01:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
2012-04-12 04:37:28 +02:00
|
|
|
* The Original Code is Copyright (C) 2012 Blender Foundation.
|
2012-03-20 03:17:37 +01:00
|
|
|
* All rights reserved.
|
2019-05-01 12:30:17 +02:00
|
|
|
*
|
2012-03-20 03:17:37 +01:00
|
|
|
*/
|
|
|
|
|
2013-03-29 07:25:22 +01:00
|
|
|
#ifndef __UTFCONV_H__
|
|
|
|
#define __UTFCONV_H__
|
|
|
|
|
2012-03-20 03:17:37 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2020-03-19 09:33:03 +01:00
|
|
|
#include <wchar.h>
|
2012-03-20 03:17:37 +01:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
2012-04-12 04:15:33 +02:00
|
|
|
extern "C" {
|
2012-03-20 03:17:37 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
2019-11-24 14:55:11 +01:00
|
|
|
* Counts how many bytes is required for future utf-8 string using utf-16
|
2019-09-05 04:56:55 +02:00
|
|
|
* \param string16: pointer to working utf-16 string
|
2021-03-11 14:46:41 +01:00
|
|
|
* \return How many bytes must be allocated including NULL.
|
2012-03-20 03:17:37 +01:00
|
|
|
*/
|
2012-04-12 04:15:33 +02:00
|
|
|
size_t count_utf_8_from_16(const wchar_t *string16);
|
2012-03-20 03:17:37 +01:00
|
|
|
|
|
|
|
/**
|
2019-11-24 14:55:11 +01:00
|
|
|
* Counts how many wchar_t (two byte) is required for future utf-16 string using utf-8
|
2019-09-05 04:56:55 +02:00
|
|
|
* \param string8: pointer to working utf-8 string
|
2021-03-11 14:46:41 +01:00
|
|
|
* \return How many bytes must be allocated including NULL.
|
2012-03-20 03:17:37 +01:00
|
|
|
*/
|
2012-04-12 04:15:33 +02:00
|
|
|
size_t count_utf_16_from_8(const char *string8);
|
2012-03-20 03:17:37 +01:00
|
|
|
|
2021-03-11 14:46:41 +01:00
|
|
|
/*
|
2012-03-20 03:17:37 +01:00
|
|
|
* conv_utf_*** errors
|
|
|
|
*/
|
2021-03-11 14:46:41 +01:00
|
|
|
|
|
|
|
/** Error occurs when required parameter is missing. */
|
|
|
|
#define UTF_ERROR_NULL_IN (1 << 0)
|
|
|
|
/** Error if character is in illegal UTF range. */
|
|
|
|
#define UTF_ERROR_ILLCHAR (1 << 1)
|
|
|
|
/** Passed size is to small. It gives legal string with character missing at the end. */
|
|
|
|
#define UTF_ERROR_SMALL (1 << 2)
|
|
|
|
/** Error if sequence is broken and doesn't finish. */
|
|
|
|
#define UTF_ERROR_ILLSEQ (1 << 3)
|
2012-03-20 03:17:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts utf-16 string to allocated utf-8 string
|
2019-09-05 04:56:55 +02:00
|
|
|
* \param in16: utf-16 string to convert
|
|
|
|
* \param out8: utf-8 string to string the conversion
|
|
|
|
* \param size8: the allocated size in bytes of out8
|
2021-03-11 14:46:41 +01:00
|
|
|
* \return Returns any errors occurred during conversion. See the block above,
|
2012-03-20 03:17:37 +01:00
|
|
|
*/
|
2012-04-12 04:15:33 +02:00
|
|
|
int conv_utf_16_to_8(const wchar_t *in16, char *out8, size_t size8);
|
2012-03-20 03:17:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts utf-8 string to allocated utf-16 string
|
2019-09-05 04:56:55 +02:00
|
|
|
* \param in8: utf-8 string to convert
|
|
|
|
* \param out16: utf-16 string to string the conversion
|
|
|
|
* \param size16: the allocated size in wchar_t (two byte) of out16
|
2021-03-11 14:46:41 +01:00
|
|
|
* \return Returns any errors occurred during conversion. See the block above,
|
2012-03-20 03:17:37 +01:00
|
|
|
*/
|
2012-04-12 04:15:33 +02:00
|
|
|
int conv_utf_8_to_16(const char *in8, wchar_t *out16, size_t size16);
|
2012-03-20 03:17:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocates and converts the utf-8 string from utf-16
|
2019-09-05 04:56:55 +02:00
|
|
|
* \param in16: utf-16 string to convert
|
|
|
|
* \param add: any additional size which will be allocated for new utf-8 string in bytes
|
|
|
|
* \return New allocated and converted utf-8 string or NULL if in16 is 0.
|
2012-03-20 03:17:37 +01:00
|
|
|
*/
|
2012-04-12 04:15:33 +02:00
|
|
|
char *alloc_utf_8_from_16(const wchar_t *in16, size_t add);
|
2012-03-20 03:17:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocates and converts the utf-16 string from utf-8
|
2019-09-05 04:56:55 +02:00
|
|
|
* \param in8: utf-8 string to convert
|
|
|
|
* \param add: any additional size which will be allocated for new utf-16 string
|
2019-05-01 12:30:17 +02:00
|
|
|
* in wchar_t (two bytes)
|
2019-09-05 04:56:55 +02:00
|
|
|
* \return New allocated and converted utf-16 string or NULL if in8 is 0.
|
2012-03-20 03:17:37 +01:00
|
|
|
*/
|
2012-04-12 04:15:33 +02:00
|
|
|
wchar_t *alloc_utf16_from_8(const char *in8, size_t add);
|
2012-03-20 03:17:37 +01:00
|
|
|
|
2019-05-01 12:30:17 +02:00
|
|
|
/* Easy allocation and conversion of new utf-16 string. New string has _16 suffix.
|
|
|
|
* Must be deallocated with UTF16_UN_ENCODE in right order. */
|
2012-04-12 04:15:33 +02:00
|
|
|
#define UTF16_ENCODE(in8str) \
|
|
|
|
if (1) { \
|
2017-02-06 10:44:25 +01:00
|
|
|
wchar_t *in8str##_16 = alloc_utf16_from_8((const char *)in8str, 0)
|
2012-03-20 03:17:37 +01:00
|
|
|
|
|
|
|
#define UTF16_UN_ENCODE(in8str) \
|
2012-11-03 16:35:03 +01:00
|
|
|
free(in8str##_16); \
|
|
|
|
} \
|
|
|
|
(void)0
|
2012-03-20 03:17:37 +01:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
2012-03-24 02:42:08 +01:00
|
|
|
#endif
|
2013-03-29 07:25:22 +01:00
|
|
|
|
|
|
|
#endif /* __UTFCONV_H__ */
|