summaryrefslogtreecommitdiff
path: root/src/ffi
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2018-10-12 17:33:32 +0800
committer最萌小汐 <sumneko@hotmail.com>2018-10-12 17:33:32 +0800
commit12efa3035f85df148b23e54e051b0769572d1589 (patch)
tree08f16d7317068e6410086dcf88c16041413e9ce1 /src/ffi
parent87dd78bf46c4ff0d02f851acd927e3d3f521392c (diff)
downloadlua-language-server-12efa3035f85df148b23e54e051b0769572d1589.zip
先打印一下标准输入
Diffstat (limited to 'src/ffi')
-rw-r--r--src/ffi/sleep.lua8
-rw-r--r--src/ffi/unicode.lua49
2 files changed, 57 insertions, 0 deletions
diff --git a/src/ffi/sleep.lua b/src/ffi/sleep.lua
new file mode 100644
index 00000000..5c4be639
--- /dev/null
+++ b/src/ffi/sleep.lua
@@ -0,0 +1,8 @@
+local ffi = require 'ffi'
+ffi.cdef[[
+ void Sleep(unsigned long dwMilliseconds);
+]]
+
+return function (time)
+ ffi.C.Sleep(time)
+end
diff --git a/src/ffi/unicode.lua b/src/ffi/unicode.lua
new file mode 100644
index 00000000..734b4679
--- /dev/null
+++ b/src/ffi/unicode.lua
@@ -0,0 +1,49 @@
+local ffi = require 'ffi'
+ffi.cdef[[
+ int MultiByteToWideChar(unsigned int CodePage, unsigned long dwFlags, const char* lpMultiByteStr, int cbMultiByte, wchar_t* lpWideCharStr, int cchWideChar);
+ int WideCharToMultiByte(unsigned int CodePage, unsigned long dwFlags, const wchar_t* lpWideCharStr, int cchWideChar, char* lpMultiByteStr, int cchMultiByte, const char* lpDefaultChar, int* pfUsedDefaultChar);
+]]
+
+local CP_UTF8 = 65001
+local CP_ACP = 0
+
+local function u2w(input)
+ local wlen = ffi.C.MultiByteToWideChar(CP_UTF8, 0, input, #input, nil, 0)
+ local wstr = ffi.new('wchar_t[?]', wlen+1)
+ ffi.C.MultiByteToWideChar(CP_UTF8, 0, input, #input, wstr, wlen)
+ return wstr, wlen
+end
+
+local function a2w(input)
+ local wlen = ffi.C.MultiByteToWideChar(CP_ACP, 0, input, #input, nil, 0)
+ local wstr = ffi.new('wchar_t[?]', wlen+1)
+ ffi.C.MultiByteToWideChar(CP_ACP, 0, input, #input, wstr, wlen)
+ return wstr, wlen
+end
+
+local function w2u(wstr, wlen)
+ local len = ffi.C.WideCharToMultiByte(CP_UTF8, 0, wstr, wlen, nil, 0, nil, nil)
+ local str = ffi.new('char[?]', len+1)
+ ffi.C.WideCharToMultiByte(CP_UTF8, 0, wstr, wlen, str, len, nil, nil)
+ return ffi.string(str)
+end
+
+local function w2a(wstr, wlen)
+ local len = ffi.C.WideCharToMultiByte(CP_ACP, 0, wstr, wlen, nil, 0, nil, nil)
+ local str = ffi.new('char[?]', len)
+ ffi.C.WideCharToMultiByte(CP_ACP, 0, wstr, wlen, str, len, nil, nil)
+ return ffi.string(str)
+end
+
+return {
+ u2w = u2w,
+ a2w = a2w,
+ w2u = w2u,
+ w2a = w2a,
+ u2a = function (input)
+ return w2a(u2w(input))
+ end,
+ a2u = function (input)
+ return w2u(a2w(input))
+ end,
+}