示例#1
0
        public string Convert(string text)
        {
            IntPtr inStr  = NativeUtf8FromString(text);
            IntPtr outStr = CInterface.opencc_convert_utf8(OpenCCInstance.ToInt64(), inStr, -1);

            Marshal.FreeHGlobal(inStr);
            return(StringFromNativeUtf8(outStr));
        }
示例#2
0
 public OpenCcClient(string configFile = "opencc_data/s2t.json")
 {
     OpenCCInstance = CInterface.opencc_open(FindConfigPath(configFile));
     if (OpenCCInstance.ToInt64() == 0)
     {
         throw new Exception($"Failed to initialize opencc");
     }
 }
示例#3
0
        public void Dispose()
        {
            var result = CInterface.opencc_close(OpenCCInstance.ToInt64());

            if (result.ToInt64() != 0)
            {
                throw new Exception($"Non zero return value when calling opencc_close: {result.ToInt64()}");
            }
        }
示例#4
0
        public static string StringFromNativeUtf8(IntPtr nativeUtf8)
        {
            int len = 0;

            while (Marshal.ReadByte(nativeUtf8, len) != 0)
            {
                ++len;
            }
            byte[] buffer = new byte[len];
            Marshal.Copy(nativeUtf8, buffer, 0, buffer.Length);
            CInterface.opencc_convert_utf8_free(nativeUtf8);
            return(Encoding.UTF8.GetString(buffer));
        }