sunpinyin online的简单例子

#include <stdio.h>
#include <sunpinyin.h>

class WinHandler : public CIMIWinHandler {
   void enableDeferedUpdate(CIMIView* view, int waitTime) {}
   void disableDeferedUpdate() {}
   void doneDeferedUpdate() {}

  /** commit a string, normally the converted result */
   void commit(const TWCHAR* wstr) {}

  /** when a key is not processed */
   void throwBackKey(unsigned keycode,
     unsigned keyvalue,
     unsigned modifier) {}

  /** Update window's preedit area. */
   void updatePreedit(const IPreeditString* ppd) {}

  /** Update window's candidates area. */
   void updateCandidates(const ICandidateList* pcl) {
    for (int i = 0; i < pcl->size(); i++) {
      const TWCHAR* p = pcl->candiString(i); // typedef unsigned int TWCHAR;
      char  s[64] = "";
      WCSTOMBS(s, p, sizeof(s));
      printf("%s\n", s);
    }
   }

  /** Update status area. */
  void updateStatus(int key, int value) {}
} windowHandler;

int main () {
  CSunpinyinSessionFactory& fac = CSunpinyinSessionFactory::getFactory();
  fac.setPinyinScheme(CSunpinyinSessionFactory::QUANPIN);
  CIMIView* view = fac.createSession();
  view->getIC()->setCharsetLevel(3);
  view->attachWinHandler(&windowHandler);
  view->onKeyEvent(CKeyEvent('c', 'c', 0));
  return 0;
}

上述代码是阅读了fcitx-sunpinyin的源码后写出来的,编译上述代码需要sunpinyin的源码或dev包。

apt source fcitx-sunpinyin可下载源码。编译fcitx-sunpinyin需要安装fcitx-libs-dev


  • getwc()和fgetwc()返回wint_t
  • mbstowcs、wctomb等使用wchar_t
  • std::wstring是basic_string<wchar_t>
    • sunpinyin: typedef std::basic_string<TWCHAR>   wstring;

64位系统下,虽然它们的size都是4,但:

  • int i; unsigned* p = &i; // error
  • warning: narrowing conversion of ... from ‘wint_t’ {aka ‘unsigned int’} to ‘wchar_t’
    •  aka : also known as

 

posted @ 2025-10-31 00:27  华容道专家  阅读(3)  评论(0)    收藏  举报