diff options
author | pixel <pixel> | 2008-01-25 10:09:19 +0000 |
---|---|---|
committer | pixel <pixel> | 2008-01-25 10:09:19 +0000 |
commit | df9c76bcd6d112367cdc72402e6b39d1d858e17c (patch) | |
tree | 2055b9274e1f4a1b50fac367a80517775f49b904 | |
parent | 8fd3464127b9ff15ae804253675088ba85e94990 (diff) |
Trying better string handling method - allowing embedded zeroes.
-rw-r--r-- | include/BString.h | 4 | ||||
-rw-r--r-- | lib/BLua.cc | 11 | ||||
-rw-r--r-- | lib/String.cc | 31 |
3 files changed, 23 insertions, 23 deletions
diff --git a/include/BString.h b/include/BString.h index 5db0db5..c070fe6 100644 --- a/include/BString.h +++ b/include/BString.h @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: BString.h,v 1.17 2007-09-05 14:11:44 pixel Exp $ */ +/* $Id: BString.h,v 1.18 2008-01-25 10:09:19 pixel Exp $ */ #ifndef __STRING_H__ #define __STRING_H__ @@ -36,7 +36,7 @@ struct ugly_string { class String : public Base { public: String(const String &); - String(const char * = ""); + String(const char * = "", int = -1); String(char); String(int); String(unsigned int); diff --git a/lib/BLua.cc b/lib/BLua.cc index 76a0c6d..1db9dda 100644 --- a/lib/BLua.cc +++ b/lib/BLua.cc @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: BLua.cc,v 1.57 2008-01-23 17:53:12 pixel Exp $ */ +/* $Id: BLua.cc,v 1.58 2008-01-25 10:09:19 pixel Exp $ */ #include <stdlib.h> #include "BLua.h" @@ -789,14 +789,17 @@ lua_Number Lua::tonumber(int i) { String Lua::tostring(int i) { const char * r = 0; + size_t l = -1; if (isnil(i)) { - r = ""; + r = "(nil)"; } else if (isboolean(i)) { r = toboolean(i) ? "true" : "false"; + } else if (isnumber(i)) { + return String(tonumber(i)); } else { - r = lua_tostring(L, i); + r = lua_tolstring(L, i, &l); } - return String(r ? r : "<lua-NULL>"); + return String(r ? r : "<lua-NULL>", l); } lua_CFunction Lua::tocfunction(int i) { diff --git a/lib/String.cc b/lib/String.cc index 948f012..529a8dd 100644 --- a/lib/String.cc +++ b/lib/String.cc @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: String.cc,v 1.48 2007-09-05 14:11:44 pixel Exp $ */ +/* $Id: String.cc,v 1.49 2008-01-25 10:09:19 pixel Exp $ */ #include <stdio.h> #include <string.h> @@ -44,7 +44,9 @@ extern "C" { char String::t[BUFSIZ + 1]; -String::String(const String & s) : str(Base::strdup(s.str)), siz(s.siz) { +String::String(const String & s) : str((char *) malloc(s.siz + 1)), siz(s.siz) { + memcpy(str, s.str, siz); + str[siz] = 0; #ifdef DEBUG fprintf(stderr, _("Duplicating string `%s', from %p to %p, from this %p to this %p\n"), str, s.str, str, &s, this); #endif @@ -58,16 +60,15 @@ String::String(char c) : siz(1) { #ifdef DEBUG fprintf(stderr, _("Creating a string with `%c' at %p, this = %p\n"), c, str, this); #endif -#ifndef HAVE_ASPRINTF - char * t = (char *) malloc(2); - sprintf(t, "%c", c); - str = t; -#else - asprintf(&str, "%c", c); -#endif + str = (char *) malloc(2); + str[0] = c; + str[1] = 0; } -String::String(const char * s) : str(Base::strdup(s)), siz(::strlen(str)) { +String::String(const char * s, int _siz) : str(0), siz(_siz < 0 ? ::strlen(s) : _siz) { + str = (char *) malloc(siz + 1); + memcpy(str, s, siz); + str[siz] = 0; #ifdef DEBUG fprintf(stderr, _("Creating a string with `%s' at %p from %p, this = %p\n"), str, str, s, this); #endif @@ -236,11 +237,7 @@ int String::scanf(const ugly_string & s, ...) const { const char * String::to_charp(size_t from, ssize_t to) const throw (GeneralException) { if (to < 0) { - if (from) { - //throw GeneralException("This usage of String is deprecated."); - strncpy(t, &(str[from]), BUFSIZ); - } else - return str; + return str + from; } else { if (((size_t) to) >= siz) { to = siz - 1; @@ -599,8 +596,8 @@ String & String::iconv(const String & from, const String & _to) { free(str); - str = Base::strdup(t); - siz = ::strlen(t); + str = (char *) malloc(outbuf - t + 1); + memcpy(str, t, outbuf - t); UNLOCK; iconv_close(cd); |