Open Chinese Convert
0.4.3
A project for conversion between Traditional and Simplified Chinese
|
00001 /* 00002 * Open Chinese Convert 00003 * 00004 * Copyright 2010-2013 BYVoid <byvoid@byvoid.com> 00005 * 00006 * Licensed under the Apache License, Version 2.0 (the "License"); 00007 * you may not use this file except in compliance with the License. 00008 * You may obtain a copy of the License at 00009 * 00010 * http://www.apache.org/licenses/LICENSE-2.0 00011 * 00012 * Unless required by applicable law or agreed to in writing, software 00013 * distributed under the License is distributed on an "AS IS" BASIS, 00014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 * See the License for the specific language governing permissions and 00016 * limitations under the License. 00017 */ 00018 00019 #ifndef __OPENCCXX_H_ 00020 #define __OPENCCXX_H_ 00021 00025 #ifdef __cplusplus 00026 00027 extern "C" { 00028 # include <opencc.h> 00029 } 00030 00031 # include <cstdlib> 00032 # include <string> 00033 00034 namespace opencc { 00035 00036 class opencc { 00037 public: 00038 opencc(const char* config_file = NULL) 00039 : od((opencc_t)-1) { 00040 open(config_file); 00041 } 00042 00043 virtual ~opencc() { 00044 if (od != (opencc_t)-1) { 00045 opencc_close(od); 00046 } 00047 } 00048 00049 operator bool() const { 00050 return od != (opencc_t)-1; 00051 } 00052 00053 int open(const char* config_file) { 00054 if (od != (opencc_t)-1) { 00055 opencc_close(od); 00056 } 00057 od = opencc_open(config_file); 00058 return (od == (opencc_t)-1) ? (-1) : (0); 00059 } 00060 00061 int set_conversion_mode(opencc_conversion_mode conversion_mode) { 00062 if (od == (opencc_t)-1) { 00063 return -1; 00064 } 00065 opencc_set_conversion_mode(od, conversion_mode); 00066 return 0; 00067 } 00068 00069 long convert(const std::string& in, std::string& out, long length = -1) { 00070 if (od == (opencc_t)-1) { 00071 return -1; 00072 } 00073 if (length == -1) { 00074 length = in.length(); 00075 } 00076 char* outbuf = opencc_convert_utf8(od, in.c_str(), length); 00077 if (outbuf == (char*)-1) { 00078 return -1; 00079 } 00080 out = outbuf; 00081 free(outbuf); 00082 return length; 00083 } 00084 00090 long convert(const std::wstring& in, std::wstring& out, long length = -1) { 00091 if (od == (opencc_t)-1) { 00092 return -1; 00093 } 00094 size_t inbuf_left = in.length(); 00095 if ((length >= 0) && (length < (long)inbuf_left)) { 00096 inbuf_left = length; 00097 } 00098 const ucs4_t* inbuf = (const ucs4_t*)in.c_str(); 00099 long count = 0; 00100 while (inbuf_left != 0) { 00101 size_t retval; 00102 size_t outbuf_left; 00103 ucs4_t* outbuf; 00104 /* occupy space */ 00105 outbuf_left = inbuf_left + 64; 00106 out.resize(count + outbuf_left); 00107 outbuf = (ucs4_t*)out.c_str() + count; 00108 retval = opencc_convert(od, (ucs4_t**)&inbuf, 00109 &inbuf_left, &outbuf, &outbuf_left); 00110 if (retval == (size_t)-1) { 00111 return -1; 00112 } 00113 count += retval; 00114 } 00115 /* set the zero termination and shrink the size */ 00116 out.resize(count + 1); 00117 out[count] = L'\0'; 00118 return count; 00119 } 00120 00121 opencc_error errno() const { 00122 return opencc_errno(); 00123 } 00124 00125 void perror(const char* spec = "OpenCC") const { 00126 opencc_perror(spec); 00127 } 00128 00129 private: 00130 opencc_t od; 00131 }; 00132 } 00133 00134 #endif // ifdef __cplusplus 00135 00136 #endif /* __OPENCCXX_H_ */