Loading...
Searching...
No Matches
QoreEncoding.h
Go to the documentation of this file.
/* -*- mode: c++; indent-tabs-mode: nil -*- */
/*
QoreEncoding.h
Qore Programming Language
Copyright (C) 2003 - 2022 Qore Technologies, s.r.o.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
Note that the Qore library is released under a choice of three open-source
licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
information.
*/
#ifndef _QORE_ENCODING_H
#define _QORE_ENCODING_H
#include <qore/common.h>
#include <qore/QoreThreadLock.h>
#include <cstring>
#include <map>
#include <string>
#include <strings.h>
typedef size_t (*mbcs_length_t)(const char* str, const char* end, bool &invalid);
typedef size_t (*mbcs_end_t)(const char* str, const char* end, size_t num_chars, bool &invalid);
typedef size_t (*mbcs_pos_t)(const char* str, const char* ptr, bool &invalid);
typedef qore_offset_t (*mbcs_charlen_t)(const char* str, size_t valid_len);
typedef unsigned (*mbcs_get_unicode_t)(const char* p);
// private implementation of the QoreEncoding class
struct qore_encoding_private;
class QoreEncoding {
friend struct qore_encoding_private;
protected:
qore_encoding_private* priv;
public:
DLLLOCAL QoreEncoding(const char* code, const char* desc = nullptr, unsigned char minwidth = 1,
unsigned char maxwidth = 1, mbcs_length_t l = nullptr, mbcs_end_t e = nullptr, mbcs_pos_t p = nullptr,
mbcs_charlen_t c = nullptr, mbcs_get_unicode_t gu = nullptr, bool ascii_compat = true);
DLLLOCAL ~QoreEncoding();
DLLEXPORT size_t getLength(const char* p, const char* end, bool& invalid) const;
DLLEXPORT size_t getLength(const char* p, const char* end, ExceptionSink* xsink) const;
DLLEXPORT size_t getByteLen(const char* p, const char* end, size_t c, bool& invalid) const;
DLLEXPORT size_t getByteLen(const char* p, const char* end, size_t c, ExceptionSink* xsink) const;
DLLEXPORT size_t getCharPos(const char* p, const char* end, bool& invalid) const;
DLLEXPORT size_t getCharPos(const char* p, const char* end, ExceptionSink* xsink) const;
DLLEXPORT qore_offset_t getCharLen(const char* p, size_t valid_len) const;
DLLEXPORT bool isMultiByte() const;
DLLEXPORT const char* getCode() const;
DLLEXPORT const char* getDesc() const;
DLLEXPORT int getMaxCharWidth() const;
DLLEXPORT unsigned getMinCharWidth() const;
DLLEXPORT bool isAsciiCompat() const;
DLLEXPORT int getUnicode(const char* p, const char* end, unsigned& clen, ExceptionSink* xsink) const;
};
// case-insensitive maps for encodings
typedef std::map<const char*, QoreEncoding*, ltcstrcase> encoding_map_t;
typedef std::map<const char*, const QoreEncoding*, ltcstrcase> const_encoding_map_t;
class QoreString;
class QoreEncodingManager {
private:
DLLLOCAL static encoding_map_t emap;
DLLLOCAL static const_encoding_map_t amap;
DLLLOCAL static QoreThreadLock mutex;
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);
DLLLOCAL static const QoreEncoding* findUnlocked(const char* name);
public:
DLLEXPORT static void addAlias(const QoreEncoding* qcs, const char* alias);
DLLEXPORT static const QoreEncoding* findCreate(const char* name);
DLLEXPORT static const QoreEncoding* findCreate(const QoreString* str);
DLLEXPORT static void showEncodings();
DLLEXPORT static void showAliases();
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);
DLLLOCAL static void init(const char* def);
DLLLOCAL QoreEncodingManager();
DLLLOCAL ~QoreEncodingManager();
};
DLLEXPORT size_t q_get_byte_len(const QoreEncoding* enc, const char* p, const char* end, size_t c, ExceptionSink* xsink);
DLLEXPORT qore_offset_t q_get_char_len(const QoreEncoding* enc, const char* p, size_t valid_len, ExceptionSink* xsink);
DLLEXPORT extern QoreEncodingManager QEM;
// builtin character encodings
DLLEXPORT extern const QoreEncoding* QCS_DEFAULT,
*QCS_USASCII,
*QCS_UTF8,
*QCS_UTF16,
*QCS_UTF16BE,
*QCS_UTF16LE,
*QCS_ISO_8859_1,
*QCS_ISO_8859_2,
*QCS_ISO_8859_3,
*QCS_ISO_8859_4,
*QCS_ISO_8859_5,
*QCS_ISO_8859_6,
*QCS_ISO_8859_7,
*QCS_ISO_8859_8,
*QCS_ISO_8859_9,
*QCS_ISO_8859_10,
*QCS_ISO_8859_11,
*QCS_ISO_8859_13,
*QCS_ISO_8859_14,
*QCS_ISO_8859_15,
*QCS_ISO_8859_16,
*QCS_KOI8_R,
*QCS_KOI8_U,
*QCS_KOI7,
*QCS_WINDOWS_874,
*QCS_WINDOWS_936,
*QCS_WINDOWS_1250,
*QCS_WINDOWS_1251,
*QCS_WINDOWS_1252,
*QCS_WINDOWS_1253,
*QCS_WINDOWS_1254,
*QCS_WINDOWS_1255,
*QCS_WINDOWS_1256,
*QCS_WINDOWS_1257,
*QCS_WINDOWS_1258;
#endif // _QORE_ENCODING_H
QCS_UTF16
DLLEXPORT const QoreEncoding * QCS_UTF16
UTF-16 (only UTF-* are multi-byte encodings)
Definition: QoreEncoding.h:248
QCS_KOI7
DLLEXPORT const QoreEncoding * QCS_KOI7
Russian: Kod Obmena Informatsiey, 7 bit characters.
Definition: QoreEncoding.h:268
mbcs_pos_t
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:56
QCS_KOI8_R
DLLEXPORT const QoreEncoding * QCS_KOI8_R
Russian: Kod Obmena Informatsiey, 8 bit.
Definition: QoreEncoding.h:266
QCS_KOI8_U
DLLEXPORT const QoreEncoding * QCS_KOI8_U
Ukrainian: Kod Obmena Informatsiey, 8 bit.
Definition: QoreEncoding.h:267
QCS_ISO_8859_4
DLLEXPORT const QoreEncoding * QCS_ISO_8859_4
latin-4, Northern European character set
Definition: QoreEncoding.h:254
QCS_ISO_8859_11
DLLEXPORT const QoreEncoding * QCS_ISO_8859_11
Thai character set.
Definition: QoreEncoding.h:261
QCS_WINDOWS_1258
DLLEXPORT const QoreEncoding * QCS_WINDOWS_1258
Windows 1258: Vietnamese.
Definition: QoreEncoding.h:279
QCS_WINDOWS_1251
DLLEXPORT const QoreEncoding * QCS_WINDOWS_1251
Windows 1251: Cyrillic: Russian, Ukrainian, Balarusian, Bulgarian, Serbian Cyrillic,...
Definition: QoreEncoding.h:272
QCS_DEFAULT
DLLEXPORT const QoreEncoding * QCS_DEFAULT
the default encoding for the Qore library
QCS_ISO_8859_15
DLLEXPORT const QoreEncoding * QCS_ISO_8859_15
latin-9, Western European with euro symbol
Definition: QoreEncoding.h:264
QCS_UTF16LE
DLLEXPORT const QoreEncoding * QCS_UTF16LE
UTF-16LE (only UTF-* are multi-byte encodings)
Definition: QoreEncoding.h:250
QCS_ISO_8859_6
DLLEXPORT const QoreEncoding * QCS_ISO_8859_6
Arabic character set.
Definition: QoreEncoding.h:256
QCS_ISO_8859_8
DLLEXPORT const QoreEncoding * QCS_ISO_8859_8
Hebrew character set.
Definition: QoreEncoding.h:258
QCS_UTF16BE
DLLEXPORT const QoreEncoding * QCS_UTF16BE
UTF-16BE (only UTF-* are multi-byte encodings)
Definition: QoreEncoding.h:249
QCS_ISO_8859_9
DLLEXPORT const QoreEncoding * QCS_ISO_8859_9
latin-5, Turkish character set
Definition: QoreEncoding.h:259
QCS_ISO_8859_7
DLLEXPORT const QoreEncoding * QCS_ISO_8859_7
Greek character set.
Definition: QoreEncoding.h:257
QCS_ISO_8859_16
DLLEXPORT const QoreEncoding * QCS_ISO_8859_16
latin-10, Southeast European character set
Definition: QoreEncoding.h:265
QCS_WINDOWS_1252
DLLEXPORT const QoreEncoding * QCS_WINDOWS_1252
Windows 1252: European: Spanish, French, German.
Definition: QoreEncoding.h:273
QCS_WINDOWS_936
DLLEXPORT const QoreEncoding * QCS_WINDOWS_936
Windows 936: Simplified Chinese.
Definition: QoreEncoding.h:270
mbcs_charlen_t
qore_offset_t(* mbcs_charlen_t)(const char *str, 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:63
QCS_UTF8
DLLEXPORT const QoreEncoding * QCS_UTF8
UTF-8 multi-byte encoding (only UTF-8 and UTF-16 are multi-byte encodings)
Definition: QoreEncoding.h:247
QCS_ISO_8859_2
DLLEXPORT const QoreEncoding * QCS_ISO_8859_2
latin-2, Central European encoding
Definition: QoreEncoding.h:252
QCS_ISO_8859_10
DLLEXPORT const QoreEncoding * QCS_ISO_8859_10
latin-6, Nordic character set
Definition: QoreEncoding.h:260
QCS_ISO_8859_1
DLLEXPORT const QoreEncoding * QCS_ISO_8859_1
latin-1, Western European encoding
Definition: QoreEncoding.h:251
QCS_WINDOWS_1255
DLLEXPORT const QoreEncoding * QCS_WINDOWS_1255
Windows 1255: Hebrew.
Definition: QoreEncoding.h:276
QCS_WINDOWS_1253
DLLEXPORT const QoreEncoding * QCS_WINDOWS_1253
Windows 1253: Greek.
Definition: QoreEncoding.h:274
QCS_ISO_8859_5
DLLEXPORT const QoreEncoding * QCS_ISO_8859_5
Cyrillic character set.
Definition: QoreEncoding.h:255
mbcs_end_t
size_t(* mbcs_end_t)(const char *str, const char *end, 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:53
QEM
DLLEXPORT QoreEncodingManager QEM
the QoreEncodingManager object
QCS_ISO_8859_3
DLLEXPORT const QoreEncoding * QCS_ISO_8859_3
latin-3, Southern European character set
Definition: QoreEncoding.h:253
QCS_WINDOWS_1254
DLLEXPORT const QoreEncoding * QCS_WINDOWS_1254
Windows 1254: Turkish.
Definition: QoreEncoding.h:275
QCS_WINDOWS_1257
DLLEXPORT const QoreEncoding * QCS_WINDOWS_1257
Windows 1257: Baltic.
Definition: QoreEncoding.h:278
mbcs_length_t
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:50
QCS_USASCII
DLLEXPORT const QoreEncoding * QCS_USASCII
ascii encoding
Definition: QoreEncoding.h:246
mbcs_get_unicode_t
unsigned(* mbcs_get_unicode_t)(const char *p)
returns the unicode code point for the given character, assumes there is enough data for the characte...
Definition: QoreEncoding.h:66
QCS_WINDOWS_874
DLLEXPORT const QoreEncoding * QCS_WINDOWS_874
Windows 874: Latin/Thai - similar to ISO-8859-11.
Definition: QoreEncoding.h:269
QCS_WINDOWS_1250
DLLEXPORT const QoreEncoding * QCS_WINDOWS_1250
Windows 1250: Central/Eastern European.
Definition: QoreEncoding.h:271
QCS_ISO_8859_14
DLLEXPORT const QoreEncoding * QCS_ISO_8859_14
latin-8, Celtic character set
Definition: QoreEncoding.h:263
QCS_WINDOWS_1256
DLLEXPORT const QoreEncoding * QCS_WINDOWS_1256
Windows 1256: Arabic.
Definition: QoreEncoding.h:277
QCS_ISO_8859_13
DLLEXPORT const QoreEncoding * QCS_ISO_8859_13
latin-7, Baltic rim character set
Definition: QoreEncoding.h:262
ExceptionSink
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
QoreEncoding
defines string encoding functions in Qore
Definition: QoreEncoding.h:83
QoreEncoding::isMultiByte
DLLEXPORT bool isMultiByte() const
returns true if the encoding is a multi-byte encoding
QoreEncoding::getUnicode
DLLEXPORT int getUnicode(const char *p, const char *end, unsigned &clen, ExceptionSink *xsink) const
returns the unicode code point for the given character; if there are any errors (invalid character,...
QoreEncoding::getLength
DLLEXPORT size_t getLength(const char *p, const char *end, ExceptionSink *xsink) const
gives the length of the string in characters
QoreEncoding::getMaxCharWidth
DLLEXPORT int getMaxCharWidth() const
returns the maximum character width in bytes for the encoding
QoreEncoding::getDesc
DLLEXPORT const char * getDesc() const
returns the description for the encoding
QoreEncoding::getMinCharWidth
DLLEXPORT unsigned getMinCharWidth() const
returns the minimum character width in bytes for the encoding
QoreEncoding::getCharPos
DLLEXPORT 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
QoreEncoding::getByteLen
DLLEXPORT size_t getByteLen(const char *p, const char *end, 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
QoreEncoding::getCharPos
DLLEXPORT size_t getCharPos(const char *p, const char *end, ExceptionSink *xsink) const
gives the character position (number of characters) starting from the first pointer to the second
QoreEncoding::isAsciiCompat
DLLEXPORT bool isAsciiCompat() const
returns true if the character encoding is backwards-compatible with ASCII
QoreEncoding::getCode
DLLEXPORT const char * getCode() const
returns the string code (ex: "UTF-8") for the encoding
QoreEncoding::getByteLen
DLLEXPORT size_t getByteLen(const char *p, const char *end, size_t c, ExceptionSink *xsink) const
gives the number of bytes for the number of chars in the string or up to the end of the string
QoreEncoding::getCharLen
DLLEXPORT qore_offset_t getCharLen(const char *p, size_t valid_len) const
gives the number of total bytes for the next character at the given pointer
QoreEncoding::getLength
DLLEXPORT size_t getLength(const char *p, const char *end, bool &invalid) const
gives the length of the string in characters
QoreEncodingManager
manages encodings in Qore
Definition: QoreEncoding.h:205
QoreEncodingManager::showAliases
static DLLEXPORT void showAliases()
prints out all aliases to stdout
QoreEncodingManager::findCreate
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'...
QoreEncodingManager::add
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
QoreEncodingManager::showEncodings
static DLLEXPORT void showEncodings()
prints out all valid encodings to stdout
QoreEncodingManager::findCreate
static DLLEXPORT const QoreEncoding * findCreate(const QoreString *str)
finds an encoding if it exists (also looks up against alias names) and creates a new one if it doesn'...
QoreEncodingManager::addAlias
static DLLEXPORT void addAlias(const QoreEncoding *qcs, const char *alias)
adds an alias for an encoding
QoreString
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:93
QoreThreadLock
provides a mutually-exclusive thread lock
Definition: QoreThreadLock.h:49
common.h
qore_offset_t
intptr_t qore_offset_t
used for offsets that could be negative
Definition: common.h:76