Qore Programming Language 1.19.1
Loading...
Searching...
No Matches
IconvHelper.h
Go to the documentation of this file.
1//--------------------------------------------------------------------*- C++ -*-
2//
3// Qore Programming Language
4//
5// Copyright (C) 2016 - 2023 Qore Technologies, s.r.o.
6//
7// Permission is hereby granted, free of charge, to any person obtaining a
8// copy of this software and associated documentation files (the "Software"),
9// to deal in the Software without restriction, including without limitation
10// the rights to use, copy, modify, merge, publish, distribute, sublicense,
11// and/or sell copies of the Software, and to permit persons to whom the
12// Software is furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in
15// all copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23// DEALINGS IN THE SOFTWARE.
24//
25//------------------------------------------------------------------------------
30//------------------------------------------------------------------------------
31#ifndef INCLUDE_QORE_INTERN_ICONVHELPER_H_
32#define INCLUDE_QORE_INTERN_ICONVHELPER_H_
33
34#include <qore/Qore.h>
35
36#include <cerrno>
37#include <iconv.h>
38
39class IconvHelper {
40
41public:
42 DLLLOCAL IconvHelper(const QoreEncoding *to, const QoreEncoding *from, ExceptionSink *xsink) : to(to), from(from) {
43#ifdef NEED_ICONV_TRANSLIT
44 QoreString to_code(const_cast<char *>(to->getCode()));
45 to_code.concat("//TRANSLIT");
46 c = iconv_open(to_code.getBuffer(), from->getCode());
47#else
48 c = iconv_open(to->getCode(), from->getCode());
49#endif
50 if (c == (iconv_t) -1) {
51 if (errno == EINVAL) {
52 xsink->raiseException("ENCODING-CONVERSION-ERROR", "cannot convert from \"%s\" to \"%s\"",
53 from->getCode(), to->getCode());
54 } else {
55 reportUnknownError(xsink);
56 }
57 }
58 }
59
60 DLLLOCAL ~IconvHelper() {
61 if (c != (iconv_t) -1) {
62 iconv_close(c);
63 }
64 }
65
66 DLLLOCAL size_t iconv(char **inbuf, size_t *inavail, char **outbuf, size_t *outavail) {
67 return iconv_adapter(::iconv, c, inbuf, inavail, outbuf, outavail);
68 }
69
70 void reportIllegalSequence(size_t offset, ExceptionSink *xsink) {
71 xsink->raiseException("ENCODING-CONVERSION-ERROR",
72 "illegal character sequence at byte offset " QLLD " found in input type \"%s\" (while converting to \"%s\")",
73 (int64)offset, from->getCode(), to->getCode());
74 }
75
76 void reportUnknownError(ExceptionSink *xsink) {
77 xsink->raiseErrnoException("ENCODING-CONVERSION-ERROR", errno, "unknown error converting from \"%s\" to \"%s\"",
78 from->getCode(), to->getCode());
79 }
80
81private:
82 // needed for platforms where the input buffer is defined as "const char"
83 template<typename T>
84 static size_t iconv_adapter(size_t (*iconv_f)(iconv_t, T, size_t *, char **, size_t *), iconv_t handle,
85 char **inbuf, size_t *inavail, char **outbuf, size_t *outavail) {
86 return (*iconv_f) (handle, const_cast<T>(inbuf), inavail, outbuf, outavail);
87 }
88
89private:
90 const QoreEncoding *to;
91 const QoreEncoding *from;
92 iconv_t c;
93};
94
95#endif // INCLUDE_QORE_INTERN_ICONVHELPER_H_
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:50
DLLEXPORT AbstractQoreNode * raiseException(const char *err, const char *fmt,...)
appends a Qore-language exception to the list
DLLEXPORT AbstractQoreNode * raiseErrnoException(const char *err, int en, const char *fmt,...)
appends a Qore-language exception to the list and appends the result of strerror(errno) to the descri...
defines string encoding functions in Qore
Definition: QoreEncoding.h:83
DLLEXPORT const char * getCode() const
returns the string code (ex: "UTF-8") for the encoding
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:93
long long int64
64bit integer type, cannot use int64_t here since it breaks the API on some 64-bit systems due to equ...
Definition: common.h:260