Open Chinese Convert 0.4.3 A project for conversion between Traditional and Simplified Chinese

- Main Page
- Related Pages
- Modules
- Data Structures
- Files
- Examples

- File List
- Globals

All Data Structures Files Functions Variables Groups Pages
openccxx.h
1 /*
2 * Open Chinese Convert
3 *
4 * Copyright 2010-2013 BYVoid <byvoid@byvoid.com>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #ifndef __OPENCCXX_H_
20 #define __OPENCCXX_H_
21
25 #ifdef __cplusplus
26
27 extern "C" {
28 # include <opencc.h>
29 }
30
31 # include <cstdlib>
32 # include <string>
33
34 namespace opencc {
35
36 class opencc {
37 public:
38 opencc(const char* config_file = NULL)
39 : od((opencc_t)-1) {
40 open(config_file);
41 }
42
43 virtual ~opencc() {
44 if (od != (opencc_t)-1) {
45 opencc_close(od);
46 }
47 }
48
49 operator bool() const {
50 return od != (opencc_t)-1;
51 }
52
53 int open(const char* config_file) {
54 if (od != (opencc_t)-1) {
55 opencc_close(od);
56 }
57 od = opencc_open(config_file);
58 return (od == (opencc_t)-1) ? (-1) : (0);
59 }
60
61 int set_conversion_mode(opencc_conversion_mode conversion_mode) {
62 if (od == (opencc_t)-1) {
63 return -1;
64 }
65 opencc_set_conversion_mode(od, conversion_mode);
66 return 0;
67 }
68
69 long convert(const std::string& in, std::string& out, long length = -1) {
70 if (od == (opencc_t)-1) {
71 return -1;
72 }
73 if (length == -1) {
74 length = in.length();
75 }
76 char* outbuf = opencc_convert_utf8(od, in.c_str(), length);
77 if (outbuf == (char*)-1) {
78 return -1;
79 }
80 out = outbuf;
81 free(outbuf);
82 return length;
83 }
84
90 long convert(const std::wstring& in, std::wstring& out, long length = -1) {
91 if (od == (opencc_t)-1) {
92 return -1;
93 }
94 size_t inbuf_left = in.length();
95 if ((length >= 0) && (length < (long)inbuf_left)) {
96 inbuf_left = length;
97 }
98 const ucs4_t* inbuf = (const ucs4_t*)in.c_str();
99 long count = 0;
100 while (inbuf_left != 0) {
101 size_t retval;
102 size_t outbuf_left;
103 ucs4_t* outbuf;
104 /* occupy space */
105 outbuf_left = inbuf_left + 64;
106 out.resize(count + outbuf_left);
107 outbuf = (ucs4_t*)out.c_str() + count;
108 retval = opencc_convert(od, (ucs4_t**)&inbuf,
109 &inbuf_left, &outbuf, &outbuf_left);
110 if (retval == (size_t)-1) {
111 return -1;
112 }
113 count += retval;
114 }
115 /* set the zero termination and shrink the size */
116 out.resize(count + 1);
117 out[count] = L'\0';
118 return count;
119 }
120
121 opencc_error errno() const {
122 return opencc_errno();
123 }
124
125 void perror(const char* spec = "OpenCC") const {
126 opencc_perror(spec);
127 }
128
129 private:
130 opencc_t od;
131 };
132 }
133
134 #endif // ifdef __cplusplus
135
136 #endif /* __OPENCCXX_H_ */
opencc_convert_utf8
char * opencc_convert_utf8(opencc_t t_opencc, const char *inbuf, size_t length)
Converts UTF-8 string from inbuf.
Definition: opencc.c:66
opencc_open
opencc_t opencc_open(const char *config_file)
Makes an instance of opencc.
Definition: opencc.c:142
opencc_close
int opencc_close(opencc_t t_opencc)
Destroys an instance of opencc.
Definition: opencc.c:168
opencc_perror
void opencc_perror(const char *spec)
Prints the error message to stderr.
Definition: opencc.c:220
opencc_errno
opencc_error opencc_errno(void)
Returns an opencc_convert_errno_t which describes the last error.
Definition: opencc.c:213
opencc.h
OpenCC API.
opencc_set_conversion_mode
void opencc_set_conversion_mode(opencc_t t_opencc, opencc_conversion_mode conversion_mode)
Changes the mode of conversion.
Definition: opencc.c:204
opencc_convert
size_t opencc_convert(opencc_t t_opencc, ucs4_t **inbuf, size_t *inbuf_left, ucs4_t **outbuf, size_t *outbuf_left)
Converts a UCS-4 string from *inbuf to *outbuf.
Definition: opencc.c:46