00001
00002 #include <new>
00003 #include <squirrel.h>
00004 #include <sqstdio.h>
00005 #include <string.h>
00006 #include <sqstdblob.h>
00007 #include "sqstdstream.h"
00008 #include "sqstdblobimpl.h"
00009
00010 #define SQSTD_BLOB_TYPE_TAG (SQSTD_STREAM_TYPE_TAG | 0x00000002)
00011
00012
00013
00014
00015 #define SETUP_BLOB(v) \
00016 SQBlob *self = NULL; \
00017 { if(SQ_FAILED(sq_getinstanceup(v,1,(SQUserPointer*)&self,(SQUserPointer)SQSTD_BLOB_TYPE_TAG))) \
00018 return SQ_ERROR; }
00019
00020
00021 static SQInteger _blob_resize(HSQUIRRELVM v)
00022 {
00023 SETUP_BLOB(v);
00024 SQInteger size;
00025 sq_getinteger(v,2,&size);
00026 if(!self->Resize(size))
00027 return sq_throwerror(v,_SC("resize failed"));
00028 return 0;
00029 }
00030
00031 static void __swap_dword(unsigned int *n)
00032 {
00033 *n=(unsigned int)(((*n&0xFF000000)>>24) |
00034 ((*n&0x00FF0000)>>8) |
00035 ((*n&0x0000FF00)<<8) |
00036 ((*n&0x000000FF)<<24));
00037 }
00038
00039 static void __swap_word(unsigned short *n)
00040 {
00041 *n=(unsigned short)((*n>>8)&0x00FF)| ((*n<<8)&0xFF00);
00042 }
00043
00044 static SQInteger _blob_swap4(HSQUIRRELVM v)
00045 {
00046 SETUP_BLOB(v);
00047 SQInteger num=(self->Len()-(self->Len()%4))>>2;
00048 unsigned int *t=(unsigned int *)self->GetBuf();
00049 for(SQInteger i = 0; i < num; i++) {
00050 __swap_dword(&t[i]);
00051 }
00052 return 0;
00053 }
00054
00055 static SQInteger _blob_swap2(HSQUIRRELVM v)
00056 {
00057 SETUP_BLOB(v);
00058 SQInteger num=(self->Len()-(self->Len()%2))>>1;
00059 unsigned short *t = (unsigned short *)self->GetBuf();
00060 for(SQInteger i = 0; i < num; i++) {
00061 __swap_word(&t[i]);
00062 }
00063 return 0;
00064 }
00065
00066 static SQInteger _blob__set(HSQUIRRELVM v)
00067 {
00068 SETUP_BLOB(v);
00069 SQInteger idx,val;
00070 sq_getinteger(v,2,&idx);
00071 sq_getinteger(v,3,&val);
00072 if(idx < 0 || idx >= self->Len())
00073 return sq_throwerror(v,_SC("index out of range"));
00074 ((unsigned char *)self->GetBuf())[idx] = (unsigned char) val;
00075 sq_push(v,3);
00076 return 1;
00077 }
00078
00079 static SQInteger _blob__get(HSQUIRRELVM v)
00080 {
00081 SETUP_BLOB(v);
00082 SQInteger idx;
00083 sq_getinteger(v,2,&idx);
00084 if(idx < 0 || idx >= self->Len())
00085 return sq_throwerror(v,_SC("index out of range"));
00086 sq_pushinteger(v,((unsigned char *)self->GetBuf())[idx]);
00087 return 1;
00088 }
00089
00090 static SQInteger _blob__nexti(HSQUIRRELVM v)
00091 {
00092 SETUP_BLOB(v);
00093 if(sq_gettype(v,2) == OT_NULL) {
00094 sq_pushinteger(v, 0);
00095 return 1;
00096 }
00097 SQInteger idx;
00098 if(SQ_SUCCEEDED(sq_getinteger(v, 2, &idx))) {
00099 if(idx+1 < self->Len()) {
00100 sq_pushinteger(v, idx+1);
00101 return 1;
00102 }
00103 sq_pushnull(v);
00104 return 1;
00105 }
00106 return sq_throwerror(v,_SC("internal error (_nexti) wrong argument type"));
00107 }
00108
00109 static SQInteger _blob__typeof(HSQUIRRELVM v)
00110 {
00111 sq_pushstring(v,_SC("blob"),-1);
00112 return 1;
00113 }
00114
00115 static SQInteger _blob_releasehook(SQUserPointer p, SQInteger size)
00116 {
00117 SQBlob *self = (SQBlob*)p;
00118 delete self;
00119 return 1;
00120 }
00121
00122 static SQInteger _blob_constructor(HSQUIRRELVM v)
00123 {
00124 SQInteger nparam = sq_gettop(v);
00125 SQInteger size = 0;
00126 if(nparam == 2) {
00127 sq_getinteger(v, 2, &size);
00128 }
00129 if(size < 0) return sq_throwerror(v, _SC("cannot create blob with negative size"));
00130 SQBlob *b = new SQBlob(size);
00131 if(SQ_FAILED(sq_setinstanceup(v,1,b))) {
00132 delete b;
00133 return sq_throwerror(v, _SC("cannot create blob with negative size"));
00134 }
00135 sq_setreleasehook(v,1,_blob_releasehook);
00136 return 0;
00137 }
00138
00139 #define _DECL_BLOB_FUNC(name,nparams,typecheck) {_SC(#name),_blob_##name,nparams,typecheck}
00140 static SQRegFunction _blob_methods[] = {
00141 _DECL_BLOB_FUNC(constructor,-1,_SC("xn")),
00142 _DECL_BLOB_FUNC(resize,2,_SC("xn")),
00143 _DECL_BLOB_FUNC(swap2,1,_SC("x")),
00144 _DECL_BLOB_FUNC(swap4,1,_SC("x")),
00145 _DECL_BLOB_FUNC(_set,3,_SC("xnn")),
00146 _DECL_BLOB_FUNC(_get,2,_SC("xn")),
00147 _DECL_BLOB_FUNC(_typeof,1,_SC("x")),
00148 _DECL_BLOB_FUNC(_nexti,2,_SC("x")),
00149 {0,0,0,0}
00150 };
00151
00152
00153
00154
00155
00156 static SQInteger _g_blob_casti2f(HSQUIRRELVM v)
00157 {
00158 SQInteger i;
00159 sq_getinteger(v,2,&i);
00160 sq_pushfloat(v,*((SQFloat *)&i));
00161 return 1;
00162 }
00163
00164 static SQInteger _g_blob_castf2i(HSQUIRRELVM v)
00165 {
00166 SQFloat f;
00167 sq_getfloat(v,2,&f);
00168 sq_pushinteger(v,*((SQInteger *)&f));
00169 return 1;
00170 }
00171
00172 static SQInteger _g_blob_swap2(HSQUIRRELVM v)
00173 {
00174 SQInteger i;
00175 sq_getinteger(v,2,&i);
00176 short s=(short)i;
00177 sq_pushinteger(v,(s<<8)|((s>>8)&0x00FF));
00178 return 1;
00179 }
00180
00181 static SQInteger _g_blob_swap4(HSQUIRRELVM v)
00182 {
00183 SQInteger i;
00184 sq_getinteger(v,2,&i);
00185 unsigned int t4 = (unsigned int)i;
00186 __swap_dword(&t4);
00187 sq_pushinteger(v,(SQInteger)t4);
00188 return 1;
00189 }
00190
00191 static SQInteger _g_blob_swapfloat(HSQUIRRELVM v)
00192 {
00193 SQFloat f;
00194 sq_getfloat(v,2,&f);
00195 __swap_dword((unsigned int *)&f);
00196 sq_pushfloat(v,f);
00197 return 1;
00198 }
00199
00200 #define _DECL_GLOBALBLOB_FUNC(name,nparams,typecheck) {_SC(#name),_g_blob_##name,nparams,typecheck}
00201 static SQRegFunction bloblib_funcs[]={
00202 _DECL_GLOBALBLOB_FUNC(casti2f,2,_SC(".n")),
00203 _DECL_GLOBALBLOB_FUNC(castf2i,2,_SC(".n")),
00204 _DECL_GLOBALBLOB_FUNC(swap2,2,_SC(".n")),
00205 _DECL_GLOBALBLOB_FUNC(swap4,2,_SC(".n")),
00206 _DECL_GLOBALBLOB_FUNC(swapfloat,2,_SC(".n")),
00207 {0,0,0,0}
00208 };
00209
00210 SQRESULT sqstd_getblob(HSQUIRRELVM v,SQInteger idx,SQUserPointer *ptr)
00211 {
00212 SQBlob *blob;
00213 if(SQ_FAILED(sq_getinstanceup(v,idx,(SQUserPointer *)&blob,(SQUserPointer)SQSTD_BLOB_TYPE_TAG)))
00214 return -1;
00215 *ptr = blob->GetBuf();
00216 return SQ_OK;
00217 }
00218
00219 SQInteger sqstd_getblobsize(HSQUIRRELVM v,SQInteger idx)
00220 {
00221 SQBlob *blob;
00222 if(SQ_FAILED(sq_getinstanceup(v,idx,(SQUserPointer *)&blob,(SQUserPointer)SQSTD_BLOB_TYPE_TAG)))
00223 return -1;
00224 return blob->Len();
00225 }
00226
00227 SQUserPointer sqstd_createblob(HSQUIRRELVM v, SQInteger size)
00228 {
00229 SQInteger top = sq_gettop(v);
00230 sq_pushregistrytable(v);
00231 sq_pushstring(v,_SC("std_blob"),-1);
00232 if(SQ_SUCCEEDED(sq_get(v,-2))) {
00233 sq_remove(v,-2);
00234 sq_push(v,1);
00235 sq_pushinteger(v,size);
00236 SQBlob *blob = NULL;
00237 if(SQ_SUCCEEDED(sq_call(v,2,SQTrue,SQFalse))
00238 && SQ_SUCCEEDED(sq_getinstanceup(v,-1,(SQUserPointer *)&blob,(SQUserPointer)SQSTD_BLOB_TYPE_TAG))) {
00239 sq_remove(v,-2);
00240 return blob->GetBuf();
00241 }
00242 }
00243 sq_settop(v,top);
00244 return NULL;
00245 }
00246
00247 SQRESULT sqstd_register_bloblib(HSQUIRRELVM v)
00248 {
00249 return declare_stream(v,_SC("blob"),(SQUserPointer)SQSTD_BLOB_TYPE_TAG,_SC("std_blob"),_blob_methods,bloblib_funcs);
00250 }
00251