MeVisLab Toolbox Reference
mlConvertUTF.h
Go to the documentation of this file.
1 /*
4  * Copyright 2001 Unicode, Inc.
5  *
6  * Disclaimer
7  *
8  * This source code is provided as is by Unicode, Inc. No claims are
9  * made as to fitness for any particular purpose. No warranties of any
10  * kind are expressed or implied. The recipient agrees to determine
11  * applicability of information provided. If this file has been
12  * purchased on magnetic or optical media from Unicode, Inc., the
13  * sole remedy for any claim will be exchange of defective media
14  * within 90 days of receipt.
15  *
16  * Limitations on Rights to Redistribute This Code
17  *
18  * Unicode, Inc. hereby grants the right to freely use the information
19  * supplied in this file in the creation of products supporting the
20  * Unicode Standard, and to make copies of this file in any form
21  * for internal or external distribution as long as this notice
22  * remains attached.
23  */
24 
25 /* ---------------------------------------------------------------------
26 
27  Conversions between UTF32, UTF-16, and UTF-8. Header file.
28 
29  Several functions are included here, forming a complete set of
30  conversions between the three formats. UTF-7 is not included
31  here, but is handled in a separate source file.
32 
33  Each of these routines takes pointers to input buffers and output
34  buffers. The input buffers are const.
35 
36  Each routine converts the text between *sourceStart and sourceEnd,
37  putting the result into the buffer between *targetStart and
38  targetEnd. Note: the end pointers are *after* the last item: e.g.,
39  *(sourceEnd - 1) is the last item.
40 
41  The return result indicates whether the conversion was successful,
42  and if not, whether the problem was in the source or target buffers.
43  (Only the first encountered problem is indicated.)
44 
45  After the conversion, *sourceStart and *targetStart are both
46  updated to point to the end of last text successfully converted in
47  the respective buffers.
48 
49  General (parameter) errors:
50  - If any illegal parameter (that means NULL pointers) is passed
51  then in cases of sourceStart, sourceEnd or length "sourceIllegal"
52  is returned. \n
53  In case of NULL targetStart or targetEnd then "targetExhausted" is
54  returned. \n
55  None of any parameter passed to those functions will
56  be changed in those cases.
57  - In cases of caught exceptions "sourceIllegal" or "false" will be
58  returned after sending a fatal error message to the ML error handler.
59  The state of the target buffers or return pointers is left
60  undefined then.
61 
62  Input parameters:
63  sourceStart - pointer to a pointer to the source buffer.
64  The contents of this are modified on return so that
65  it points at the next thing to be converted.
66  targetStart - similarly, pointer to pointer to the target buffer.
67  sourceEnd, targetEnd - respectively pointers to the ends of the
68  two buffers, for overflow checking only.
69 
70  These conversion functions take a ConversionFlags argument. When this
71  flag is set to strict, both irregular sequences and isolated surrogates
72  will cause an error. When the flag is set to lenient, both irregular
73  sequences and isolated surrogates are converted.
74 
75  Whether the flag is strict or lenient, all illegal sequences will cause
76  an error return. This includes sequences such as: <F4 90 80 80>, <C0 80>,
77  or <A0> in UTF-8, and values above 0x10FFFF in UTF-32. Conform code
78  must check for illegal sequences.
79 
80  When the flag is set to lenient, characters over 0x10FFFF are converted
81  to the replacement character; otherwise (when the flag is set to strict)
82  they constitute an error.
83 
84  Output parameters:
85  The value "sourceIllegal" is returned from some routines if the input
86  sequence is malformed. When "sourceIllegal" is returned, the source
87  value will point to the illegal value that caused the problem. E.g.,
88  in UTF-8 when a sequence is malformed, it points to the start of the
89  malformed sequence.
90 
91  Author: Mark E. Davis, 1994.
92  Rev History: Rick McGowan, fixes & updates May 2001.
93  Fixes & updates, Sept 2001.
94 
95 ------------------------------------------------------------------------ */
96 
97 //--------------------------------------------------------------------
113 //--------------------------------------------------------------------
114 
115 #ifndef __mlConvertUTF_H
116 #define __mlConvertUTF_H
117 
118 #include "mlTypeDefs.h"
119 
120 //--------------------------------------------------------------------
126 //--------------------------------------------------------------------
127 typedef MLuint32 UTF32;
128 typedef MLuint16 UTF16;
129 typedef MLuint8 UTF8;
130 
131 
132 //--------------------------------------------------------------------
134 //--------------------------------------------------------------------
135 #define UNI_REPLACEMENT_CHAR (static_cast<UTF32>(0x0000FFFD))
136 #define UNI_MAX_BMP (static_cast<UTF32>(0x0000FFFF))
137 #define UNI_MAX_UTF16 (static_cast<UTF32>(0x0010FFFF))
138 #define UNI_MAX_UTF32 (static_cast<UTF32>(0x7FFFFFFF))
140 
141 //--------------------------------------------------------------------
143 //--------------------------------------------------------------------
144 typedef enum {
150 
151 //--------------------------------------------------------------------
153 //--------------------------------------------------------------------
154 typedef enum {
158 
159 //--------------------------------------------------------------------
160 /* This is for C++ and does no harm in C */
161 //--------------------------------------------------------------------
162 #ifdef __cplusplus
163 extern "C" {
164 #endif
165 
166 //---------------------------------------
169 //---------------------------------------
170 
175  const UTF32 *sourceEnd,
176  UTF16 **targetStart,
177  UTF16 *targetEnd,
178  ConversionFlags flags);
179 
184  const UTF16 *sourceEnd,
185  UTF32 **targetStart,
186  UTF32 *targetEnd,
187  ConversionFlags flags);
188 
193  const UTF16 *sourceEnd,
194  UTF8 **targetStart,
195  UTF8 *targetEnd,
196  ConversionFlags flags);
197 
202  const UTF8 *sourceEnd,
203  UTF16 **targetStart,
204  UTF16 *targetEnd,
205  ConversionFlags flags);
206 
211  const UTF32 *sourceEnd,
212  UTF8 **targetStart,
213  UTF8 *targetEnd,
214  ConversionFlags flags);
215 
220  const UTF8 *sourceEnd,
221  UTF32 **targetStart,
222  UTF32 *targetEnd);
223 
224 
225 
226 //---------------------------------------
229 //---------------------------------------
235  char *targetStart,
236  char *targetEnd,
237  ConversionFlags flags);
238 
245  unsigned int *length,
246  ConversionFlags flags);
247 
255  unsigned int *length,
256  ConversionFlags flags);
258 
259 
260 
261 #ifdef __cplusplus
262 }
263 #endif
264 
265 /* --------------------------------------------------------------------- */
266 
267 #endif // __mlConvertUTF_H
268 
269 
ConversionResult ConvertUTF32toUTF16(const UTF32 **sourceStart, const UTF32 *sourceEnd, UTF16 **targetStart, UTF16 *targetEnd, ConversionFlags flags)
Converts UTF32 string to UTF16 string.
MLuint16 UTF16
at least 16 bits
Definition: mlConvertUTF.h:128
ConversionFlags
Enum to describe conversion strictness.
Definition: mlConvertUTF.h:154
@ strictConversion
Strict conversion, error on conversion problem.
Definition: mlConvertUTF.h:155
@ lenientConversion
Non-strict conversion, use '?' or UNI_REPLACEMENT_CHAR instead.
Definition: mlConvertUTF.h:156
ConversionResult ConvertUTF8toLatin1(const UTF8 *sourceStart, char *targetStart, char *targetEnd, ConversionFlags flags)
Convert UTF8 to latin1 chars.
ConversionResult ConvertUTF8toUTF16(const UTF8 **sourceStart, const UTF8 *sourceEnd, UTF16 **targetStart, UTF16 *targetEnd, ConversionFlags flags)
Converts UTF8 string to UTF16 string.
ConversionResult CalculateNumCharsInUTF8(const UTF8 *sourceStart, unsigned int *length, ConversionFlags flags)
Calculate the number of chars in an UTF8 string (may be less than strlen, because of multibyte encode...
MLuint8 UTF8
typically 8 bits
Definition: mlConvertUTF.h:129
ConversionResult ConvertUTF8toUTF32(const UTF8 **sourceStart, const UTF8 *sourceEnd, UTF32 **targetStart, UTF32 *targetEnd)
Converts UTF8 string to UTF32 string.
ConversionResult CalculateUTF16BufferSizeForUTF8(const UTF8 *sourceStart, unsigned int *length, ConversionFlags flags)
Calculate the number of chars required in UTF16 encoding to fit the given UTF8 string (may be less th...
ConversionResult ConvertUTF16toUTF8(const UTF16 **sourceStart, const UTF16 *sourceEnd, UTF8 **targetStart, UTF8 *targetEnd, ConversionFlags flags)
Converts UTF16 string to UTF8 string.
MLuint32 UTF32
Note : This file has been adapted to the purpose of ML and MeVisLab usage.
Definition: mlConvertUTF.h:127
ConversionResult ConvertUTF32toUTF8(const UTF32 **sourceStart, const UTF32 *sourceEnd, UTF8 **targetStart, UTF8 *targetEnd, ConversionFlags flags)
Converts UTF32 string to UTF8 string.
ConversionResult ConvertUTF16toUTF32(const UTF16 **sourceStart, const UTF16 *sourceEnd, UTF32 **targetStart, UTF32 *targetEnd, ConversionFlags flags)
Converts UTF16 string to UTF32 string.
ConversionResult
Enum to describe conversion results.
Definition: mlConvertUTF.h:144
@ targetExhausted
insuff. room in target for conversion
Definition: mlConvertUTF.h:147
@ sourceIllegal
source sequence is illegal/malformed
Definition: mlConvertUTF.h:148
@ conversionOK
conversion successful
Definition: mlConvertUTF.h:145
@ sourceExhausted
partial character in source, but hit end
Definition: mlConvertUTF.h:146
unsigned int MLuint32
Definition: mlTypeDefs.h:191
unsigned char MLuint8
Definition: mlTypeDefs.h:115
unsigned short MLuint16
Definition: mlTypeDefs.h:148