分类: C/C++
2013-01-28 15:54:13
C代码: 参考
//-------- Array Begin -------------------------------------------------------------------- //C语言不允许大小为0的数组,这个1只是一个占位符, newarray 中重新给数组分配空间 typedef struct NumArray { size_t size; double values[1]; } NumArray; static NumArray *checkarray (lua_State *L) { void *ud = luaL_checkudata(L, 1, "LuaBook.array"); luaL_argcheck(L, ud != NULL, 1, "`array' expected"); return (NumArray *)ud; } static double *getelem (lua_State *L) { NumArray *a = checkarray(L); int index = luaL_checkint(L, 2); luaL_argcheck(L, 1 <= index && index <= a->size, 2, "index out of range"); /* return element address */ return &a->values[index - 1]; } static int newarray (lua_State *L) { int n = luaL_checkint(L, 1); //由于原始的结构中已经包含了一个元素的空间, 因此实际分配时从n中减去1 size_t nbytes = sizeof(NumArray) + (n - 1)*sizeof(double); NumArray *a = (NumArray *)lua_newuserdata(L, nbytes); luaL_getmetatable(L, "LuaBook.array"); lua_setmetatable(L, -2); a->size = n; return 1; /* new userdatum is already on the stack */ } static int setarray (lua_State *L) { #if 0 NumArray *a = (NumArray *)lua_touserdata(L, 1); int index = luaL_checkint(L, 2); double value = luaL_checknumber(L, 3); luaL_argcheck(L, a != NULL, 1, "`array' expected"); luaL_argcheck(L, 1 <= index && index <= a->size, 2, "index out of range"); a->values[index-1] = value; #else double newvalue = luaL_checknumber(L, 3); *getelem(L) = newvalue; #endif return 0; } static int getarray (lua_State *L) { #if 0 NumArray *a = (NumArray *)lua_touserdata(L, 1); int index = luaL_checkint(L, 2); luaL_argcheck(L, a != NULL, 1, "'array' expected"); luaL_argcheck(L, 1 <= index && index <= a->size, 2, "index out of range"); lua_pushnumber(L, a->values[index-1]); #else lua_pushnumber(L, *getelem(L)); #endif return 1; } static int getsize (lua_State *L) { #if 0 NumArray *a = (NumArray *)lua_touserdata(L, 1); luaL_argcheck(L, a != NULL, 1, "`array' expected"); #else NumArray *a = checkarray(L); #endif lua_pushnumber(L, a->size); return 1; } static const struct luaL_reg arraylib [] = { {"new", newarray}, {"set", setarray}, {"get", getarray}, {"size", getsize}, {NULL, NULL} }; int luaopen_array (lua_State *L) { luaL_newmetatable(L, "LuaBook.array"); luaL_openlib(L, "array", arraylib, 0); //用传统的 a[] 来操作数组, /* now the stack has the metatable at index 1 and 'array' at index 2 */ lua_pushstring(L, "__index"); lua_pushstring(L, "get"); lua_gettable(L, 2); /* get array.get */ lua_settable(L, 1); /* metatable.__index = array.get */ lua_pushstring(L, "__newindex"); lua_pushstring(L, "set"); lua_gettable(L, 2); /* get array.set */ lua_settable(L, 1); /* metatable.__newindex = array.set */ return 1; } //-------- Array End ------------------------------------------------------------ //--- lua中以字符串长度参数 static int crc8_hex(lua_State *L) { u8 ucRet; size_t Len; short n = lua_gettop(L); //参数个数 char *pcStr; char pcTmp[3]; pcStr = lua_tolstring(L, 1, &Len); //获取第一个字符串参数 lua_pop(L, 1); //pcStr = lua_tostring(L, 1); //DEBUGP("C:crc8-->argument %d, Len = %d, Data = %s, ", n, Len, pcStr); ucRet = CheckCRC8(pcStr, Len/2, 'H'); sprintf(pcTmp, "%02X", ucRet); lua_pushstring(L, (const char *)pcTmp); //返回值压栈 //STACK_LIST(L); return 1; //返回参数个数 } //--- lua中以数组传递参数 static int crc8(lua_State *L) { u8 ucret, ucdata[200]; size_t len, i; char pctmp[3]; NumArray *a = checkarray(L); len = lua_tonumber(L, 2); lua_pop(L, 2); for(i=0; ivalues[i]; DEBUGP("%02X ", ucdata[i]); } ucret = CheckCRC8(ucdata, len, '0'); sprintf(pctmp, "%02X", ucret); lua_pushstring(L, (const char *)pctmp); //返回值压栈 STACK_LIST(L); return 1; //返回参数个数 }
lua代码:
-- 数组测试
alen = 100
a = array.new(alen)
alen = 6 --有效数组长度,
for i=1,alen do
a[i] = 0xf0 + i
end
str = "array = "
for i=1,alen do
str = str .. string.format("%02X", a[i])
end
print(str)
ret = owlib.crc8(a, alen) --传递有效长度
print("crc8 = 0x" .. ret)