#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);
      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的源码里扒出来的。apt source fcitx-sunpinyin下载它的源码。

编译需要安装fcitx-libs-dev

835行。

在两个坑里呆了很久:

  • wcstombs(char dest, const wchar_t *src, size_t n); n 是输出的size. At most n bytes are written to dest.
  • 用for循环print完p的十六进制后,wcstombs(p),出来一堆垃圾数据。

最后,man wcs2mbs,没有。

typedef unsigned int TWCHAR;

#include <stdio.h>
int main() {
  wchar_t*  x;
  int*  y = x;
  unsigned* z  = x;
  return 0;
}
---
t.cpp:4:21: error: invalid conversion from ‘wchar_t*’ to ‘int*’ [-fpermissive]
4 |         int*    y = x;
|                     ^
|                     |
|                     wchar_t*
t.cpp:5:24: error: invalid conversion from ‘wchar_t*’ to ‘unsigned int*’ [-fpermissive]
5 |         unsigned* z  = x;
|                        ^
|                        |
|                        wchar_t*

一个size是4的玩艺,既不是int, 也不是unsigned int,还不是uint32_t和int32_t!

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