diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/BLua.cc | 98 | ||||
-rw-r--r-- | lib/LuaHandle.cc | 66 | ||||
-rw-r--r-- | lib/String.cc | 13 |
3 files changed, 166 insertions, 11 deletions
diff --git a/lib/BLua.cc b/lib/BLua.cc index c15e6d9..bdb35d2 100644 --- a/lib/BLua.cc +++ b/lib/BLua.cc @@ -14,6 +14,12 @@ class LuaStatics : public Base { static int putF(lua_State *, const void *, size_t, void *);
static int luapanic(lua_State *) throw(GeneralException);
static int destructor(lua_State *);
+
+ static int andB(lua_State *);
+ static int orB(lua_State *);
+ static int xorB(lua_State *);
+ static int notB(lua_State *);
+ static int hex(lua_State *);
};
std::map<lua_State *, Lua *> Lua::lualist;
@@ -23,9 +29,101 @@ int LuaStatics::luapanic(lua_State * L) throw (GeneralException) { throw LuaException("Error running Lua code, bailing out.");
}
+int LuaStatics::andB(lua_State * _L) {
+ Lua * L = Lua::find(_L);
+ int n = L->gettop();
+ int a, b;
+
+ if ((n != 2) && !L->isnumber(1) && !L->isnumber(2)) {
+ L->error("Incorrect arguments to function `andB'");
+ }
+
+ a = L->tonumber(1);
+ b = L->tonumber(2);
+
+ L->push((lua_Number) (a & b));
+
+ return 1;
+}
+
+int LuaStatics::orB(lua_State * _L) {
+ Lua * L = Lua::find(_L);
+ int n = L->gettop();
+ int a, b;
+
+ if ((n != 2) && !L->isnumber(1) && !L->isnumber(2)) {
+ L->error("Incorrect arguments to function `orB'");
+ }
+
+ a = L->tonumber(1);
+ b = L->tonumber(2);
+
+ L->push((lua_Number) (a | b));
+
+ return 1;
+}
+
+int LuaStatics::xorB(lua_State * _L) {
+ Lua * L = Lua::find(_L);
+ int n = L->gettop();
+ int a, b;
+
+ if ((n != 2) && !L->isnumber(1) && !L->isnumber(2)) {
+ L->error("Incorrect arguments to function `xorB'");
+ }
+
+ a = L->tonumber(1);
+ b = L->tonumber(2);
+
+ L->push((lua_Number) (a ^ b));
+
+ return 1;
+}
+
+int LuaStatics::notB(lua_State * _L) {
+ Lua * L = Lua::find(_L);
+ int n = L->gettop();
+ int x;
+
+ if ((n != 1) && !L->isnumber()) {
+ L->error("Incorrect arguments to function `notB'");
+ }
+
+ x = L->tonumber();
+
+ L->push((lua_Number) (~x));
+
+ return 1;
+}
+
+int LuaStatics::hex(lua_State * _L) {
+ Lua * L = Lua::find(_L);
+ int n = L->gettop();
+ int x;
+ String r;
+
+ if (((n != 1) || (n != 2)) && !L->isnumber(1) && ((n == 2) && !L->isstring(2))) {
+ L->error("Incorrect arguments to function `hex'");
+ }
+
+ x = L->tonumber(1);
+ String fmt = n == 2 ? L->tostring() : "%x";
+ r.set(fmt.to_charp(), x);
+
+ L->push(r);
+
+ return 1;
+}
+
Lua::Lua() : L(lua_open()) {
lua_atpanic(L, LuaStatics::luapanic); lualist[L] = this;
+
+ declarefunc("andB", LuaStatics::andB);
+ declarefunc("orB", LuaStatics::orB);
+ declarefunc("xorB", LuaStatics::xorB);
+ declarefunc("notB", LuaStatics::notB);
+ declarefunc("hex", LuaStatics::hex);
}
Lua::Lua(lua_State * _L) : L(_L) {
diff --git a/lib/LuaHandle.cc b/lib/LuaHandle.cc index 2a8996e..f0e0706 100644 --- a/lib/LuaHandle.cc +++ b/lib/LuaHandle.cc @@ -17,10 +17,12 @@ class sLuaHandle : public Base { static int newinput(lua_State * L);
static int newoutput(lua_State * L);
static int read(lua_State * L);
- static int write(lua_State * L);
+ static int readstring(lua_State * L);
static int readU8(lua_State * L);
static int readU16(lua_State * L);
static int readU32(lua_State * L);
+ static int write(lua_State * L);
+ static int writestring(lua_State * L);
static int writeU8(lua_State * L);
static int writeU16(lua_State * L);
static int writeU32(lua_State * L);
@@ -105,6 +107,10 @@ int sLuaHandle::read(lua_State * _L) { Handle * h;
Byte * b;
+ if (n == 1) {
+ return readstring(_L);
+ }
+
if ((n != 2) || !L->isnumber()) {
L->error("Incorrect arguments to method `Headle::read'");
}
@@ -130,6 +136,37 @@ int sLuaHandle::read(lua_State * _L) { return 2;
}
+int sLuaHandle::readstring(lua_State * _L) {
+ Lua * L = Lua::find(_L);
+ int n = L->gettop(), i;
+ Handle * h;
+ String r;
+
+ if (n != 1) {
+ L->error("Incorrect arguments to method `Headle::readstring'");
+ }
+
+ h = (Handle *) LuaObject::getme(L);
+
+ (*h) >> r;
+
+ L->push(r);
+
+ return 1;
+}
+
+int sLuaHandle::readU8(lua_State * L) {
+ return read(L, U8);
+}
+
+int sLuaHandle::readU16(lua_State * L) {
+ return read(L, U16);
+}
+
+int sLuaHandle::readU32(lua_State * L) {
+ return read(L, U32);
+}
+
int sLuaHandle::write(lua_State * _L) {
Lua * L = Lua::find(_L);
int n = L->gettop(), i;
@@ -138,6 +175,10 @@ int sLuaHandle::write(lua_State * _L) { Handle * h;
Byte * b;
+ if ((n == 2) && L->isstring()) {
+ return writestring(_L);
+ }
+
if ((n != 3) || !L->isnumber() || !L->istable(2)) {
L->error("Incorrect arguments to method `Headle::write'");
}
@@ -162,16 +203,22 @@ int sLuaHandle::write(lua_State * _L) { return 1;
}
-int sLuaHandle::readU8(lua_State * L) {
- return read(L, U8);
-}
+int sLuaHandle::writestring(lua_State * _L) {
+ Lua * L = Lua::find(_L);
+ int n = L->gettop(), i;
+ Handle * h;
+ String r;
-int sLuaHandle::readU16(lua_State * L) {
- return read(L, U16);
-}
+ if ((n != 2) || !L->isstring()) {
+ L->error("Incorrect arguments to method `Headle::writestring'");
+ }
-int sLuaHandle::readU32(lua_State * L) {
- return read(L, U32);
+ h = (Handle *) LuaObject::getme(L);
+ r = L->tostring();
+
+ (*h) << r;
+
+ return 0;
}
int sLuaHandle::writeU8(lua_State * L) {
@@ -459,5 +506,4 @@ void LuaHandle::pushmembers(Lua * L) { L->push("SEEK_END");
L->push((lua_Number) SEEK_END);
L->settable(LUA_GLOBALSINDEX);
-
}
diff --git a/lib/String.cc b/lib/String.cc index 82b0e00..04fca01 100644 --- a/lib/String.cc +++ b/lib/String.cc @@ -137,10 +137,21 @@ const char * String::set(const char * s, va_list ap) { #else // !HAVE_VASPRINTF #ifdef HAVE_VSNPRINTF vsnprintf(t, BUFSIZ, s, ap); + str = Base::strdup(r = t); #else // !HAVE_VSNPRINTF +#ifdef _WIN32 +#ifdef _MSC_VER + r = str = (char *) malloc(_vscprintf(s, ap) + 1); + vsprintf(str, s, ap); +#else + _vsnprintf(t, BUFSIZ, s, ap); + str = Base::strdup(r = t); +#endif +#else vsprintf(t, s, ap); -#endif // HAVE_VSNPRINTF str = Base::strdup(r = t); +#endif +#endif // HAVE_VSNPRINTF #endif // HAVE_VASPRINTF #endif // HAVE_GMP siz = ::strlen(str); |