utfconv.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License
  4. * as published by the Free Software Foundation; either version 2
  5. * of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software Foundation,
  14. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. *
  16. * The Original Code is Copyright (C) 2012 Blender Foundation.
  17. * All rights reserved.
  18. *
  19. */
  20. #ifndef __UTFCONV_H__
  21. #define __UTFCONV_H__
  22. #include <wchar.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. /**
  29. * Counts how many bytes is requered for for future utf-8 string using utf-16
  30. * @param string-16 pointer to working utf-16 string
  31. * @return How many bytes must be allocated includeng NULL.
  32. */
  33. size_t count_utf_8_from_16(const wchar_t *string16);
  34. /**
  35. * Counts how many wchar_t (two byte) is requered for for future utf-16 string using utf-8
  36. * @param string-8 pointer to working utf-8 string
  37. * @return How many bytes must be allocated includeng NULL.
  38. */
  39. size_t count_utf_16_from_8(const char *string8);
  40. /**
  41. * conv_utf_*** errors
  42. */
  43. #define UTF_ERROR_NULL_IN 1 << 0 /* Error occures when requered parameter is missing*/
  44. #define UTF_ERROR_ILLCHAR 1 << 1 /* Error if character is in illigal UTF rage*/
  45. #define UTF_ERROR_SMALL \
  46. 1 << 2 /* Passed size is to small. It gives legal string with character missing at the end*/
  47. #define UTF_ERROR_ILLSEQ 1 << 3 /* Error if sequence is broken and doesn't finish*/
  48. /**
  49. * Converts utf-16 string to allocated utf-8 string
  50. * @param in16 utf-16 string to convert
  51. * @param out8 utf-8 string to string the conversion
  52. * @param size8 the allocated size in bytes of out8
  53. * @return Returns any errors occured during conversion. See the block above,
  54. */
  55. int conv_utf_16_to_8(const wchar_t *in16, char *out8, size_t size8);
  56. /**
  57. * Converts utf-8 string to allocated utf-16 string
  58. * @param in8 utf-8 string to convert
  59. * @param out16 utf-16 string to string the conversion
  60. * @param size16 the allocated size in wchar_t (two byte) of out16
  61. * @return Returns any errors occured during conversion. See the block above,
  62. */
  63. int conv_utf_8_to_16(const char *in8, wchar_t *out16, size_t size16);
  64. /**
  65. * Allocates and converts the utf-8 string from utf-16
  66. * @param in16 utf-16 string to convert
  67. * @param add any additional size which will be allocated for new utf-8 string in bytes
  68. * @return New allocated and converted utf-8 string or NULL if in16 is 0.
  69. */
  70. char *alloc_utf_8_from_16(const wchar_t *in16, size_t add);
  71. /**
  72. * Allocates and converts the utf-16 string from utf-8
  73. * @param in8 utf-8 string to convert
  74. * @param add any additional size which will be allocated for new utf-16 string
  75. * in wchar_t (two bytes)
  76. * @return New allocated and converted utf-16 string or NULL if in8 is 0.
  77. */
  78. wchar_t *alloc_utf16_from_8(const char *in8, size_t add);
  79. /* Easy allocation and conversion of new utf-16 string. New string has _16 suffix.
  80. * Must be deallocated with UTF16_UN_ENCODE in right order. */
  81. #define UTF16_ENCODE(in8str) \
  82. if (1) { \
  83. wchar_t *in8str##_16 = alloc_utf16_from_8((const char *)in8str, 0)
  84. #define UTF16_UN_ENCODE(in8str) \
  85. free(in8str##_16); \
  86. } \
  87. (void)0
  88. #ifdef __cplusplus
  89. }
  90. #endif
  91. #endif /* __UTFCONV_H__ */