From 008b684090a2c0f1ca6e1fb3a7c4587de1fb9bfd Mon Sep 17 00:00:00 2001 From: Pixel Date: Mon, 5 Oct 2009 16:53:21 -0700 Subject: Some sanity changes. - Making the whole library a little bit more threadsafe. - Truly deprecating the to_charp() call - let's see what will break now, and fix it. - Adding the "sleep" call to Lua, in ms. --- lib/BLua.cc | 27 ++++++++++++++++++++++++++- lib/Exceptions.cc | 10 +++++----- lib/LuaOCCI.cc | 2 +- lib/String.cc | 54 +++++++++++++++++++++++++----------------------------- 4 files changed, 57 insertions(+), 36 deletions(-) (limited to 'lib') diff --git a/lib/BLua.cc b/lib/BLua.cc index f7f8d53..76cd443 100644 --- a/lib/BLua.cc +++ b/lib/BLua.cc @@ -88,6 +88,8 @@ class LuaStatics : public Base { static int getenv(lua_State *); static int setenv(lua_State *); static int unsetenv(lua_State *); + + static int sleep(lua_State *); static int globalindex(lua_State *); @@ -361,7 +363,7 @@ int LuaStatics::unsetenv(lua_State * _L) { int n = L->gettop(); /* number of arguments */ if (n != 1) { - L->error("Incorrect arguments to function `setenv'"); + L->error("Incorrect arguments to function `unsetenv'"); } ::unsetenv(L->tostring(1).to_charp()); @@ -369,6 +371,26 @@ int LuaStatics::unsetenv(lua_State * _L) { return 0; } +int LuaStatics::sleep(lua_State * _L) { + Lua * L = Lua::find(_L); + int t; + + int n = L->gettop(); /* number of arguments */ + if ((n != 1) || !L->isnumber(1)) { + L->error("Incorrect arguments to function `sleep'"); + } + + t = L->tonumber(1); + +#ifdef _WIN32 + ::Sleep(t); +#else + ::usleep(t * 1000); +#endif + + return 0; +} + int LuaStatics::print(lua_State * _L) { Lua * L = Lua::find(_L); @@ -556,6 +578,9 @@ void Lua::open_base() { push("unsetenv"); push(LuaStatics::unsetenv); settable(LUA_GLOBALSINDEX); + push("sleep"); + push(LuaStatics::sleep); + settable(LUA_GLOBALSINDEX); } void Lua::open_table() { diff --git a/lib/Exceptions.cc b/lib/Exceptions.cc index 5c14404..a42f416 100644 --- a/lib/Exceptions.cc +++ b/lib/Exceptions.cc @@ -45,24 +45,22 @@ #include "generic.h" #include "gettext.h" -char GeneralException::t[BUFSIZ]; - std::vector Base::context; GeneralException::GeneralException(const String & emsg) : msg(emsg.strdup()) { - UNLOCK; +// UNLOCK; #ifdef DEBUG printm(M_BARE, String(_("Generating a General Exception error: '")) + msg + "'.\n"); #endif } GeneralException::GeneralException() : msg(0) { - UNLOCK; +// UNLOCK; #ifdef DEBUG printm(M_BARE, String(_("Generating a General Exception error: '")) + msg + "'.\n"); #endif } GeneralException::GeneralException(const GeneralException & e) : msg(strdup(e.msg)) { - UNLOCK; +// UNLOCK; #ifdef DEBUG printm(M_BARE, String(_("Generating a General Exception error: '")) + msg + "'.\n"); #endif @@ -79,11 +77,13 @@ const char * GeneralException::GetMsg() const { } MemoryException::MemoryException(ssize_t s) { + char t[BUFSIZ]; sprintf(t, _("Failed allocating %u bytes."), s); msg = strdup(t); } IOException::IOException(const String & fn, op_t op, ssize_t s) { + char t[BUFSIZ]; sprintf(t, _("An error has occured while %s %u bytes on %s: %s"), op == IO_WRITE ? _("writing") : _("reading"), s, fn.to_charp(), strerror(errno)); msg = strdup(t); diff --git a/lib/LuaOCCI.cc b/lib/LuaOCCI.cc index 8bd507b..9fa9332 100644 --- a/lib/LuaOCCI.cc +++ b/lib/LuaOCCI.cc @@ -238,7 +238,7 @@ int sLua_EncapOCCI_Environment::EncapOCCI_Environment_proceed_statics(Lua * L, i try { switch (caller) { case ENVIRONMENT_CREATEENVIRONMENT: - env = new EncapOCCI_Environment(Environment::createEnvironment(Environment::DEFAULT)); + env = new EncapOCCI_Environment(Environment::createEnvironment(Environment::THREADED_MUTEXED)); if (!global_env) { global_env = env->Get(); } diff --git a/lib/String.cc b/lib/String.cc index 0b7b3e6..c960992 100644 --- a/lib/String.cc +++ b/lib/String.cc @@ -40,8 +40,6 @@ extern "C" { int isDateArgument(char *); } -char String::t[BUFSIZ + 1]; - String::String(const String & s) : str((char *) malloc(s.siz + 1)), siz(s.siz) { memcpy(str, s.str, siz); str[siz] = 0; @@ -157,26 +155,23 @@ const char * String::set(const char * s, va_list ap) { r = str; #else // !HAVE_VASPRINTF #ifdef HAVE_VSNPRINTF - LOCK; + char String::t[BUFSIZ + 1]; vsnprintf(t, BUFSIZ, s, ap); str = Base::strdup(r = t); - UNLOCK; #else // !HAVE_VSNPRINTF #ifdef _WIN32 #ifdef _MSC_VER r = str = (char *) malloc(_vscprintf(s, ap) + 1); vsprintf(str, s, ap); #else - LOCK; + char String::t[BUFSIZ + 1]; _vsnprintf(t, BUFSIZ, s, ap); str = Base::strdup(r = t); - UNLOCK; #endif #else - LOCK; + char String::t[BUFSIZ + 1]; vsprintf(t, s, ap); str = Base::strdup(r = t); - UNLOCK; #endif #endif // HAVE_VSNPRINTF #endif // HAVE_VASPRINTF @@ -248,18 +243,18 @@ const char * String::to_charp(size_t from, ssize_t to) const throw (GeneralExcep from -= (to - from) - BUFSIZ; } - if (((size_t) to) >= from) { - size_t i; - for (i = 0; i <= ((size_t) to) - from; i++) { - t[i] = str[i + from]; - } - t[i] = '\0'; - } else { - t[0] = '\0'; - } +// if (((size_t) to) >= from) { +// size_t i; +// for (i = 0; i <= ((size_t) to) - from; i++) { +// t[i] = str[i + from]; +// } +// t[i] = '\0'; +// } else { +// t[0] = '\0'; +// } } - //throw GeneralException("This usage of String is deprecated."); - return t; + throw GeneralException("This usage of String is deprecated."); +// return t; } String String::extract(size_t from, ssize_t to) const { @@ -370,19 +365,20 @@ size_t String::strlen() const { return (siz); } -char String::operator[](size_t i) const { +const char & String::operator[](size_t i) const { + static const char zero = 0; + if (i >= siz) { - return 0; + return zero; } else { return str[i]; } } -char & String::operator[](size_t i) { - static char zero = 0; - +char & String::operator[](size_t i) throw (GeneralException) { + if (i >= siz) { - return zero; + throw GeneralException("operator[] on String out of bounds"); } else { return str[i]; } @@ -580,16 +576,15 @@ String & String::iconv(const String & from, const String & _to) { #else const char * inbuf; #endif - char * outbuf; + char * outbuf, * t; size_t inleft, outleft; if ((cd = iconv_open(to.str, from.str)) == (iconv_t) (-1)) { return *this; } - LOCK; inbuf = str; - outbuf = t; + t = outbuf = (char *) malloc(BUFSIZ + 1); inleft = siz; outleft = BUFSIZ; memset(t, 0, BUFSIZ + 1); @@ -606,9 +601,10 @@ String & String::iconv(const String & from, const String & _to) { str[siz + 1] = 0; str[siz + 2] = 0; str[siz + 3] = 0; - UNLOCK; iconv_close(cd); + + free(t); return *this; } -- cgit v1.2.3