コード例 #1
0
ファイル: Migemo.cs プロジェクト: u338steven/UIAssistant
        unsafe public static void Initialize(string migemoPath = null, string migemoDictionaryPath = null)
        {
            try
            {
                if (IsEnable())
                {
                    Dispose();
                }

                string migemoFileName;
                if (Environment.Is64BitProcess)
                {
                    migemoFileName = "migemo.dll";
                }
                else
                {
                    migemoFileName = "migemo32.dll";
                }

                string migemoRootDir;
                string dllPath;
                if (string.IsNullOrEmpty(migemoPath))
                {
                    migemoRootDir = System.IO.Directory.GetParent(System.Reflection.Assembly.GetEntryAssembly().Location).ToString();
                    dllPath       = System.IO.Path.Combine(migemoRootDir, migemoFileName);
                }
                else
                {
                    migemoRootDir = migemoPath;
                    dllPath       = System.IO.Path.Combine(migemoPath, migemoFileName);
                }

                if (!System.IO.File.Exists(dllPath))
                {
                    throw new DllNotFoundException("migemo.dll not found");
                }

                migemoDll = new UnmanagedDll(dllPath);

                migemo_open             = migemoDll.GetProcDelegate <MigemoOpen>("migemo_open");
                migemo_close            = migemoDll.GetProcDelegate <MigemoClose>("migemo_close");
                migemo_query            = migemoDll.GetProcDelegate <MigemoQuery>("migemo_query");
                migemo_release          = migemoDll.GetProcDelegate <MigemoRelease>("migemo_release");
                migemo_set_operator     = migemoDll.GetProcDelegate <MigemoSetOperator>("migemo_set_operator");
                migemo_get_operator     = migemoDll.GetProcDelegate <MigemoGetOperator>("migemo_get_operator");
                migemo_load             = migemoDll.GetProcDelegate <MigemoLoad>("migemo_load");
                migemo_is_enable        = migemoDll.GetProcDelegate <MigemoIsEnable>("migemo_is_enable");
                migemo_setproc_int2char = migemoDll.GetProcDelegate <MigemoSetProcInt2Char>("migemo_setproc_int2char");

                string dictpath = migemoDictionaryPath;
                if (string.IsNullOrEmpty(dictpath))
                {
                    dictpath = System.IO.Path.Combine(@migemoRootDir, "dict/migemo-dict");
                    if (!System.IO.File.Exists(dictpath))
                    {
                        dictpath = System.IO.Path.Combine(@migemoRootDir, "dict/utf-8/migemo-dict");
                    }
                }
                MigemoObject = migemo_open(dictpath);
                MigemoStruct migemoStruct = new MigemoStruct();
                migemoStruct = Marshal.PtrToStructure <MigemoStruct>(MigemoObject);

                if (!IsEnable())
                {
                    throw new DllNotFoundException("cannot read dictionary");
                }

                OperatorNestIn = "(?:";
                if (migemoStruct.charset == CharSet.Cp932)
                {
                    int2char_delegate = ProcAnsiInt2Char;
                    migemo_setproc_int2char(MigemoObject, int2char_delegate);
                }
                else if (migemoStruct.charset == CharSet.UTF8)
                {
                    int2char_delegate = ProcUtf8Int2Char;
                    migemo_setproc_int2char(MigemoObject, int2char_delegate);
                }

                AppDomain.CurrentDomain.ProcessExit += (o, e) =>
                {
                    Dispose();
                };
            }
            catch (Exception ex)
            {
                throw new Exception("cannot load migemo.dll or dictionary", ex);
            }
        }