| 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_CHARSETDECODER_H_ |
| 23 |
#define LSP_PLUG_IN_IO_CHARSETDECODER_H_ |
| 24 |
|
| 25 |
#include <lsp-plug.in/runtime/version.h> |
| 26 |
#include <lsp-plug.in/runtime/LSPString.h> |
| 27 |
#include <lsp-plug.in/common/status.h> |
| 28 |
#include <lsp-plug.in/io/charset.h> |
| 29 |
#include <lsp-plug.in/io/File.h> |
| 30 |
#include <lsp-plug.in/io/IInStream.h> |
| 31 |
#include <lsp-plug.in/io/IOutSequence.h> |
| 32 |
|
| 33 |
namespace lsp |
| 34 |
{ |
| 35 |
namespace io |
| 36 |
{ |
| 37 |
/** |
| 38 |
* Character set decoder, from any character set to UTF-32 |
| 39 |
* For most methods utilizes internal buffer. |
| 40 |
* Very suitable for streaming processing. |
| 41 |
*/ |
| 42 |
class CharsetDecoder |
| 43 |
{ |
| 44 |
protected: |
| 45 |
uint8_t *bBuffer; // Temporary buffer for storing input byte sequence |
| 46 |
uint8_t *bBufHead; // Buffer head |
| 47 |
uint8_t *bBufTail; // Buffer tail |
| 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 decode_buffer(); |
| 62 |
|
| 63 |
public: |
| 64 |
explicit CharsetDecoder(); |
| 65 |
CharsetDecoder(const CharsetDecoder &) = delete; |
| 66 |
CharsetDecoder(CharsetDecoder &&) = delete; |
| 67 |
~CharsetDecoder(); |
| 68 |
|
| 69 |
CharsetDecoder & operator = (const CharsetDecoder &) = delete; |
| 70 |
CharsetDecoder & operator = (CharsetDecoder &&) = 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 |
* Fetch a single character |
| 87 |
* @return single character to fetch |
| 88 |
*/ |
| 89 |
lsp_swchar_t fetch(); |
| 90 |
|
| 91 |
/** |
| 92 |
* Fetch decoded characters into output buffer |
| 93 |
* @param outbuf output buffer to perform fetch |
| 94 |
* @param count the size of output buffer |
| 95 |
* @return number of characters fetched or negative error code |
| 96 |
*/ |
| 97 |
ssize_t fetch(lsp_wchar_t *outbuf, size_t count); |
| 98 |
|
| 99 |
/** |
| 100 |
* Fetch decoded characters directly into string |
| 101 |
* @param out output string to perform fetch |
| 102 |
* @param count the maximum possible amount of characters to fetch, 0 means maximum possible value |
| 103 |
* @return number of characters fetched or negative error code |
| 104 |
*/ |
| 105 |
ssize_t fetch(LSPString *out, size_t count = 0); |
| 106 |
|
| 107 |
/** |
| 108 |
* Fetch decoded characters directly into output sequence |
| 109 |
* @param out output sequence |
| 110 |
* @param count maximum number of characters to fetch, 0 means maximum possible value |
| 111 |
* @return number of characters fetched or negative status of operation |
| 112 |
*/ |
| 113 |
ssize_t fetch(IOutSequence *out, size_t count = 0); |
| 114 |
|
| 115 |
/** |
| 116 |
* Fill the internal byte buffer with additional data for decoding |
| 117 |
* @param buf source buffer with data |
| 118 |
* @param count size of the buffer |
| 119 |
* @return number of bytes added to internal buffer |
| 120 |
*/ |
| 121 |
ssize_t fill(const void *buf, size_t count); |
| 122 |
|
| 123 |
/** |
| 124 |
* Fill the internal byte buffer with additional data directly from file |
| 125 |
* @param fd file to read data |
| 126 |
* @param count maximum number of bytes to read, 0 means maximum possible value |
| 127 |
* @return number of bytes read into internal buffer |
| 128 |
*/ |
| 129 |
ssize_t fill(File *fd, size_t count = 0); |
| 130 |
|
| 131 |
/** |
| 132 |
* Fill the internal byte buffer with additional data directly from input stream |
| 133 |
* @param is input stream to read data |
| 134 |
* @param count maximum number of bytes to read, 0 means maximum possible value |
| 135 |
* @return number of bytes read into internal buffer |
| 136 |
*/ |
| 137 |
ssize_t fill(IInStream *is, size_t count = 0); |
| 138 |
}; |
| 139 |
|
| 140 |
} /* namespace io */ |
| 141 |
} /* namespace lsp */ |
| 142 |
|
| 143 |
#endif /* LSP_PLUG_IN_IO_CHARSETDECODER_H_ */ |