summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorpixel <pixel>2008-01-25 10:09:19 +0000
committerpixel <pixel>2008-01-25 10:09:19 +0000
commitdf9c76bcd6d112367cdc72402e6b39d1d858e17c (patch)
tree2055b9274e1f4a1b50fac367a80517775f49b904 /lib
parent8fd3464127b9ff15ae804253675088ba85e94990 (diff)
Trying better string handling method - allowing embedded zeroes.
Diffstat (limited to 'lib')
-rw-r--r--lib/BLua.cc11
-rw-r--r--lib/String.cc31
2 files changed, 21 insertions, 21 deletions
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);