-/* see copyright notice in squirrel.h */\r
-#include <new>\r
-#include <squirrel.h>\r
-#include <sqstdio.h>\r
-#include <string.h>\r
-#include <sqstdblob.h>\r
-#include "sqstdstream.h"\r
-#include "sqstdblobimpl.h"\r
-\r
-#define SQSTD_BLOB_TYPE_TAG (SQSTD_STREAM_TYPE_TAG | 0x00000002)\r
-\r
-//Blob\r
-\r
-\r
-#define SETUP_BLOB(v) \\r
- SQBlob *self = NULL; \\r
- { if(SQ_FAILED(sq_getinstanceup(v,1,(SQUserPointer*)&self,SQSTD_BLOB_TYPE_TAG))) \\r
- return SQ_ERROR; }\r
-\r
-\r
-static int _blob_resize(HSQUIRRELVM v)\r
-{\r
- SETUP_BLOB(v);\r
- SQInteger size;\r
- sq_getinteger(v,2,&size);\r
- if(!self->Resize(size))\r
- return sq_throwerror(v,_SC("resize failed"));\r
- return 0;\r
-}\r
-\r
-static void __swap_dword(unsigned int *n)\r
-{\r
- *n=(unsigned int)(((*n&0xFF000000)>>24) |\r
- ((*n&0x00FF0000)>>8) |\r
- ((*n&0x0000FF00)<<8) |\r
- ((*n&0x000000FF)<<24));\r
-}\r
-\r
-static void __swap_word(unsigned short *n)\r
-{\r
- *n=(unsigned short)((*n>>8)&0x00FF)| ((*n<<8)&0xFF00);\r
-}\r
-\r
-static int _blob_swap4(HSQUIRRELVM v)\r
-{\r
- SETUP_BLOB(v);\r
- int num=(self->Len()-(self->Len()%4))>>2;\r
- unsigned int *t=(unsigned int *)self->GetBuf();\r
- for(int i = 0; i < num; i++) {\r
- __swap_dword(&t[i]);\r
- }\r
- return 0;\r
-}\r
-\r
-static int _blob_swap2(HSQUIRRELVM v)\r
-{\r
- SETUP_BLOB(v);\r
- int num=(self->Len()-(self->Len()%2))>>1;\r
- unsigned short *t = (unsigned short *)self->GetBuf();\r
- for(int i = 0; i < num; i++) {\r
- __swap_word(&t[i]);\r
- }\r
- return 0;\r
-}\r
-\r
-static int _blob__set(HSQUIRRELVM v)\r
-{\r
- SETUP_BLOB(v);\r
- SQInteger idx,val;\r
- sq_getinteger(v,2,&idx);\r
- sq_getinteger(v,3,&val);\r
- if(idx < 0 || idx >= self->Len())\r
- return sq_throwerror(v,_SC("index out of range"));\r
- ((unsigned char *)self->GetBuf())[idx] = (unsigned char) val;\r
- sq_push(v,3);\r
- return 1;\r
-}\r
-\r
-static int _blob__get(HSQUIRRELVM v)\r
-{\r
- SETUP_BLOB(v);\r
- SQInteger idx;\r
- sq_getinteger(v,2,&idx);\r
- if(idx < 0 || idx >= self->Len())\r
- return sq_throwerror(v,_SC("index out of range"));\r
- sq_pushinteger(v,((unsigned char *)self->GetBuf())[idx]);\r
- return 1;\r
-}\r
-\r
-static int _blob__nexti(HSQUIRRELVM v)\r
-{\r
- SETUP_BLOB(v);\r
- if(sq_gettype(v,2) == OT_NULL) {\r
- sq_pushinteger(v, 0);\r
- return 1;\r
- }\r
- SQInteger idx;\r
- if(SQ_SUCCEEDED(sq_getinteger(v, 2, &idx))) {\r
- if(idx+1 < self->Len()) {\r
- sq_pushinteger(v, idx+1);\r
- return 1;\r
- }\r
- sq_pushnull(v);\r
- return 1;\r
- }\r
- return sq_throwerror(v,_SC("internal error (_nexti) wrong argument type"));\r
-}\r
-\r
-static int _blob__typeof(HSQUIRRELVM v)\r
-{\r
- sq_pushstring(v,_SC("blob"),-1);\r
- return 1;\r
-}\r
-\r
-static int _blob_releasehook(SQUserPointer p, int size)\r
-{\r
- SQBlob *self = (SQBlob*)p;\r
- delete self;\r
- return 1;\r
-}\r
-\r
-static int _blob_constructor(HSQUIRRELVM v)\r
-{\r
- SQInteger nparam = sq_gettop(v);\r
- SQInteger size = 0;\r
- if(nparam == 2) {\r
- sq_getinteger(v, 2, &size);\r
- }\r
- if(size < 0) return sq_throwerror(v, _SC("cannot create blob with negative size"));\r
- SQBlob *b = new SQBlob(size);\r
- if(SQ_FAILED(sq_setinstanceup(v,1,b))) {\r
- delete b;\r
- return sq_throwerror(v, _SC("cannot create blob with negative size"));\r
- }\r
- sq_setreleasehook(v,1,_blob_releasehook);\r
- return 0;\r
-}\r
-\r
-#define _DECL_BLOB_FUNC(name,nparams,typecheck) {_SC(#name),_blob_##name,nparams,typecheck}\r
-static SQRegFunction _blob_methods[] = {\r
- _DECL_BLOB_FUNC(constructor,-1,_SC("xn")),\r
- _DECL_BLOB_FUNC(resize,2,_SC("xn")),\r
- _DECL_BLOB_FUNC(swap2,1,_SC("x")),\r
- _DECL_BLOB_FUNC(swap4,1,_SC("x")),\r
- _DECL_BLOB_FUNC(_set,3,_SC("xnn")),\r
- _DECL_BLOB_FUNC(_get,2,_SC("xn")),\r
- _DECL_BLOB_FUNC(_typeof,1,_SC("x")),\r
- _DECL_BLOB_FUNC(_nexti,2,_SC("x")),\r
- {0,0,0,0}\r
-};\r
-\r
-\r
-\r
-//GLOBAL FUNCTIONS\r
-\r
-static int _g_blob_casti2f(HSQUIRRELVM v)\r
-{\r
- SQInteger i;\r
- sq_getinteger(v,2,&i);\r
- sq_pushfloat(v,*((SQFloat *)&i));\r
- return 1;\r
-}\r
-\r
-static int _g_blob_castf2i(HSQUIRRELVM v)\r
-{\r
- SQFloat f;\r
- sq_getfloat(v,2,&f);\r
- sq_pushinteger(v,*((SQInteger *)&f));\r
- return 1;\r
-}\r
-\r
-static int _g_blob_swap2(HSQUIRRELVM v)\r
-{\r
- SQInteger i;\r
- sq_getinteger(v,2,&i);\r
- short s=(short)i;\r
- sq_pushinteger(v,(s<<8)|((s>>8)&0x00FF));\r
- return 1;\r
-}\r
-\r
-static int _g_blob_swap4(HSQUIRRELVM v)\r
-{\r
- SQInteger i;\r
- sq_getinteger(v,2,&i);\r
- __swap_dword((unsigned int *)&i);\r
- sq_pushinteger(v,i);\r
- return 1;\r
-}\r
-\r
-static int _g_blob_swapfloat(HSQUIRRELVM v)\r
-{\r
- SQFloat f;\r
- sq_getfloat(v,2,&f);\r
- __swap_dword((unsigned int *)&f);\r
- sq_pushfloat(v,f);\r
- return 1;\r
-}\r
-\r
-#define _DECL_GLOBALBLOB_FUNC(name,nparams,typecheck) {_SC(#name),_g_blob_##name,nparams,typecheck}\r
-static SQRegFunction bloblib_funcs[]={\r
- _DECL_GLOBALBLOB_FUNC(casti2f,2,_SC(".n")),\r
- _DECL_GLOBALBLOB_FUNC(castf2i,2,_SC(".n")),\r
- _DECL_GLOBALBLOB_FUNC(swap2,2,_SC(".n")),\r
- _DECL_GLOBALBLOB_FUNC(swap4,2,_SC(".n")),\r
- _DECL_GLOBALBLOB_FUNC(swapfloat,2,_SC(".n")),\r
- {0,0}\r
-};\r
-\r
-SQRESULT sqstd_getblob(HSQUIRRELVM v,int idx,SQUserPointer *ptr)\r
-{\r
- SQBlob *blob;\r
- if(SQ_FAILED(sq_getinstanceup(v,idx,(SQUserPointer *)&blob,SQSTD_BLOB_TYPE_TAG)))\r
- return -1;\r
- *ptr = blob->GetBuf();\r
- return SQ_OK;\r
-}\r
-\r
-int sqstd_getblobsize(HSQUIRRELVM v,int idx)\r
-{\r
- SQBlob *blob;\r
- if(SQ_FAILED(sq_getinstanceup(v,idx,(SQUserPointer *)&blob,SQSTD_BLOB_TYPE_TAG)))\r
- return -1;\r
- return blob->Len();\r
-}\r
-\r
-SQUserPointer sqstd_createblob(HSQUIRRELVM v, int size)\r
-{\r
- int top = sq_gettop(v);\r
-// SQUserPointer p = sq_newuserdata(v, sizeof(SQBlob));\r
-// sq_setreleasehook(v,-1,_blob_releasehook);\r
-// sq_settypetag(v,-1,SQSTD_BLOB_TYPE_TAG);\r
-// new (p) SQBlob(size);\r
- sq_pushregistrytable(v);\r
- sq_pushstring(v,_SC("std_blob"),-1);\r
- if(SQ_SUCCEEDED(sq_get(v,-2))) {\r
- sq_remove(v,-2); //removes the registry\r
- sq_push(v,1); // push the this\r
- sq_pushinteger(v,size); //size\r
- SQBlob *blob = NULL;\r
- if(SQ_SUCCEEDED(sq_call(v,2,SQTrue))\r
- && SQ_SUCCEEDED(sq_getinstanceup(v,-1,(SQUserPointer *)&blob,SQSTD_BLOB_TYPE_TAG))) {\r
- sq_remove(v,-2);\r
- sq_remove(v,-2);\r
- return blob->GetBuf();\r
- }\r
- }\r
- sq_settop(v,top);\r
- return NULL;\r
-}\r
-\r
-SQRESULT sqstd_register_bloblib(HSQUIRRELVM v)\r
-{\r
- return declare_stream(v,_SC("blob"),SQSTD_BLOB_TYPE_TAG,_SC("std_blob"),_blob_methods,bloblib_funcs);\r
-}\r
-\r
+/* see copyright notice in squirrel.h */
+#include <new>
+#include <squirrel.h>
+#include <sqstdio.h>
+#include <string.h>
+#include <sqstdblob.h>
+#include "sqstdstream.h"
+#include "sqstdblobimpl.h"
+
+#define SQSTD_BLOB_TYPE_TAG (SQSTD_STREAM_TYPE_TAG | 0x00000002)
+
+//Blob
+
+
+#define SETUP_BLOB(v) \
+ SQBlob *self = NULL; \
+ { if(SQ_FAILED(sq_getinstanceup(v,1,(SQUserPointer*)&self,(SQUserPointer)SQSTD_BLOB_TYPE_TAG))) \
+ return SQ_ERROR; }
+
+
+static SQInteger _blob_resize(HSQUIRRELVM v)
+{
+ SETUP_BLOB(v);
+ SQInteger size;
+ sq_getinteger(v,2,&size);
+ if(!self->Resize(size))
+ return sq_throwerror(v,_SC("resize failed"));
+ return 0;
+}
+
+static void __swap_dword(unsigned int *n)
+{
+ *n=(unsigned int)(((*n&0xFF000000)>>24) |
+ ((*n&0x00FF0000)>>8) |
+ ((*n&0x0000FF00)<<8) |
+ ((*n&0x000000FF)<<24));
+}
+
+static void __swap_word(unsigned short *n)
+{
+ *n=(unsigned short)((*n>>8)&0x00FF)| ((*n<<8)&0xFF00);
+}
+
+static SQInteger _blob_swap4(HSQUIRRELVM v)
+{
+ SETUP_BLOB(v);
+ SQInteger num=(self->Len()-(self->Len()%4))>>2;
+ unsigned int *t=(unsigned int *)self->GetBuf();
+ for(SQInteger i = 0; i < num; i++) {
+ __swap_dword(&t[i]);
+ }
+ return 0;
+}
+
+static SQInteger _blob_swap2(HSQUIRRELVM v)
+{
+ SETUP_BLOB(v);
+ SQInteger num=(self->Len()-(self->Len()%2))>>1;
+ unsigned short *t = (unsigned short *)self->GetBuf();
+ for(SQInteger i = 0; i < num; i++) {
+ __swap_word(&t[i]);
+ }
+ return 0;
+}
+
+static SQInteger _blob__set(HSQUIRRELVM v)
+{
+ SETUP_BLOB(v);
+ SQInteger idx,val;
+ sq_getinteger(v,2,&idx);
+ sq_getinteger(v,3,&val);
+ if(idx < 0 || idx >= self->Len())
+ return sq_throwerror(v,_SC("index out of range"));
+ ((unsigned char *)self->GetBuf())[idx] = (unsigned char) val;
+ sq_push(v,3);
+ return 1;
+}
+
+static SQInteger _blob__get(HSQUIRRELVM v)
+{
+ SETUP_BLOB(v);
+ SQInteger idx;
+ sq_getinteger(v,2,&idx);
+ if(idx < 0 || idx >= self->Len())
+ return sq_throwerror(v,_SC("index out of range"));
+ sq_pushinteger(v,((unsigned char *)self->GetBuf())[idx]);
+ return 1;
+}
+
+static SQInteger _blob__nexti(HSQUIRRELVM v)
+{
+ SETUP_BLOB(v);
+ if(sq_gettype(v,2) == OT_NULL) {
+ sq_pushinteger(v, 0);
+ return 1;
+ }
+ SQInteger idx;
+ if(SQ_SUCCEEDED(sq_getinteger(v, 2, &idx))) {
+ if(idx+1 < self->Len()) {
+ sq_pushinteger(v, idx+1);
+ return 1;
+ }
+ sq_pushnull(v);
+ return 1;
+ }
+ return sq_throwerror(v,_SC("internal error (_nexti) wrong argument type"));
+}
+
+static SQInteger _blob__typeof(HSQUIRRELVM v)
+{
+ sq_pushstring(v,_SC("blob"),-1);
+ return 1;
+}
+
+static SQInteger _blob_releasehook(SQUserPointer p, SQInteger size)
+{
+ SQBlob *self = (SQBlob*)p;
+ delete self;
+ return 1;
+}
+
+static SQInteger _blob_constructor(HSQUIRRELVM v)
+{
+ SQInteger nparam = sq_gettop(v);
+ SQInteger size = 0;
+ if(nparam == 2) {
+ sq_getinteger(v, 2, &size);
+ }
+ if(size < 0) return sq_throwerror(v, _SC("cannot create blob with negative size"));
+ SQBlob *b = new SQBlob(size);
+ if(SQ_FAILED(sq_setinstanceup(v,1,b))) {
+ delete b;
+ return sq_throwerror(v, _SC("cannot create blob with negative size"));
+ }
+ sq_setreleasehook(v,1,_blob_releasehook);
+ return 0;
+}
+
+#define _DECL_BLOB_FUNC(name,nparams,typecheck) {_SC(#name),_blob_##name,nparams,typecheck}
+static SQRegFunction _blob_methods[] = {
+ _DECL_BLOB_FUNC(constructor,-1,_SC("xn")),
+ _DECL_BLOB_FUNC(resize,2,_SC("xn")),
+ _DECL_BLOB_FUNC(swap2,1,_SC("x")),
+ _DECL_BLOB_FUNC(swap4,1,_SC("x")),
+ _DECL_BLOB_FUNC(_set,3,_SC("xnn")),
+ _DECL_BLOB_FUNC(_get,2,_SC("xn")),
+ _DECL_BLOB_FUNC(_typeof,1,_SC("x")),
+ _DECL_BLOB_FUNC(_nexti,2,_SC("x")),
+ {0,0,0,0}
+};
+
+
+
+//GLOBAL FUNCTIONS
+
+static SQInteger _g_blob_casti2f(HSQUIRRELVM v)
+{
+ SQInteger i;
+ sq_getinteger(v,2,&i);
+ sq_pushfloat(v,*((SQFloat *)&i));
+ return 1;
+}
+
+static SQInteger _g_blob_castf2i(HSQUIRRELVM v)
+{
+ SQFloat f;
+ sq_getfloat(v,2,&f);
+ sq_pushinteger(v,*((SQInteger *)&f));
+ return 1;
+}
+
+static SQInteger _g_blob_swap2(HSQUIRRELVM v)
+{
+ SQInteger i;
+ sq_getinteger(v,2,&i);
+ short s=(short)i;
+ sq_pushinteger(v,(s<<8)|((s>>8)&0x00FF));
+ return 1;
+}
+
+static SQInteger _g_blob_swap4(HSQUIRRELVM v)
+{
+ SQInteger i;
+ sq_getinteger(v,2,&i);
+ unsigned int t4 = (unsigned int)i;
+ __swap_dword(&t4);
+ sq_pushinteger(v,(SQInteger)t4);
+ return 1;
+}
+
+static SQInteger _g_blob_swapfloat(HSQUIRRELVM v)
+{
+ SQFloat f;
+ sq_getfloat(v,2,&f);
+ __swap_dword((unsigned int *)&f);
+ sq_pushfloat(v,f);
+ return 1;
+}
+
+#define _DECL_GLOBALBLOB_FUNC(name,nparams,typecheck) {_SC(#name),_g_blob_##name,nparams,typecheck}
+static SQRegFunction bloblib_funcs[]={
+ _DECL_GLOBALBLOB_FUNC(casti2f,2,_SC(".n")),
+ _DECL_GLOBALBLOB_FUNC(castf2i,2,_SC(".n")),
+ _DECL_GLOBALBLOB_FUNC(swap2,2,_SC(".n")),
+ _DECL_GLOBALBLOB_FUNC(swap4,2,_SC(".n")),
+ _DECL_GLOBALBLOB_FUNC(swapfloat,2,_SC(".n")),
+ {0,0}
+};
+
+SQRESULT sqstd_getblob(HSQUIRRELVM v,SQInteger idx,SQUserPointer *ptr)
+{
+ SQBlob *blob;
+ if(SQ_FAILED(sq_getinstanceup(v,idx,(SQUserPointer *)&blob,(SQUserPointer)SQSTD_BLOB_TYPE_TAG)))
+ return -1;
+ *ptr = blob->GetBuf();
+ return SQ_OK;
+}
+
+SQInteger sqstd_getblobsize(HSQUIRRELVM v,SQInteger idx)
+{
+ SQBlob *blob;
+ if(SQ_FAILED(sq_getinstanceup(v,idx,(SQUserPointer *)&blob,(SQUserPointer)SQSTD_BLOB_TYPE_TAG)))
+ return -1;
+ return blob->Len();
+}
+
+SQUserPointer sqstd_createblob(HSQUIRRELVM v, SQInteger size)
+{
+ SQInteger top = sq_gettop(v);
+ sq_pushregistrytable(v);
+ sq_pushstring(v,_SC("std_blob"),-1);
+ if(SQ_SUCCEEDED(sq_get(v,-2))) {
+ sq_remove(v,-2); //removes the registry
+ sq_push(v,1); // push the this
+ sq_pushinteger(v,size); //size
+ SQBlob *blob = NULL;
+ if(SQ_SUCCEEDED(sq_call(v,2,SQTrue,SQFalse))
+ && SQ_SUCCEEDED(sq_getinstanceup(v,-1,(SQUserPointer *)&blob,(SQUserPointer)SQSTD_BLOB_TYPE_TAG))) {
+ sq_remove(v,-2);
+ return blob->GetBuf();
+ }
+ }
+ sq_settop(v,top);
+ return NULL;
+}
+
+SQRESULT sqstd_register_bloblib(HSQUIRRELVM v)
+{
+ return declare_stream(v,_SC("blob"),(SQUserPointer)SQSTD_BLOB_TYPE_TAG,_SC("std_blob"),_blob_methods,bloblib_funcs);
+}
+