All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
QoreEncoding.h
Go to the documentation of this file.
/* -*- mode: c++; indent-tabs-mode: nil -*- */
/*
QoreEncoding.h
Qore Programming Language
Copyright (C) 2003 - 2017 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 <strings.h>
#include <string.h>
#include <map>
#include <string>
typedef qore_size_t (*mbcs_length_t)(const char* str, const char* end, bool &invalid);
typedef qore_size_t (*mbcs_end_t)(const char* str, const char* end, qore_size_t num_chars, bool &invalid);
typedef qore_size_t (*mbcs_pos_t)(const char* str, const char* ptr, bool &invalid);
typedef qore_offset_t (*mbcs_charlen_t)(const char* str, qore_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:
// FIXME: move all this to the private implementation with the ABI change
// NOTE: the following class members cannot be removed because until Qore 0.8.12 this class implemented inline member functions
// that refered directly to these member variables, therefore they make up a part of the library's ABI :(
std::string code;
std::string desc;
mbcs_length_t flength;
mbcs_end_t fend;
mbcs_pos_t fpos;
mbcs_charlen_t fcharlen;
unsigned char maxwidth;
qore_encoding_private* priv;
public:
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);
DLLLOCAL ~QoreEncoding();
DLLEXPORT qore_size_t getLength(const char* p, const char* end, bool& invalid) const;
DLLEXPORT qore_size_t getLength(const char* p, const char* end, ExceptionSink* xsink) const;
DLLEXPORT qore_size_t getByteLen(const char* p, const char* end, qore_size_t c, bool& invalid) const;
DLLEXPORT qore_size_t getByteLen(const char* p, const char* end, qore_size_t c, ExceptionSink* xsink) const;
DLLEXPORT qore_size_t getCharPos(const char* p, const char* end, bool& invalid) const;
DLLEXPORT qore_size_t getCharPos(const char* p, const char* end, ExceptionSink* xsink) const;
DLLEXPORT qore_offset_t getCharLen(const char* p, qore_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 qore_size_t q_get_byte_len(const QoreEncoding* enc, const char* p, const char* end, qore_size_t c, ExceptionSink* xsink);
DLLEXPORT qore_offset_t q_get_char_len(const QoreEncoding* enc, const char* p, qore_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;
#endif // _QORE_ENCODING_H
QCS_UTF8
DLLEXPORT const QoreEncoding * QCS_UTF8
UTF-8 multi-byte encoding (only UTF-8 and UTF-16 are multi-byte encodings)
QoreEncoding::getLength
DLLEXPORT qore_size_t getLength(const char *p, const char *end, bool &invalid) const
gives the length of the string in characters
QoreEncoding
defines string encoding functions in Qore
Definition: QoreEncoding.h:85
QCS_ISO_8859_8
DLLEXPORT const QoreEncoding * QCS_ISO_8859_8
Hebrew character set.
QCS_ISO_8859_1
DLLEXPORT const QoreEncoding * QCS_ISO_8859_1
latin-1, Western European encoding
QCS_UTF16
DLLEXPORT const QoreEncoding * QCS_UTF16
UTF-16 (only UTF-8 and UTF-16* are multi-byte encodings)
QCS_DEFAULT
DLLEXPORT const QoreEncoding * QCS_DEFAULT
the default encoding for the Qore library
QoreEncoding::getMaxCharWidth
DLLEXPORT int getMaxCharWidth() const
returns the maximum character width in bytes for the encoding
QoreEncoding::getCharLen
DLLEXPORT qore_offset_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
QoreEncodingManager::addAlias
static DLLEXPORT void addAlias(const QoreEncoding *qcs, const char *alias)
adds an alias for an encoding
QoreEncoding::getByteLen
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 ...
QCS_ISO_8859_2
DLLEXPORT const QoreEncoding * QCS_ISO_8859_2
latin-2, Central European encoding
QCS_ISO_8859_11
DLLEXPORT const QoreEncoding * QCS_ISO_8859_11
Thai character set.
QoreEncodingManager
manages encodings in Qore
Definition: QoreEncoding.h:210
QCS_ISO_8859_3
DLLEXPORT const QoreEncoding * QCS_ISO_8859_3
latin-3, Southern European character set
qore_size_t
size_t qore_size_t
used for sizes (same range as a pointer)
Definition: common.h:74
QCS_ISO_8859_4
DLLEXPORT const QoreEncoding * QCS_ISO_8859_4
latin-4, Northern European character set
QoreEncodingManager::showEncodings
static DLLEXPORT void showEncodings()
prints out all valid encodings to stdout
QCS_USASCII
DLLEXPORT const QoreEncoding * QCS_USASCII
ascii encoding
QoreEncoding::getDesc
DLLEXPORT const char * getDesc() const
returns the description for the encoding
QCS_ISO_8859_10
DLLEXPORT const QoreEncoding * QCS_ISO_8859_10
latin-6, Nordic character set
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...
QoreString
Qore&#39;s string type supported by the QoreEncoding class.
Definition: QoreString.h:82
QCS_KOI8_U
DLLEXPORT const QoreEncoding * QCS_KOI8_U
Ukrainian: Kod Obmena Informatsiey, 8 bit.
mbcs_length_t
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
QEM
DLLEXPORT QoreEncodingManager QEM
the QoreEncodingManager object
mbcs_end_t
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
QCS_ISO_8859_9
DLLEXPORT const QoreEncoding * QCS_ISO_8859_9
latin-5, Turkish character set
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&#39;...
mbcs_charlen_t
qore_offset_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
QCS_UTF16BE
DLLEXPORT const QoreEncoding * QCS_UTF16BE
UTF-16BE (only UTF-8 and UTF-16* are multi-byte encodings)
QCS_KOI7
DLLEXPORT const QoreEncoding * QCS_KOI7
Russian: Kod Obmena Informatsiey, 7 bit characters.
mbcs_pos_t
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
QoreEncoding::isAsciiCompat
DLLEXPORT bool isAsciiCompat() const
returns true if the character encoding is backwards-compatible with ASCII
ExceptionSink
container for holding Qore-language exception information and also for registering a &quot;thread_exit&quot; ca...
Definition: ExceptionSink.h:47
QCS_ISO_8859_14
DLLEXPORT const QoreEncoding * QCS_ISO_8859_14
latin-8, Celtic character set
QCS_ISO_8859_6
DLLEXPORT const QoreEncoding * QCS_ISO_8859_6
Arabic character set.
QoreEncoding::getMinCharWidth
DLLEXPORT unsigned getMinCharWidth() const
returns the minimum character width in bytes for the encoding
qore_offset_t
intptr_t qore_offset_t
used for offsets that could be negative
Definition: common.h:77
QCS_ISO_8859_5
DLLEXPORT const QoreEncoding * QCS_ISO_8859_5
Cyrillic character set.
QoreEncoding::getCharPos
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 ...
common.h
QoreThreadLock
provides a mutually-exclusive thread lock
Definition: QoreThreadLock.h:49
QCS_UTF16LE
DLLEXPORT const QoreEncoding * QCS_UTF16LE
UTF-16LE (only UTF-8 and UTF-16* are multi-byte encodings)
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
QCS_ISO_8859_16
DLLEXPORT const QoreEncoding * QCS_ISO_8859_16
latin-10, Southeast European character set
QoreEncoding::getCode
DLLEXPORT const char * getCode() const
returns the string code (ex: &quot;UTF-8&quot;) for the encoding
QoreEncoding::isMultiByte
DLLEXPORT bool isMultiByte() const
returns true if the encoding is a multi-byte encoding
QCS_ISO_8859_15
DLLEXPORT const QoreEncoding * QCS_ISO_8859_15
latin-9, Western European with euro symbol
QCS_KOI8_R
DLLEXPORT const QoreEncoding * QCS_KOI8_R
Russian: Kod Obmena Informatsiey, 8 bit.
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:68
QCS_ISO_8859_7
DLLEXPORT const QoreEncoding * QCS_ISO_8859_7
Greek character set.
QCS_ISO_8859_13
DLLEXPORT const QoreEncoding * QCS_ISO_8859_13
latin-7, Baltic rim character set