File: CharsetEncoder.h

package info (click to toggle)
lsp-plugins 1.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 91,856 kB
  • sloc: cpp: 427,831; xml: 57,779; makefile: 9,961; php: 1,005; sh: 18
file content (152 lines) | stat: -rw-r--r-- 6,498 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
 *           (C) 2020 Vladimir Sadovnikov <sadko4u@gmail.com>
 *
 * This file is part of lsp-runtime-lib
 * Created on: 8 мар. 2019 г.
 *
 * lsp-runtime-lib 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 3 of the License, or
 * any later version.
 *
 * lsp-runtime-lib 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 lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef LSP_PLUG_IN_IO_CHARSETENCODER_H_
#define LSP_PLUG_IN_IO_CHARSETENCODER_H_

#include <lsp-plug.in/runtime/version.h>
#include <lsp-plug.in/common/status.h>
#include <lsp-plug.in/io/charset.h>
#include <lsp-plug.in/io/File.h>
#include <lsp-plug.in/io/IInSequence.h>
#include <lsp-plug.in/io/IOutStream.h>

namespace lsp
{
    namespace io
    {
        /**
         * Character set encoder, from UTF-32 to any character set
         * Important! fetch() operations do not always return blocks
         * aligned to the character range.
         */
        class CharsetEncoder
        {
            protected:
                uint8_t        *bBuffer;        // Temporary buffer for storing input byte sequence
                uint8_t        *bBufHead;       // Buffer head
                uint8_t        *bBufTail;       // Buffer tail

                lsp_wchar_t    *cBuffer;        // Temporary buffer for storing UTF-16 code points
                lsp_wchar_t    *cBufHead;       // Character buffer head
                lsp_wchar_t    *cBufTail;       // Character buffer tail

#if defined(PLATFORM_WINDOWS)
                lsp_utf16_t    *xBuffer;        // Additional translation buffer
                UINT            nCodePage;      // Code page
#else
                iconv_t         hIconv;         // iconv handle
#endif /* PLATFORM_WINDOWS */

            private:
                CharsetEncoder &operator = (const CharsetEncoder &);    // Prevent from copying

                inline size_t   prepare_buffer();
                ssize_t         encode_buffer();

            public:
                explicit CharsetEncoder();
                ~CharsetEncoder();

            public:
                /**
                 * Initialize character-set decoder
                 * @param charset character set name, NULL for default native character set
                 * @return status of operation
                 */
                status_t    init(const char *charset = NULL);

                /**
                 * Close decoder and free all resources
                 */
                void        close();

                /**
                 * Fill internal buffer with character data
                 * @param buf source buffer
                 * @param count buffer size
                 * @return number of characters processed or negative error code
                 */
                ssize_t     fill(const lsp_wchar_t *buf, size_t count);

                /**
                 * Fill internal buffer with ASCII character data
                 * @param buf source buffer
                 * @param count buffer size
                 * @return number of characters processed or negative error code
                 */
                ssize_t     fill(const char *buf, size_t count);

                /**
                 * Fill internal buffer with character data from sequence
                 * @param in input sequence to perform character reading
                 * @param count maximum number of characters to process, 0 means maximum possible value
                 * @return number of characters processed or negative error code
                 */
                ssize_t     fill(IInSequence *in, size_t count = 0);

                /**
                 * Fill internal buffer with one single character
                 * @param ch character to add to buffer
                 * @return positive number on success 0 if no place in buffer or negative status of operation
                 */
                ssize_t     fill(lsp_wchar_t ch);

                /**
                 * Fill internal buffer with character data from string
                 * @param in input string to perform character reading
                 * @param first the index of first character to take from string
                 * @param last the index of first character that won't be taken
                 * @return number of characters processed or negative error code
                 */
                ssize_t         fill(const LSPString *in, size_t first, size_t last);
                inline ssize_t  fill(const LSPString *in, size_t first) { return (in == NULL) ? -STATUS_BAD_ARGUMENTS : fill(in, first, in->length()); };
                inline ssize_t  fill(const LSPString *in) { return (in == NULL) ? -STATUS_BAD_ARGUMENTS : fill(in, 0, in->length()); };

                /**
                 * Fetch output byte data into buffer
                 * @param buf buffer to perform fetch
                 * @param count maximum number of bytes to fetch
                 * @return number of bytes fetched or negative error code
                 */
                ssize_t     fetch(void *buf, size_t count);

                /**
                 * Fetch output byte data into buffer
                 * @param buf buffer to perform fetch
                 * @param count maximum number of bytes to fetch, 0 means maximum possible value
                 * @return number of bytes fetched or negative error code
                 */
                ssize_t     fetch(File *out, size_t count = 0);

                /**
                 * Fetch output byte data into output stream
                 * @param buf buffer to perform fetch
                 * @param count maximum number of bytes to fetch, 0 means maximum possible value
                 * @return number of bytes fetched or negative error code
                 */
                ssize_t     fetch(IOutStream *out, size_t count = 0);
        };
    
    } /* namespace io */
} /* namespace lsp */

#endif /* LSP_PLUG_IN_IO_CHARSETENCODER_H_ */