]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Allow to get rspamd text from torch storage
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sun, 27 Aug 2017 11:35:42 +0000 (12:35 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sun, 27 Aug 2017 11:36:19 +0000 (12:36 +0100)
contrib/torch/torch7/Storage.c
contrib/torch/torch7/generic/Storage.c

index 874838e8c25171e0ecab908687ff54561adc41bc..730d974767e05e84ccf613b7a2a12c489dade4a5 100644 (file)
@@ -1,5 +1,11 @@
 #include "general.h"
 
+struct _rspamd_lua_text {
+       const char *start;
+       unsigned int len;
+       unsigned int flags;
+};
+
 #define torch_Storage_(NAME) TH_CONCAT_4(torch_,Real,Storage_,NAME)
 #define THFile_readRealRaw TH_CONCAT_3(THFile_read, Real, Raw)
 #define THFile_writeRealRaw TH_CONCAT_3(THFile_write, Real, Raw)
index a6652a5da7d23e830ce7ac581737b485db6396c5..b936e5714cfb69b5472a5579a50c5434ea6776b0 100644 (file)
@@ -213,6 +213,28 @@ static int torch_Storage_(string)(lua_State *L)
 }
 #endif
 
+static int torch_Storage_(text)(lua_State *L)
+{
+  THStorage *storage = luaT_checkudata(L, 1, torch_Storage);
+  struct _rspamd_lua_text *t;
+
+  if(lua_type(L, -1) == LUA_TUSERDATA)
+  {
+    t = lua_touserdata(L, -1);
+    THStorage_(resize)(storage, t->len);
+    memmove(storage->data, t->start, t->len);
+    lua_settop(L, 1);
+  }
+  else {
+    t = lua_newuserdata (L, sizeof (*t));
+    t->start = (const char *)storage->data;
+    t->len = t->len;
+    t->flags = 0;
+  }
+
+  return 1; /* either storage or string */
+}
+
 static int torch_Storage_(totable)(lua_State *L)
 {
   THStorage *storage = luaT_checkudata(L, 1, torch_Storage);
@@ -274,6 +296,7 @@ static const struct luaL_Reg torch_Storage_(_) [] = {
   {"totable", torch_Storage_(totable)},
   {"write", torch_Storage_(write)},
   {"read", torch_Storage_(read)},
+  {"text", torch_Storage_(text)},
 #if defined(TH_REAL_IS_CHAR) || defined(TH_REAL_IS_BYTE)
   {"string", torch_Storage_(string)},
 #endif