Qore Programming Language - C/C++ Library  0.8.12.3
QoreEncoding.h
Go to the documentation of this file.
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreEncoding.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2003 - 2015 David Nichols
8 
9  Permission is hereby granted, free of charge, to any person obtaining a
10  copy of this software and associated documentation files (the "Software"),
11  to deal in the Software without restriction, including without limitation
12  the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  and/or sell copies of the Software, and to permit persons to whom the
14  Software is furnished to do so, subject to the following conditions:
15 
16  The above copyright notice and this permission notice shall be included in
17  all copies or substantial portions of the Software.
18 
19  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  DEALINGS IN THE SOFTWARE.
26 
27  Note that the Qore library is released under a choice of three open-source
28  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
29  information.
30 */
31 
32 #ifndef _QORE_ENCODING_H
33 
34 #define _QORE_ENCODING_H
35 
41 #include <qore/common.h>
42 #include <qore/QoreThreadLock.h>
43 
44 #include <strings.h>
45 #include <string.h>
46 
47 #include <map>
48 
49 #include <string>
50 
52 typedef qore_size_t (*mbcs_length_t)(const char* str, const char* end, bool &invalid);
53 
55 typedef qore_size_t (*mbcs_end_t)(const char* str, const char* end, qore_size_t num_chars, bool &invalid);
56 
58 typedef qore_size_t (*mbcs_pos_t)(const char* str, const char* ptr, bool &invalid);
59 
61 
65 typedef qore_size_t (*mbcs_charlen_t)(const char* str, qore_size_t valid_len);
66 
68 typedef unsigned (*mbcs_get_unicode_t)(const char* p);
69 
70 // private implementation of the QoreEncoding class
71 struct qore_encoding_private;
72 
74 
85 class QoreEncoding {
86 protected:
87  // FIXME: move all this to the private implementation with the ABI change
88  // NOTE: the following class members cannot be removed because until Qore 0.8.12 this class implemented inline member functions
89  // that refered directly to these member variables, therefore they make up a part of the library's ABI :(
90  std::string code;
91  std::string desc;
92  mbcs_length_t flength;
93  mbcs_end_t fend;
94  mbcs_pos_t fpos;
95  mbcs_charlen_t fcharlen;
96  unsigned char maxwidth;
97 
98  qore_encoding_private* priv;
99 
100 public:
101  DLLLOCAL QoreEncoding(const char* n_code, const char* n_desc = 0, unsigned char n_minwidth = 1, unsigned char n_maxwidth = 1, mbcs_length_t l = 0, mbcs_end_t e = 0, mbcs_pos_t p = 0, mbcs_charlen_t c = 0, mbcs_get_unicode_t gu = 0, bool n_ascii_compat = true);
102 
103  DLLLOCAL ~QoreEncoding();
104 
106 
111  DLLEXPORT qore_size_t getLength(const char* p, const char* end, bool& invalid) const;
112 
114 
119  DLLEXPORT qore_size_t getLength(const char* p, const char* end, ExceptionSink* xsink) const;
120 
122 
128  DLLEXPORT qore_size_t getByteLen(const char* p, const char* end, qore_size_t c, bool& invalid) const;
129 
131 
137  DLLEXPORT qore_size_t getByteLen(const char* p, const char* end, qore_size_t c, ExceptionSink* xsink) const;
138 
140 
145  DLLEXPORT qore_size_t getCharPos(const char* p, const char* end, bool& invalid) const;
146 
148 
153  DLLEXPORT qore_size_t getCharPos(const char* p, const char* end, ExceptionSink* xsink) const;
154 
156 
161  DLLEXPORT qore_size_t getCharLen(const char* p, qore_size_t valid_len) const;
162 
164  DLLEXPORT bool isMultiByte() const;
165 
167  DLLEXPORT const char* getCode() const;
168 
170  DLLEXPORT const char* getDesc() const;
171 
173  DLLEXPORT int getMaxCharWidth() const;
174 
176 
178  DLLEXPORT unsigned getMinCharWidth() const;
179 
181 
183  DLLEXPORT bool isAsciiCompat() const;
184 
186 
189  DLLLOCAL unsigned getUnicode(const char* p) const;
190 };
191 
192 // case-insensitive maps for encodings
193 typedef std::map<const char*, QoreEncoding*, ltcstrcase> encoding_map_t;
194 typedef std::map<const char*, const QoreEncoding*, ltcstrcase> const_encoding_map_t;
195 
196 class QoreString;
197 
199 
202 private:
203  DLLLOCAL static encoding_map_t emap;
204  DLLLOCAL static const_encoding_map_t amap;
205  DLLLOCAL static QoreThreadLock mutex;
206 
207  DLLLOCAL static const QoreEncoding* addUnlocked(const char* n_code, const char* n_desc = 0, unsigned char n_minwidth = 1, unsigned char n_maxwidth = 1, mbcs_length_t l = 0, mbcs_end_t e = 0, mbcs_pos_t p = 0, mbcs_charlen_t c = 0, mbcs_get_unicode_t gu = 0, bool n_ascii_compat = true);
208  DLLLOCAL static const QoreEncoding* findUnlocked(const char* name);
209 
210 public:
212  DLLEXPORT static void addAlias(const QoreEncoding* qcs, const char* alias);
213 
215  DLLEXPORT static const QoreEncoding* findCreate(const char* name);
216 
218  DLLEXPORT static const QoreEncoding* findCreate(const QoreString* str);
219 
221  DLLEXPORT static void showEncodings();
222 
224  DLLEXPORT static void showAliases();
225 
227  DLLEXPORT static const QoreEncoding* add(const char* code, const char* desc = 0, unsigned char maxwidth = 1, mbcs_length_t l = 0, mbcs_end_t e = 0, mbcs_pos_t p = 0, mbcs_charlen_t = 0);
228 
229  DLLLOCAL static void init(const char* def);
230  DLLLOCAL QoreEncodingManager();
231  DLLLOCAL ~QoreEncodingManager();
232 };
233 
234 DLLEXPORT qore_size_t q_get_byte_len(const QoreEncoding* enc, const char* p, const char* end, qore_size_t c, ExceptionSink* xsink);
235 DLLEXPORT qore_size_t q_get_char_len(const QoreEncoding* enc, const char* p, qore_size_t valid_len, ExceptionSink* xsink);
236 
238 DLLEXPORT extern QoreEncodingManager QEM;
239 
240 // builtin character encodings
241 DLLEXPORT extern const QoreEncoding* QCS_DEFAULT,
242  *QCS_USASCII,
243  *QCS_UTF8,
244  *QCS_UTF16,
245  *QCS_UTF16BE,
246  *QCS_UTF16LE,
247  *QCS_ISO_8859_1,
248  *QCS_ISO_8859_2,
249  *QCS_ISO_8859_3,
250  *QCS_ISO_8859_4,
251  *QCS_ISO_8859_5,
252  *QCS_ISO_8859_6,
253  *QCS_ISO_8859_7,
254  *QCS_ISO_8859_8,
255  *QCS_ISO_8859_9,
256  *QCS_ISO_8859_10,
257  *QCS_ISO_8859_11,
258  *QCS_ISO_8859_13,
259  *QCS_ISO_8859_14,
260  *QCS_ISO_8859_15,
261  *QCS_ISO_8859_16,
262  *QCS_KOI8_R,
263  *QCS_KOI8_U,
264  *QCS_KOI7;
265 
266 #endif // _QORE_ENCODING_H
qore_size_t(* mbcs_end_t)(const char *str, const char *end, qore_size_t num_chars, bool &invalid)
for multi-byte character set encodings: gives the number of bytes for the number of chars ...
Definition: QoreEncoding.h:55
DLLEXPORT const QoreEncoding * QCS_UTF8
UTF-8 multi-byte encoding (only UTF-8 and UTF-16 are multi-byte encodings)
DLLEXPORT qore_size_t getLength(const char *p, const char *end, bool &invalid) const
gives the length of the string in characters
defines string encoding functions in Qore
Definition: QoreEncoding.h:85
DLLEXPORT const QoreEncoding * QCS_ISO_8859_8
Hebrew character set.
DLLEXPORT const QoreEncoding * QCS_ISO_8859_1
latin-1, Western European encoding
DLLEXPORT const QoreEncoding * QCS_UTF16
UTF-16 (only UTF-8 and UTF-16* are multi-byte encodings) - do not use; use UTF-8 instead.
DLLEXPORT const QoreEncoding * QCS_DEFAULT
the default encoding for the Qore library
DLLEXPORT qore_size_t getCharLen(const char *p, qore_size_t valid_len) const
gives the number of total bytes for the character given one or more characters
DLLEXPORT int getMaxCharWidth() const
returns the maximum character width in bytes for the encoding
static DLLEXPORT void addAlias(const QoreEncoding *qcs, const char *alias)
adds an alias for an encoding
qore_size_t(* mbcs_charlen_t)(const char *str, qore_size_t valid_len)
for multi-byte encodings: gives the number of total bytes for the character given one or more charact...
Definition: QoreEncoding.h:65
DLLLOCAL unsigned getUnicode(const char *p) const
returns the unicode code point for the given character, must be a complete character and only one cha...
DLLEXPORT qore_size_t getByteLen(const char *p, const char *end, qore_size_t c, bool &invalid) const
gives the number of bytes for the number of chars in the string or up to the end of the string ...
DLLEXPORT const QoreEncoding * QCS_ISO_8859_2
latin-2, Central European encoding
DLLEXPORT const QoreEncoding * QCS_ISO_8859_11
Thai character set.
manages encodings in Qore
Definition: QoreEncoding.h:201
DLLEXPORT const QoreEncoding * QCS_ISO_8859_3
latin-3, Southern European character set
size_t qore_size_t
used for sizes (same range as a pointer)
Definition: common.h:71
DLLEXPORT const QoreEncoding * QCS_ISO_8859_4
latin-4, Northern European character set
static DLLEXPORT void showEncodings()
prints out all valid encodings to stdout
DLLEXPORT const QoreEncoding * QCS_USASCII
ascii encoding
qore_size_t(* mbcs_pos_t)(const char *str, const char *ptr, bool &invalid)
for multi-byte character set encodings: gives the character position of the ptr
Definition: QoreEncoding.h:58
unsigned(* mbcs_get_unicode_t)(const char *p)
for multi-byte non-ascii compatible character encodings: returns the unicode code point for the given...
Definition: QoreEncoding.h:68
DLLEXPORT const char * getDesc() const
returns the description for the encoding
DLLEXPORT const QoreEncoding * QCS_ISO_8859_10
latin-6, Nordic character set
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:82
DLLEXPORT const QoreEncoding * QCS_KOI8_U
Ukrainian: Kod Obmena Informatsiey, 8 bit.
DLLEXPORT QoreEncodingManager QEM
the QoreEncodingManager object
DLLEXPORT const QoreEncoding * QCS_ISO_8859_9
latin-5, Turkish character set
static DLLEXPORT void showAliases()
prints out all aliases to stdout
static DLLEXPORT const QoreEncoding * findCreate(const char *name)
finds an encoding if it exists (also looks up against alias names) and creates a new one if it doesn'...
DLLEXPORT const QoreEncoding * QCS_UTF16BE
UTF-16BE (only UTF-8 and UTF-16* are multi-byte encodings) - do not use; use UTF-8 instead...
DLLEXPORT const QoreEncoding * QCS_KOI7
Russian: Kod Obmena Informatsiey, 7 bit characters.
DLLEXPORT bool isAsciiCompat() const
returns true if the character encoding is backwards-compatible with ASCII
qore_size_t(* mbcs_length_t)(const char *str, const char *end, bool &invalid)
for multi-byte character set encodings: gives the length of the string in characters ...
Definition: QoreEncoding.h:52
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:43
DLLEXPORT const QoreEncoding * QCS_ISO_8859_14
latin-8, Celtic character set
DLLEXPORT const QoreEncoding * QCS_ISO_8859_6
Arabic character set.
DLLEXPORT unsigned getMinCharWidth() const
returns the minimum character width in bytes for the encoding
DLLEXPORT const QoreEncoding * QCS_ISO_8859_5
Cyrillic character set.
DLLEXPORT qore_size_t getCharPos(const char *p, const char *end, bool &invalid) const
gives the character position (number of characters) starting from the first pointer to the second ...
provides a mutually-exclusive thread lock
Definition: QoreThreadLock.h:49
DLLEXPORT const QoreEncoding * QCS_UTF16LE
UTF-16LE (only UTF-8 and UTF-16* are multi-byte encodings) - do not use; use UTF-8 instead...
static DLLEXPORT const QoreEncoding * add(const char *code, const char *desc=0, unsigned char maxwidth=1, mbcs_length_t l=0, mbcs_end_t e=0, mbcs_pos_t p=0, mbcs_charlen_t=0)
adds a new encoding to the list
DLLEXPORT const QoreEncoding * QCS_ISO_8859_16
latin-10, Southeast European character set
DLLEXPORT const char * getCode() const
returns the string code (ex: "UTF-8") for the encoding
DLLEXPORT bool isMultiByte() const
returns true if the encoding is a multi-byte encoding
DLLEXPORT const QoreEncoding * QCS_ISO_8859_15
latin-9, Western European with euro symbol
DLLEXPORT const QoreEncoding * QCS_KOI8_R
Russian: Kod Obmena Informatsiey, 8 bit.
DLLEXPORT const QoreEncoding * QCS_ISO_8859_7
Greek character set.
DLLEXPORT const QoreEncoding * QCS_ISO_8859_13
latin-7, Baltic rim character set