| 1 |
/* |
| 2 |
* Copyright (C) 2023 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2023 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 8 мар. 2019 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-runtime-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifndef LSP_PLUG_IN_IO_CHARSETENCODER_H_ |
| 23 |
#define LSP_PLUG_IN_IO_CHARSETENCODER_H_ |
| 24 |
|
| 25 |
#include <lsp-plug.in/runtime/version.h> |
| 26 |
#include <lsp-plug.in/common/status.h> |
| 27 |
#include <lsp-plug.in/io/charset.h> |
| 28 |
#include <lsp-plug.in/io/File.h> |
| 29 |
#include <lsp-plug.in/io/IInSequence.h> |
| 30 |
#include <lsp-plug.in/io/IOutStream.h> |
| 31 |
|
| 32 |
namespace lsp |
| 33 |
{ |
| 34 |
namespace io |
| 35 |
{ |
| 36 |
/** |
| 37 |
* Character set encoder, from UTF-32 to any character set |
| 38 |
* Important! fetch() operations do not always return blocks |
| 39 |
* aligned to the character range. |
| 40 |
*/ |
| 41 |
class CharsetEncoder |
| 42 |
{ |
| 43 |
protected: |
| 44 |
uint8_t *bBuffer; // Temporary buffer for storing input byte sequence |
| 45 |
uint8_t *bBufHead; // Buffer head |
| 46 |
uint8_t *bBufTail; // Buffer tail |
| 47 |
|
| 48 |
lsp_wchar_t *cBuffer; // Temporary buffer for storing UTF-16 code points |
| 49 |
lsp_wchar_t *cBufHead; // Character buffer head |
| 50 |
lsp_wchar_t *cBufTail; // Character buffer tail |
| 51 |
|
| 52 |
#if defined(PLATFORM_WINDOWS) |
| 53 |
lsp_utf16_t *xBuffer; // Additional translation buffer |
| 54 |
uint32_t nCodePage; // Code page |
| 55 |
#else |
| 56 |
iconv_t hIconv; // iconv handle |
| 57 |
#endif /* PLATFORM_WINDOWS */ |
| 58 |
|
| 59 |
private: |
| 60 |
inline size_t prepare_buffer(); |
| 61 |
ssize_t encode_buffer(); |
| 62 |
|
| 63 |
public: |
| 64 |
explicit CharsetEncoder(); |
| 65 |
CharsetEncoder(const CharsetEncoder &) = delete; |
| 66 |
CharsetEncoder(CharsetEncoder &&) = delete; |
| 67 |
~CharsetEncoder(); |
| 68 |
|
| 69 |
CharsetEncoder &operator = (const CharsetEncoder &) = delete; |
| 70 |
CharsetEncoder &operator = (CharsetEncoder &&) = delete; |
| 71 |
|
| 72 |
public: |
| 73 |
/** |
| 74 |
* Initialize character-set decoder |
| 75 |
* @param charset character set name, NULL for default native character set |
| 76 |
* @return status of operation |
| 77 |
*/ |
| 78 |
status_t init(const char *charset = NULL); |
| 79 |
|
| 80 |
/** |
| 81 |
* Close decoder and free all resources |
| 82 |
*/ |
| 83 |
void close(); |
| 84 |
|
| 85 |
/** |
| 86 |
* Fill internal buffer with character data |
| 87 |
* @param buf source buffer |
| 88 |
* @param count buffer size |
| 89 |
* @return number of characters processed or negative error code |
| 90 |
*/ |
| 91 |
ssize_t fill(const lsp_wchar_t *buf, size_t count); |
| 92 |
|
| 93 |
/** |
| 94 |
* Fill internal buffer with ASCII character data |
| 95 |
* @param buf source buffer |
| 96 |
* @param count buffer size |
| 97 |
* @return number of characters processed or negative error code |
| 98 |
*/ |
| 99 |
ssize_t fill(const char *buf, size_t count); |
| 100 |
|
| 101 |
/** |
| 102 |
* Fill internal buffer with character data from sequence |
| 103 |
* @param in input sequence to perform character reading |
| 104 |
* @param count maximum number of characters to process, 0 means maximum possible value |
| 105 |
* @return number of characters processed or negative error code |
| 106 |
*/ |
| 107 |
ssize_t fill(IInSequence *in, size_t count = 0); |
| 108 |
|
| 109 |
/** |
| 110 |
* Fill internal buffer with one single character |
| 111 |
* @param ch character to add to buffer |
| 112 |
* @return positive number on success 0 if no place in buffer or negative status of operation |
| 113 |
*/ |
| 114 |
ssize_t fill(lsp_wchar_t ch); |
| 115 |
|
| 116 |
/** |
| 117 |
* Fill internal buffer with character data from string |
| 118 |
* @param in input string to perform character reading |
| 119 |
* @param first the index of first character to take from string |
| 120 |
* @param last the index of first character that won't be taken |
| 121 |
* @return number of characters processed or negative error code |
| 122 |
*/ |
| 123 |
ssize_t fill(const LSPString *in, size_t first, size_t last); |
| 124 |
inline ssize_t fill(const LSPString *in, size_t first) { return (in == NULL) ? -STATUS_BAD_ARGUMENTS : fill(in, first, in->length()); }; |
| 125 |
inline ssize_t fill(const LSPString *in) { return (in == NULL) ? -STATUS_BAD_ARGUMENTS : fill(in, 0, in->length()); }; |
| 126 |
|
| 127 |
/** |
| 128 |
* Fetch output byte data into buffer |
| 129 |
* @param buf buffer to perform fetch |
| 130 |
* @param count maximum number of bytes to fetch |
| 131 |
* @return number of bytes fetched or negative error code |
| 132 |
*/ |
| 133 |
ssize_t fetch(void *buf, size_t count); |
| 134 |
|
| 135 |
/** |
| 136 |
* Fetch output byte data into file object |
| 137 |
* @param out file to store the fetch result |
| 138 |
* @param count maximum number of bytes to fetch, 0 means maximum possible value |
| 139 |
* @return number of bytes fetched or negative error code |
| 140 |
*/ |
| 141 |
ssize_t fetch(File *out, size_t count = 0); |
| 142 |
|
| 143 |
/** |
| 144 |
* Fetch output byte data into output stream |
| 145 |
* @param out buffer to store the fetch result |
| 146 |
* @param count maximum number of bytes to fetch, 0 means maximum possible value |
| 147 |
* @return number of bytes fetched or negative error code |
| 148 |
*/ |
| 149 |
ssize_t fetch(IOutStream *out, size_t count = 0); |
| 150 |
}; |
| 151 |
|
| 152 |
} /* namespace io */ |
| 153 |
} /* namespace lsp */ |
| 154 |
|
| 155 |
#endif /* LSP_PLUG_IN_IO_CHARSETENCODER_H_ */ |