aboutsummaryrefslogtreecommitdiff
path: root/src/lua/net/luaopen_Buffer.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2018-08-09 22:19:28 +0800
committerchai <chaifix@163.com>2018-08-09 22:19:28 +0800
commit5fe41eca99adf4bf0fb5832033a96f98b530d4f1 (patch)
treef5144fce7ab194d122f84bf0de820b6b2c9525fa /src/lua/net/luaopen_Buffer.cpp
parenta03a35b6cfe6399ceaff86a1cc035f1131427955 (diff)
*update
Diffstat (limited to 'src/lua/net/luaopen_Buffer.cpp')
-rw-r--r--src/lua/net/luaopen_Buffer.cpp191
1 files changed, 191 insertions, 0 deletions
diff --git a/src/lua/net/luaopen_Buffer.cpp b/src/lua/net/luaopen_Buffer.cpp
new file mode 100644
index 0000000..6e10f25
--- /dev/null
+++ b/src/lua/net/luaopen_Buffer.cpp
@@ -0,0 +1,191 @@
+#include "lua/luax.h"
+#include "../luaopen_types.h"
+#include "libjin/jin.h"
+
+namespace jin
+{
+namespace lua
+{
+namespace net
+{
+
+ class Buffer
+ {
+ public:
+ Buffer(size_t size);
+ Buffer(const char* data, size_t size);
+ ~Buffer()
+ {
+ }
+
+ void append(char* data, size_t size)
+ {
+
+ }
+
+ const char* grabString(int offset = 0)
+ {
+ int l = offset;
+ for (; l < size; ++l)
+ {
+ if (buffer[l] == 0)
+ {
+
+ }
+ }
+ return nullptr;
+ }
+
+ int grabInteger(int offset = 0)
+ {
+
+ }
+
+ float grabfloat(int offset = 0)
+ {
+
+ }
+
+ bool grabboolean(int offset = 0)
+ {
+
+ }
+
+ private:
+ char* buffer;
+ size_t size;
+
+ };
+
+ static int l_buffer(lua_State* L)
+ {
+ int size = luax_checkinteger(L, 1);
+ char* buffer = (char*)malloc(size);
+ memset(buffer, 0, size);
+ luax_pushlstring(L, buffer, size);
+ return 1;
+ }
+
+ // return buffer, size
+ static int l_write(lua_State* L)
+ {
+ const char* data = luax_checklstring(L, 1, NULL);
+ int len = luax_checkinteger(L, 2);
+ if (luax_isinteger(L, 3))
+ {
+ int n = luax_checkinteger(L, 3);
+ int size = len + sizeof(n);
+ char* buffer = (char*)malloc(size);
+ memcpy(buffer, data, len);
+ memcpy(buffer + len, &n, sizeof(n));
+ //free((void*)data);
+ luax_pushlstring(L, buffer, size);
+ luax_pushinteger(L, size);
+ return 2;
+ }
+ else if (luax_isfloat(L, 3))
+ {
+ float n = luax_checknumber(L, 3);
+ int size = len + sizeof(n);
+ char* buffer = (char*)malloc(size);
+ memcpy(buffer, data, len);
+ memcpy(buffer + len, &n, sizeof(n));
+ //free((void*)data);
+ luax_pushlstring(L, buffer, size);
+ luax_pushinteger(L, size);
+ return 2;
+ }
+ else if (luax_isboolean(L, 3))
+ {
+ bool b = luax_checkbool(L, 3);
+ int size = len + sizeof(b);
+ char* buffer = (char*)malloc(size);
+ memcpy(buffer, data, len);
+ memcpy(buffer + len, &b, sizeof(b));
+ //free((void*)data);
+ luax_pushlstring(L, buffer, size);
+ luax_pushinteger(L, size);
+ return 2;
+ }
+ else if (luax_isstring(L, 3))
+ {
+ const char* s = luax_checkstring(L, 3);
+ int l = strlen(s) + 1; // with \0
+ int size = len + l;
+ char* buffer = (char*)malloc(size);
+ memcpy(buffer, data, len);
+ memcpy(buffer + len, s, l);
+ luax_pushlstring(L, buffer, size);
+ luax_pushinteger(L, size);
+ return 2;
+ }
+ else
+ {
+ luax_typerror(L, 3, "number, bool or string");
+ return 0;
+ }
+ }
+
+ // jin.shift(buffer, shift)
+ static int l_shift(lua_State* L)
+ {
+ const char* buffer = luax_checklstring(L, 1, NULL);
+ int size = luax_checkinteger(L, 2);
+ int shift = luax_checkinteger(L, 3);
+ int ss = size - shift;
+ luax_pushlstring(L, buffer + shift, ss);
+ luax_pushinteger(L, ss);
+ return 2;
+ }
+
+ // jin.bit.grabstring(buffer, size)
+ static int l_grabstring(lua_State* L)
+ {
+ const char* buffer = luax_checklstring(L, 1, NULL);
+ int size = luax_checkinteger(L, 2);
+ int l = 0;
+ for (; l < size; ++l)
+ {
+ if (buffer[l] == 0)
+ {
+ int len = l + 1;
+ char* str = (char*)malloc(len);
+ memcpy(str, buffer, len);
+ luax_pushstring(L, str);
+ luax_pushinteger(L, l);
+ return 2;
+ }
+ }
+ return 0;
+ }
+
+ static int l_grabinteger(lua_State* L)
+ {
+ const char* buffer = luax_checklstring(L, 1, NULL);
+ int size = luax_checkinteger(L, 2);
+ int n = *((int*)buffer);
+ luax_pushinteger(L, n);
+ return 1;
+ }
+
+ static int l_grabfloat(lua_State* L)
+ {
+ const char* buffer = luax_checklstring(L, 1, NULL);
+ int size = luax_checkinteger(L, 2);
+ float n = *((float*)buffer);
+ luax_pushnumber(L, n);
+ return 1;
+ }
+
+ static int l_grabboolean(lua_State* L)
+ {
+ const char* buffer = luax_checklstring(L, 1, NULL);
+ int size = luax_checkinteger(L, 2);
+ bool n = *((bool*)buffer);
+ luax_pushboolean(L, n);
+ return 1;
+ }
+
+} // net
+} // lua
+} // jin \ No newline at end of file