All Classes 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 2003 - 2013 David Nichols
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _QORE_CHARSET_H
#define _QORE_CHARSET_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_size_t (*mbcs_charlen_t)(const char *str, qore_size_t valid_len);
class ExceptionSink;
class QoreEncoding {
private:
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;
public:
DLLLOCAL QoreEncoding(const char *n_code, const char *n_desc = 0, 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) : code(n_code), desc(n_desc ? n_desc : ""), flength(l), fend(e), fpos(p), fcharlen(c), maxwidth(n_maxwidth) {
}
DLLLOCAL ~QoreEncoding() {
}
DLLLOCAL qore_size_t getLength(const char *p, const char *end, bool &invalid) const {
return flength ? flength(p, end, invalid) : strlen(p);
}
DLLLOCAL qore_size_t getLength(const char *p, const char *end, ExceptionSink *xsink) const;
DLLLOCAL qore_size_t getByteLen(const char *p, const char *end, qore_size_t c, bool &invalid) const {
return fend ? fend(p, end, c, invalid) : c;
}
DLLLOCAL qore_size_t getByteLen(const char *p, const char *end, qore_size_t c, ExceptionSink *xsink) const;
DLLLOCAL qore_size_t getCharPos(const char *p, const char *end, bool &invalid) const {
return fpos ? fpos(p, end, invalid) : end - p;
}
DLLLOCAL qore_size_t getCharPos(const char *p, const char *end, ExceptionSink *xsink) const;
DLLLOCAL qore_size_t getCharLen(const char *p, qore_size_t valid_len) const {
return fcharlen ? fcharlen(p, valid_len) : 1;
}
DLLLOCAL bool isMultiByte() const {
return (bool)flength;
}
DLLLOCAL const char *getCode() const {
return code.c_str();
}
DLLLOCAL const char *getDesc() const {
return desc.empty() ? "<no description available>" : desc.c_str();
}
DLLLOCAL int getMaxCharWidth() const {
return maxwidth;
}
};
// case-insensitive maps for encodings
typedef std::map<const char *, QoreEncoding *, class ltcstrcase> encoding_map_t;
typedef std::map<const char *, const QoreEncoding *, class 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 class QoreThreadLock mutex;
DLLLOCAL static const QoreEncoding *addUnlocked(const char *code, const char *desc, 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 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 extern QoreEncodingManager QEM;
// builtin character encodings
DLLEXPORT extern const QoreEncoding *QCS_DEFAULT,
*QCS_USASCII,
*QCS_UTF8,
*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_CHARSET_H