Skip to content

Commit

Permalink
bugfix, and add testall.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwu committed Jul 28, 2014
1 parent 20c579f commit e22e4ba
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 110 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -7,7 +7,7 @@ sproto.so : sproto.c lsproto.c
gcc -O2 -Wall -fPIC --shared -o $@ $^

sproto.dll : sproto.c lsproto.c
gcc -g -Wall --shared -o $@ $^ -I/usr/local/include -L/usr/local/bin -llua52
gcc -O2 -Wall --shared -o $@ $^ -I/usr/local/include -L/usr/local/bin -llua52

clean :
rm -f sproto.so sproto.dll
14 changes: 9 additions & 5 deletions lsproto.c
@@ -1,10 +1,11 @@
#include <string.h>
#include <stdint.h>

#include "lua.h"
#include "lauxlib.h"
#include "sproto.h"

#define ENCODE_BUFFERSIZE 2
#define ENCODE_BUFFERSIZE 2050

//#define ENCODE_BUFFERSIZE 2050
#define ENCODE_MAXSIZE 0x1000000
Expand Down Expand Up @@ -80,20 +81,23 @@ encode(void *ud, const char *tagname, int type, int index, struct sproto_type *s
switch (type) {
case SPROTO_TINTEGER: {
lua_Integer v = luaL_checkinteger(L, -1);
*(lua_Integer *)value = v;
lua_pop(L,1);
// notice: in lua 5.2, lua_Integer maybe 52bit
lua_Integer vh = v >> 31;
if (vh == 0 || vh == -1)
if (vh == 0 || vh == -1) {
*(uint32_t *)value = (uint32_t)v;
return 4;
else
}
else {
*(uint64_t *)value = (uint64_t)v;
return 8;
}
}
case SPROTO_TBOOLEAN: {
int v = lua_toboolean(L, -1);
*(int *)value = v;
lua_pop(L,1);
return 1;
return 4;
}
case SPROTO_TSTRING: {
size_t sz = 0;
Expand Down
31 changes: 31 additions & 0 deletions print_r.lua
@@ -0,0 +1,31 @@
local print = print
local tconcat = table.concat
local tinsert = table.insert
local srep = string.rep
local type = type
local pairs = pairs
local tostring = tostring
local next = next

local function print_r(root)
local cache = { [root] = "." }
local function _dump(t,space,name)
local temp = {}
for k,v in pairs(t) do
local key = tostring(k)
if cache[v] then
tinsert(temp,"+" .. key .. " {" .. cache[v].."}")
elseif type(v) == "table" then
local new_key = name .. "." .. key
cache[v] = new_key
tinsert(temp,"+" .. key .. _dump(v,space .. (next(t,k) and "|" or " " ).. srep(" ",#key),new_key))
else
tinsert(temp,"+" .. key .. " [" .. tostring(v).."]")
end
end
return tconcat(temp,"\n"..space)
end
print(_dump(root, "",""))
end

return print_r
5 changes: 3 additions & 2 deletions sproto.c
Expand Up @@ -669,7 +669,6 @@ encode_array(sproto_callback cb, void *ud, struct field *f, uint8_t *data, int s
buffer[6] = 0;
buffer[7] = 0;
}
return -1;
} else {
if (sz != sizeof(uint64_t))
return -1;
Expand Down Expand Up @@ -762,6 +761,7 @@ sproto_encode(struct sproto_type *st, void * buffer, int size, sproto_callback c
if (sz == sizeof(uint32_t)) {
if (u.u32 < 0x7fff) {
value = (u.u32+1) * 2;
printf("value = %d\n", value);
sz = 2; // sz can be any number > 0
} else {
sz = encode_integer(u.u32, data, size);
Expand Down Expand Up @@ -946,7 +946,8 @@ sproto_decode(struct sproto_type *st, const void * data, int size, sproto_callba
} else if (f->type != SPROTO_TINTEGER && f->type != SPROTO_TBOOLEAN) {
return -1;
} else {
cb(ud, f->name, f->type, 0, NULL, &value, sizeof(value));
uint64_t v = value;
cb(ud, f->name, f->type, 0, NULL, &v, sizeof(v));
}
}
return 0;
Expand Down
102 changes: 0 additions & 102 deletions sprotoparser.lua
Expand Up @@ -380,105 +380,3 @@ function sparser.parse(text, name)
end

return sparser

--[[
local function packzeroseg(str, from)
local h = 0
local r = ""
for i = 0, 7 do
local index = i + from
local c = str:byte(index) or 0
if c ~= 0 then
h = h + bit32.lshift(1, i)
r = r .. str:sub(index,index)
end
end
return #r, string.char(h) .. r
end
local seg = 0
local function packzero(bytes)
local result = {}
local ff = 0
local ffstart = 0
for i = 1, #bytes, 8 do
local n,t = packzeroseg(bytes, i)
if n == 7 or n == 6 then
if ff > 0 then
n = 8
end
end
if n == 8 then
if ff == 0 then
ffstart = i
end
ff = ff + 1
elseif ff > 0 then
table.insert(result,"\255" .. string.char(ff-1))
table.insert(result, string.sub(bytes, ffstart, ffstart + ff * 8 - 1))
ff = 0
else
seg = seg + #t
table.insert(result, t)
end
end
if ff > 0 then
table.insert(result, "\255" .. string.char(ff-1))
table.insert(result, string.sub(bytes, ffstart, ffstart + ff * 8 - 1))
end
return table.concat(result)
end
]]

--[=[
local text=[[
.type {
.field {
test 4 : foobar
name 0 : string
type 1 : string
id 2 : integer
array 3 : boolean
}
.foobar {
}
name 0 : string
fields 1 : *field
}
foobar 1 {
request type
response {
ok 0 : boolean
}
}
hello 0 {
request {}
}
]]
local r = parser(text, "text")
local print_r = require "print_r"
print_r(r)
local data = encodeall(r)
print(#data)
dump(data)
local f = io.open("p.spbin","wb")
f:write(data)
f:close()
--print("========pack")
--data = packzero(data)
--print(#data)
--dump(data)
]=]
53 changes: 53 additions & 0 deletions testall.lua
@@ -0,0 +1,53 @@
local parser = require "sprotoparser"
local core = require "sproto.core"
local print_r = require "print_r"

local sp = parser.parse [[
.foobar {
.nest {
a 1 : string
b 3 : boolean
c 5 : integer
}
a 0 : string
b 1 : integer
c 2 : boolean
d 3 : nest
e 4 : *string
f 5 : *integer
g 6 : *boolean
h 7 : *foobar
}
]]

sp = core.newproto(sp)
core.dumpproto(sp)

local st = core.querytype(sp, "foobar")

local obj = {
a = "hello",
b = 1000000,
c = true,
d = {
a = "world",
-- skip b
c = -1,
},
e = { "ABC", "def" },
f = { -3, -2, -1, 0 , 1, 2},
g = { true, false, true },
h = {
{ b = 100 },
{ b = -100, c= false },
{ b = 0, e = { "test" } },
},
}

local code = core.encode(st, obj)
parser.dump(code)
obj = core.decode(st, code)
print_r(obj)


0 comments on commit e22e4ba

Please sign in to comment.