New grow and skid sounds from remaxim
[supertux.git] / src / squirrel / sqstdlib / sqstdstring.cpp
index 31735e1..19b30b7 100644 (file)
@@ -7,7 +7,7 @@
 #include <ctype.h>
 #include <assert.h>
 
-#ifdef _UNICODE
+#ifdef SQUNICODE
 #define scstrchr wcschr
 #define scsnprintf wsnprintf
 #define scatoi _wtoi
@@ -65,15 +65,16 @@ static SQInteger validate_format(HSQUIRRELVM v, SQChar *fmt, const SQChar *src,
        return n;
 }
 
-static SQInteger _string_format(HSQUIRRELVM v)
+
+SQRESULT sqstd_format(HSQUIRRELVM v,SQInteger nformatstringidx,SQInteger *outlen,SQChar **output)
 {
        const SQChar *format;
        SQChar *dest;
        SQChar fmt[MAX_FORMAT_LEN];
-       sq_getstring(v,2,&format);
-       SQInteger allocated = (sq_getsize(v,2)+1)*sizeof(SQChar);
+       sq_getstring(v,nformatstringidx,&format);
+       SQInteger allocated = (sq_getsize(v,nformatstringidx)+2)*sizeof(SQChar);
        dest = sq_getscratchpad(v,allocated);
-       SQInteger n = 0,i = 0, nparam = 3, w = 0;
+       SQInteger n = 0,i = 0, nparam = nformatstringidx+1, w = 0;
        while(format[n] != '\0') {
                if(format[n] != '%') {
                        assert(i < allocated);
@@ -118,7 +119,7 @@ static SQInteger _string_format(HSQUIRRELVM v)
                                return sq_throwerror(v,_SC("invalid format"));
                        }
                        n++;
-                       allocated += addlen;
+                       allocated += addlen + sizeof(SQChar);
                        dest = sq_getscratchpad(v,allocated);
                        switch(valtype) {
                        case 's': i += scsprintf(&dest[i],fmt,ts); break;
@@ -128,7 +129,19 @@ static SQInteger _string_format(HSQUIRRELVM v)
                        nparam ++;
                }
        }
-       sq_pushstring(v,dest,i);
+       *outlen = i;
+       dest[i] = '\0';
+       *output = dest;
+       return SQ_OK;
+}
+
+static SQInteger _string_format(HSQUIRRELVM v)
+{
+       SQChar *dest = NULL;
+       SQInteger length = 0;
+       if(SQ_FAILED(sqstd_format(v,2,&length,&dest)))
+               return -1;
+       sq_pushstring(v,dest,length);
        return 1;
 }