From 3c9f4621d2459366bb6fe6120991baad41ce6fc0 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sun, 27 Aug 2017 12:35:42 +0100 Subject: [PATCH] [Minor] Allow to get rspamd text from torch storage --- contrib/torch/torch7/Storage.c | 6 ++++++ contrib/torch/torch7/generic/Storage.c | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/contrib/torch/torch7/Storage.c b/contrib/torch/torch7/Storage.c index 874838e8c..730d97476 100644 --- a/contrib/torch/torch7/Storage.c +++ b/contrib/torch/torch7/Storage.c @@ -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) diff --git a/contrib/torch/torch7/generic/Storage.c b/contrib/torch/torch7/generic/Storage.c index a6652a5da..b936e5714 100644 --- a/contrib/torch/torch7/generic/Storage.c +++ b/contrib/torch/torch7/generic/Storage.c @@ -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 -- 2.39.5