The Squirrel interpreter. See http://www.squirrel-lang.org/
sqstdlib/sqstdstream.cpp@3:7268a3ceaffc, 2014-12-16 (annotated)
- Committer:
- jhnwkmn
- Date:
- Tue Dec 16 11:39:42 2014 +0000
- Revision:
- 3:7268a3ceaffc
- Parent:
- 0:97a4f8cc534c
Accepts \r as line terminator as well.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jhnwkmn | 0:97a4f8cc534c | 1 | /* see copyright notice in squirrel.h */ |
jhnwkmn | 0:97a4f8cc534c | 2 | #include <new> |
jhnwkmn | 0:97a4f8cc534c | 3 | #include <stdio.h> |
jhnwkmn | 0:97a4f8cc534c | 4 | #include <stdlib.h> |
jhnwkmn | 0:97a4f8cc534c | 5 | #include <string.h> |
jhnwkmn | 0:97a4f8cc534c | 6 | #include <squirrel.h> |
jhnwkmn | 0:97a4f8cc534c | 7 | #include <sqstdio.h> |
jhnwkmn | 0:97a4f8cc534c | 8 | #include <sqstdblob.h> |
jhnwkmn | 0:97a4f8cc534c | 9 | #include "sqstdstream.h" |
jhnwkmn | 0:97a4f8cc534c | 10 | #include "sqstdblobimpl.h" |
jhnwkmn | 0:97a4f8cc534c | 11 | |
jhnwkmn | 0:97a4f8cc534c | 12 | #define SETUP_STREAM(v) \ |
jhnwkmn | 0:97a4f8cc534c | 13 | SQStream *self = NULL; \ |
jhnwkmn | 0:97a4f8cc534c | 14 | if(SQ_FAILED(sq_getinstanceup(v,1,(SQUserPointer*)&self,(SQUserPointer)SQSTD_STREAM_TYPE_TAG))) \ |
jhnwkmn | 0:97a4f8cc534c | 15 | return sq_throwerror(v,_SC("invalid type tag")); \ |
jhnwkmn | 0:97a4f8cc534c | 16 | if(!self || !self->IsValid()) \ |
jhnwkmn | 0:97a4f8cc534c | 17 | return sq_throwerror(v,_SC("the stream is invalid")); |
jhnwkmn | 0:97a4f8cc534c | 18 | |
jhnwkmn | 0:97a4f8cc534c | 19 | SQInteger _stream_readblob(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 20 | { |
jhnwkmn | 0:97a4f8cc534c | 21 | SETUP_STREAM(v); |
jhnwkmn | 0:97a4f8cc534c | 22 | SQUserPointer data,blobp; |
jhnwkmn | 0:97a4f8cc534c | 23 | SQInteger size,res; |
jhnwkmn | 0:97a4f8cc534c | 24 | sq_getinteger(v,2,&size); |
jhnwkmn | 0:97a4f8cc534c | 25 | if(size > self->Len()) { |
jhnwkmn | 0:97a4f8cc534c | 26 | size = self->Len(); |
jhnwkmn | 0:97a4f8cc534c | 27 | } |
jhnwkmn | 0:97a4f8cc534c | 28 | data = sq_getscratchpad(v,size); |
jhnwkmn | 0:97a4f8cc534c | 29 | res = self->Read(data,size); |
jhnwkmn | 0:97a4f8cc534c | 30 | if(res <= 0) |
jhnwkmn | 0:97a4f8cc534c | 31 | return sq_throwerror(v,_SC("no data left to read")); |
jhnwkmn | 0:97a4f8cc534c | 32 | blobp = sqstd_createblob(v,res); |
jhnwkmn | 0:97a4f8cc534c | 33 | memcpy(blobp,data,res); |
jhnwkmn | 0:97a4f8cc534c | 34 | return 1; |
jhnwkmn | 0:97a4f8cc534c | 35 | } |
jhnwkmn | 0:97a4f8cc534c | 36 | |
jhnwkmn | 0:97a4f8cc534c | 37 | #define SAFE_READN(ptr,len) { \ |
jhnwkmn | 0:97a4f8cc534c | 38 | if(self->Read(ptr,len) != len) return sq_throwerror(v,_SC("io error")); \ |
jhnwkmn | 0:97a4f8cc534c | 39 | } |
jhnwkmn | 0:97a4f8cc534c | 40 | SQInteger _stream_readn(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 41 | { |
jhnwkmn | 0:97a4f8cc534c | 42 | SETUP_STREAM(v); |
jhnwkmn | 0:97a4f8cc534c | 43 | SQInteger format; |
jhnwkmn | 0:97a4f8cc534c | 44 | sq_getinteger(v, 2, &format); |
jhnwkmn | 0:97a4f8cc534c | 45 | switch(format) { |
jhnwkmn | 0:97a4f8cc534c | 46 | case 'l': { |
jhnwkmn | 0:97a4f8cc534c | 47 | SQInteger i; |
jhnwkmn | 0:97a4f8cc534c | 48 | SAFE_READN(&i, sizeof(i)); |
jhnwkmn | 0:97a4f8cc534c | 49 | sq_pushinteger(v, i); |
jhnwkmn | 0:97a4f8cc534c | 50 | } |
jhnwkmn | 0:97a4f8cc534c | 51 | break; |
jhnwkmn | 0:97a4f8cc534c | 52 | case 'i': { |
jhnwkmn | 0:97a4f8cc534c | 53 | SQInt32 i; |
jhnwkmn | 0:97a4f8cc534c | 54 | SAFE_READN(&i, sizeof(i)); |
jhnwkmn | 0:97a4f8cc534c | 55 | sq_pushinteger(v, i); |
jhnwkmn | 0:97a4f8cc534c | 56 | } |
jhnwkmn | 0:97a4f8cc534c | 57 | break; |
jhnwkmn | 0:97a4f8cc534c | 58 | case 's': { |
jhnwkmn | 0:97a4f8cc534c | 59 | short s; |
jhnwkmn | 0:97a4f8cc534c | 60 | SAFE_READN(&s, sizeof(short)); |
jhnwkmn | 0:97a4f8cc534c | 61 | sq_pushinteger(v, s); |
jhnwkmn | 0:97a4f8cc534c | 62 | } |
jhnwkmn | 0:97a4f8cc534c | 63 | break; |
jhnwkmn | 0:97a4f8cc534c | 64 | case 'w': { |
jhnwkmn | 0:97a4f8cc534c | 65 | unsigned short w; |
jhnwkmn | 0:97a4f8cc534c | 66 | SAFE_READN(&w, sizeof(unsigned short)); |
jhnwkmn | 0:97a4f8cc534c | 67 | sq_pushinteger(v, w); |
jhnwkmn | 0:97a4f8cc534c | 68 | } |
jhnwkmn | 0:97a4f8cc534c | 69 | break; |
jhnwkmn | 0:97a4f8cc534c | 70 | case 'c': { |
jhnwkmn | 0:97a4f8cc534c | 71 | char c; |
jhnwkmn | 0:97a4f8cc534c | 72 | SAFE_READN(&c, sizeof(char)); |
jhnwkmn | 0:97a4f8cc534c | 73 | sq_pushinteger(v, c); |
jhnwkmn | 0:97a4f8cc534c | 74 | } |
jhnwkmn | 0:97a4f8cc534c | 75 | break; |
jhnwkmn | 0:97a4f8cc534c | 76 | case 'b': { |
jhnwkmn | 0:97a4f8cc534c | 77 | unsigned char c; |
jhnwkmn | 0:97a4f8cc534c | 78 | SAFE_READN(&c, sizeof(unsigned char)); |
jhnwkmn | 0:97a4f8cc534c | 79 | sq_pushinteger(v, c); |
jhnwkmn | 0:97a4f8cc534c | 80 | } |
jhnwkmn | 0:97a4f8cc534c | 81 | break; |
jhnwkmn | 0:97a4f8cc534c | 82 | case 'f': { |
jhnwkmn | 0:97a4f8cc534c | 83 | float f; |
jhnwkmn | 0:97a4f8cc534c | 84 | SAFE_READN(&f, sizeof(float)); |
jhnwkmn | 0:97a4f8cc534c | 85 | sq_pushfloat(v, f); |
jhnwkmn | 0:97a4f8cc534c | 86 | } |
jhnwkmn | 0:97a4f8cc534c | 87 | break; |
jhnwkmn | 0:97a4f8cc534c | 88 | case 'd': { |
jhnwkmn | 0:97a4f8cc534c | 89 | double d; |
jhnwkmn | 0:97a4f8cc534c | 90 | SAFE_READN(&d, sizeof(double)); |
jhnwkmn | 0:97a4f8cc534c | 91 | sq_pushfloat(v, (SQFloat)d); |
jhnwkmn | 0:97a4f8cc534c | 92 | } |
jhnwkmn | 0:97a4f8cc534c | 93 | break; |
jhnwkmn | 0:97a4f8cc534c | 94 | default: |
jhnwkmn | 0:97a4f8cc534c | 95 | return sq_throwerror(v, _SC("invalid format")); |
jhnwkmn | 0:97a4f8cc534c | 96 | } |
jhnwkmn | 0:97a4f8cc534c | 97 | return 1; |
jhnwkmn | 0:97a4f8cc534c | 98 | } |
jhnwkmn | 0:97a4f8cc534c | 99 | |
jhnwkmn | 0:97a4f8cc534c | 100 | SQInteger _stream_writeblob(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 101 | { |
jhnwkmn | 0:97a4f8cc534c | 102 | SQUserPointer data; |
jhnwkmn | 0:97a4f8cc534c | 103 | SQInteger size; |
jhnwkmn | 0:97a4f8cc534c | 104 | SETUP_STREAM(v); |
jhnwkmn | 0:97a4f8cc534c | 105 | if(SQ_FAILED(sqstd_getblob(v,2,&data))) |
jhnwkmn | 0:97a4f8cc534c | 106 | return sq_throwerror(v,_SC("invalid parameter")); |
jhnwkmn | 0:97a4f8cc534c | 107 | size = sqstd_getblobsize(v,2); |
jhnwkmn | 0:97a4f8cc534c | 108 | if(self->Write(data,size) != size) |
jhnwkmn | 0:97a4f8cc534c | 109 | return sq_throwerror(v,_SC("io error")); |
jhnwkmn | 0:97a4f8cc534c | 110 | sq_pushinteger(v,size); |
jhnwkmn | 0:97a4f8cc534c | 111 | return 1; |
jhnwkmn | 0:97a4f8cc534c | 112 | } |
jhnwkmn | 0:97a4f8cc534c | 113 | |
jhnwkmn | 0:97a4f8cc534c | 114 | SQInteger _stream_writen(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 115 | { |
jhnwkmn | 0:97a4f8cc534c | 116 | SETUP_STREAM(v); |
jhnwkmn | 0:97a4f8cc534c | 117 | SQInteger format, ti; |
jhnwkmn | 0:97a4f8cc534c | 118 | SQFloat tf; |
jhnwkmn | 0:97a4f8cc534c | 119 | sq_getinteger(v, 3, &format); |
jhnwkmn | 0:97a4f8cc534c | 120 | switch(format) { |
jhnwkmn | 0:97a4f8cc534c | 121 | case 'l': { |
jhnwkmn | 0:97a4f8cc534c | 122 | SQInteger i; |
jhnwkmn | 0:97a4f8cc534c | 123 | sq_getinteger(v, 2, &ti); |
jhnwkmn | 0:97a4f8cc534c | 124 | i = ti; |
jhnwkmn | 0:97a4f8cc534c | 125 | self->Write(&i, sizeof(SQInteger)); |
jhnwkmn | 0:97a4f8cc534c | 126 | } |
jhnwkmn | 0:97a4f8cc534c | 127 | break; |
jhnwkmn | 0:97a4f8cc534c | 128 | case 'i': { |
jhnwkmn | 0:97a4f8cc534c | 129 | SQInt32 i; |
jhnwkmn | 0:97a4f8cc534c | 130 | sq_getinteger(v, 2, &ti); |
jhnwkmn | 0:97a4f8cc534c | 131 | i = (SQInt32)ti; |
jhnwkmn | 0:97a4f8cc534c | 132 | self->Write(&i, sizeof(SQInt32)); |
jhnwkmn | 0:97a4f8cc534c | 133 | } |
jhnwkmn | 0:97a4f8cc534c | 134 | break; |
jhnwkmn | 0:97a4f8cc534c | 135 | case 's': { |
jhnwkmn | 0:97a4f8cc534c | 136 | short s; |
jhnwkmn | 0:97a4f8cc534c | 137 | sq_getinteger(v, 2, &ti); |
jhnwkmn | 0:97a4f8cc534c | 138 | s = (short)ti; |
jhnwkmn | 0:97a4f8cc534c | 139 | self->Write(&s, sizeof(short)); |
jhnwkmn | 0:97a4f8cc534c | 140 | } |
jhnwkmn | 0:97a4f8cc534c | 141 | break; |
jhnwkmn | 0:97a4f8cc534c | 142 | case 'w': { |
jhnwkmn | 0:97a4f8cc534c | 143 | unsigned short w; |
jhnwkmn | 0:97a4f8cc534c | 144 | sq_getinteger(v, 2, &ti); |
jhnwkmn | 0:97a4f8cc534c | 145 | w = (unsigned short)ti; |
jhnwkmn | 0:97a4f8cc534c | 146 | self->Write(&w, sizeof(unsigned short)); |
jhnwkmn | 0:97a4f8cc534c | 147 | } |
jhnwkmn | 0:97a4f8cc534c | 148 | break; |
jhnwkmn | 0:97a4f8cc534c | 149 | case 'c': { |
jhnwkmn | 0:97a4f8cc534c | 150 | char c; |
jhnwkmn | 0:97a4f8cc534c | 151 | sq_getinteger(v, 2, &ti); |
jhnwkmn | 0:97a4f8cc534c | 152 | c = (char)ti; |
jhnwkmn | 0:97a4f8cc534c | 153 | self->Write(&c, sizeof(char)); |
jhnwkmn | 0:97a4f8cc534c | 154 | } |
jhnwkmn | 0:97a4f8cc534c | 155 | break; |
jhnwkmn | 0:97a4f8cc534c | 156 | case 'b': { |
jhnwkmn | 0:97a4f8cc534c | 157 | unsigned char b; |
jhnwkmn | 0:97a4f8cc534c | 158 | sq_getinteger(v, 2, &ti); |
jhnwkmn | 0:97a4f8cc534c | 159 | b = (unsigned char)ti; |
jhnwkmn | 0:97a4f8cc534c | 160 | self->Write(&b, sizeof(unsigned char)); |
jhnwkmn | 0:97a4f8cc534c | 161 | } |
jhnwkmn | 0:97a4f8cc534c | 162 | break; |
jhnwkmn | 0:97a4f8cc534c | 163 | case 'f': { |
jhnwkmn | 0:97a4f8cc534c | 164 | float f; |
jhnwkmn | 0:97a4f8cc534c | 165 | sq_getfloat(v, 2, &tf); |
jhnwkmn | 0:97a4f8cc534c | 166 | f = (float)tf; |
jhnwkmn | 0:97a4f8cc534c | 167 | self->Write(&f, sizeof(float)); |
jhnwkmn | 0:97a4f8cc534c | 168 | } |
jhnwkmn | 0:97a4f8cc534c | 169 | break; |
jhnwkmn | 0:97a4f8cc534c | 170 | case 'd': { |
jhnwkmn | 0:97a4f8cc534c | 171 | double d; |
jhnwkmn | 0:97a4f8cc534c | 172 | sq_getfloat(v, 2, &tf); |
jhnwkmn | 0:97a4f8cc534c | 173 | d = tf; |
jhnwkmn | 0:97a4f8cc534c | 174 | self->Write(&d, sizeof(double)); |
jhnwkmn | 0:97a4f8cc534c | 175 | } |
jhnwkmn | 0:97a4f8cc534c | 176 | break; |
jhnwkmn | 0:97a4f8cc534c | 177 | default: |
jhnwkmn | 0:97a4f8cc534c | 178 | return sq_throwerror(v, _SC("invalid format")); |
jhnwkmn | 0:97a4f8cc534c | 179 | } |
jhnwkmn | 0:97a4f8cc534c | 180 | return 0; |
jhnwkmn | 0:97a4f8cc534c | 181 | } |
jhnwkmn | 0:97a4f8cc534c | 182 | |
jhnwkmn | 0:97a4f8cc534c | 183 | SQInteger _stream_seek(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 184 | { |
jhnwkmn | 0:97a4f8cc534c | 185 | SETUP_STREAM(v); |
jhnwkmn | 0:97a4f8cc534c | 186 | SQInteger offset, origin = SQ_SEEK_SET; |
jhnwkmn | 0:97a4f8cc534c | 187 | sq_getinteger(v, 2, &offset); |
jhnwkmn | 0:97a4f8cc534c | 188 | if(sq_gettop(v) > 2) { |
jhnwkmn | 0:97a4f8cc534c | 189 | SQInteger t; |
jhnwkmn | 0:97a4f8cc534c | 190 | sq_getinteger(v, 3, &t); |
jhnwkmn | 0:97a4f8cc534c | 191 | switch(t) { |
jhnwkmn | 0:97a4f8cc534c | 192 | case 'b': origin = SQ_SEEK_SET; break; |
jhnwkmn | 0:97a4f8cc534c | 193 | case 'c': origin = SQ_SEEK_CUR; break; |
jhnwkmn | 0:97a4f8cc534c | 194 | case 'e': origin = SQ_SEEK_END; break; |
jhnwkmn | 0:97a4f8cc534c | 195 | default: return sq_throwerror(v,_SC("invalid origin")); |
jhnwkmn | 0:97a4f8cc534c | 196 | } |
jhnwkmn | 0:97a4f8cc534c | 197 | } |
jhnwkmn | 0:97a4f8cc534c | 198 | sq_pushinteger(v, self->Seek(offset, origin)); |
jhnwkmn | 0:97a4f8cc534c | 199 | return 1; |
jhnwkmn | 0:97a4f8cc534c | 200 | } |
jhnwkmn | 0:97a4f8cc534c | 201 | |
jhnwkmn | 0:97a4f8cc534c | 202 | SQInteger _stream_tell(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 203 | { |
jhnwkmn | 0:97a4f8cc534c | 204 | SETUP_STREAM(v); |
jhnwkmn | 0:97a4f8cc534c | 205 | sq_pushinteger(v, self->Tell()); |
jhnwkmn | 0:97a4f8cc534c | 206 | return 1; |
jhnwkmn | 0:97a4f8cc534c | 207 | } |
jhnwkmn | 0:97a4f8cc534c | 208 | |
jhnwkmn | 0:97a4f8cc534c | 209 | SQInteger _stream_len(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 210 | { |
jhnwkmn | 0:97a4f8cc534c | 211 | SETUP_STREAM(v); |
jhnwkmn | 0:97a4f8cc534c | 212 | sq_pushinteger(v, self->Len()); |
jhnwkmn | 0:97a4f8cc534c | 213 | return 1; |
jhnwkmn | 0:97a4f8cc534c | 214 | } |
jhnwkmn | 0:97a4f8cc534c | 215 | |
jhnwkmn | 0:97a4f8cc534c | 216 | SQInteger _stream_flush(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 217 | { |
jhnwkmn | 0:97a4f8cc534c | 218 | SETUP_STREAM(v); |
jhnwkmn | 0:97a4f8cc534c | 219 | if(!self->Flush()) |
jhnwkmn | 0:97a4f8cc534c | 220 | sq_pushinteger(v, 1); |
jhnwkmn | 0:97a4f8cc534c | 221 | else |
jhnwkmn | 0:97a4f8cc534c | 222 | sq_pushnull(v); |
jhnwkmn | 0:97a4f8cc534c | 223 | return 1; |
jhnwkmn | 0:97a4f8cc534c | 224 | } |
jhnwkmn | 0:97a4f8cc534c | 225 | |
jhnwkmn | 0:97a4f8cc534c | 226 | SQInteger _stream_eos(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 227 | { |
jhnwkmn | 0:97a4f8cc534c | 228 | SETUP_STREAM(v); |
jhnwkmn | 0:97a4f8cc534c | 229 | if(self->EOS()) |
jhnwkmn | 0:97a4f8cc534c | 230 | sq_pushinteger(v, 1); |
jhnwkmn | 0:97a4f8cc534c | 231 | else |
jhnwkmn | 0:97a4f8cc534c | 232 | sq_pushnull(v); |
jhnwkmn | 0:97a4f8cc534c | 233 | return 1; |
jhnwkmn | 0:97a4f8cc534c | 234 | } |
jhnwkmn | 0:97a4f8cc534c | 235 | |
jhnwkmn | 0:97a4f8cc534c | 236 | SQInteger _stream__cloned(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 237 | { |
jhnwkmn | 0:97a4f8cc534c | 238 | return sq_throwerror(v,_SC("this object cannot be cloned")); |
jhnwkmn | 0:97a4f8cc534c | 239 | } |
jhnwkmn | 0:97a4f8cc534c | 240 | |
jhnwkmn | 0:97a4f8cc534c | 241 | static SQRegFunction _stream_methods[] = { |
jhnwkmn | 0:97a4f8cc534c | 242 | _DECL_STREAM_FUNC(readblob,2,_SC("xn")), |
jhnwkmn | 0:97a4f8cc534c | 243 | _DECL_STREAM_FUNC(readn,2,_SC("xn")), |
jhnwkmn | 0:97a4f8cc534c | 244 | _DECL_STREAM_FUNC(writeblob,-2,_SC("xx")), |
jhnwkmn | 0:97a4f8cc534c | 245 | _DECL_STREAM_FUNC(writen,3,_SC("xnn")), |
jhnwkmn | 0:97a4f8cc534c | 246 | _DECL_STREAM_FUNC(seek,-2,_SC("xnn")), |
jhnwkmn | 0:97a4f8cc534c | 247 | _DECL_STREAM_FUNC(tell,1,_SC("x")), |
jhnwkmn | 0:97a4f8cc534c | 248 | _DECL_STREAM_FUNC(len,1,_SC("x")), |
jhnwkmn | 0:97a4f8cc534c | 249 | _DECL_STREAM_FUNC(eos,1,_SC("x")), |
jhnwkmn | 0:97a4f8cc534c | 250 | _DECL_STREAM_FUNC(flush,1,_SC("x")), |
jhnwkmn | 0:97a4f8cc534c | 251 | _DECL_STREAM_FUNC(_cloned,0,NULL), |
jhnwkmn | 0:97a4f8cc534c | 252 | {0,0} |
jhnwkmn | 0:97a4f8cc534c | 253 | }; |
jhnwkmn | 0:97a4f8cc534c | 254 | |
jhnwkmn | 0:97a4f8cc534c | 255 | void init_streamclass(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 256 | { |
jhnwkmn | 0:97a4f8cc534c | 257 | sq_pushregistrytable(v); |
jhnwkmn | 0:97a4f8cc534c | 258 | sq_pushstring(v,_SC("std_stream"),-1); |
jhnwkmn | 0:97a4f8cc534c | 259 | if(SQ_FAILED(sq_get(v,-2))) { |
jhnwkmn | 0:97a4f8cc534c | 260 | sq_pushstring(v,_SC("std_stream"),-1); |
jhnwkmn | 0:97a4f8cc534c | 261 | sq_newclass(v,SQFalse); |
jhnwkmn | 0:97a4f8cc534c | 262 | sq_settypetag(v,-1,(SQUserPointer)SQSTD_STREAM_TYPE_TAG); |
jhnwkmn | 0:97a4f8cc534c | 263 | SQInteger i = 0; |
jhnwkmn | 0:97a4f8cc534c | 264 | while(_stream_methods[i].name != 0) { |
jhnwkmn | 0:97a4f8cc534c | 265 | SQRegFunction &f = _stream_methods[i]; |
jhnwkmn | 0:97a4f8cc534c | 266 | sq_pushstring(v,f.name,-1); |
jhnwkmn | 0:97a4f8cc534c | 267 | sq_newclosure(v,f.f,0); |
jhnwkmn | 0:97a4f8cc534c | 268 | sq_setparamscheck(v,f.nparamscheck,f.typemask); |
jhnwkmn | 0:97a4f8cc534c | 269 | sq_newslot(v,-3,SQFalse); |
jhnwkmn | 0:97a4f8cc534c | 270 | i++; |
jhnwkmn | 0:97a4f8cc534c | 271 | } |
jhnwkmn | 0:97a4f8cc534c | 272 | sq_newslot(v,-3,SQFalse); |
jhnwkmn | 0:97a4f8cc534c | 273 | sq_pushroottable(v); |
jhnwkmn | 0:97a4f8cc534c | 274 | sq_pushstring(v,_SC("stream"),-1); |
jhnwkmn | 0:97a4f8cc534c | 275 | sq_pushstring(v,_SC("std_stream"),-1); |
jhnwkmn | 0:97a4f8cc534c | 276 | sq_get(v,-4); |
jhnwkmn | 0:97a4f8cc534c | 277 | sq_newslot(v,-3,SQFalse); |
jhnwkmn | 0:97a4f8cc534c | 278 | sq_pop(v,1); |
jhnwkmn | 0:97a4f8cc534c | 279 | } |
jhnwkmn | 0:97a4f8cc534c | 280 | else { |
jhnwkmn | 0:97a4f8cc534c | 281 | sq_pop(v,1); //result |
jhnwkmn | 0:97a4f8cc534c | 282 | } |
jhnwkmn | 0:97a4f8cc534c | 283 | sq_pop(v,1); |
jhnwkmn | 0:97a4f8cc534c | 284 | } |
jhnwkmn | 0:97a4f8cc534c | 285 | |
jhnwkmn | 0:97a4f8cc534c | 286 | SQRESULT declare_stream(HSQUIRRELVM v,const SQChar* name,SQUserPointer typetag,const SQChar* reg_name,SQRegFunction *methods,SQRegFunction *globals) |
jhnwkmn | 0:97a4f8cc534c | 287 | { |
jhnwkmn | 0:97a4f8cc534c | 288 | if(sq_gettype(v,-1) != OT_TABLE) |
jhnwkmn | 0:97a4f8cc534c | 289 | return sq_throwerror(v,_SC("table expected")); |
jhnwkmn | 0:97a4f8cc534c | 290 | SQInteger top = sq_gettop(v); |
jhnwkmn | 0:97a4f8cc534c | 291 | //create delegate |
jhnwkmn | 0:97a4f8cc534c | 292 | init_streamclass(v); |
jhnwkmn | 0:97a4f8cc534c | 293 | sq_pushregistrytable(v); |
jhnwkmn | 0:97a4f8cc534c | 294 | sq_pushstring(v,reg_name,-1); |
jhnwkmn | 0:97a4f8cc534c | 295 | sq_pushstring(v,_SC("std_stream"),-1); |
jhnwkmn | 0:97a4f8cc534c | 296 | if(SQ_SUCCEEDED(sq_get(v,-3))) { |
jhnwkmn | 0:97a4f8cc534c | 297 | sq_newclass(v,SQTrue); |
jhnwkmn | 0:97a4f8cc534c | 298 | sq_settypetag(v,-1,typetag); |
jhnwkmn | 0:97a4f8cc534c | 299 | SQInteger i = 0; |
jhnwkmn | 0:97a4f8cc534c | 300 | while(methods[i].name != 0) { |
jhnwkmn | 0:97a4f8cc534c | 301 | SQRegFunction &f = methods[i]; |
jhnwkmn | 0:97a4f8cc534c | 302 | sq_pushstring(v,f.name,-1); |
jhnwkmn | 0:97a4f8cc534c | 303 | sq_newclosure(v,f.f,0); |
jhnwkmn | 0:97a4f8cc534c | 304 | sq_setparamscheck(v,f.nparamscheck,f.typemask); |
jhnwkmn | 0:97a4f8cc534c | 305 | sq_setnativeclosurename(v,-1,f.name); |
jhnwkmn | 0:97a4f8cc534c | 306 | sq_newslot(v,-3,SQFalse); |
jhnwkmn | 0:97a4f8cc534c | 307 | i++; |
jhnwkmn | 0:97a4f8cc534c | 308 | } |
jhnwkmn | 0:97a4f8cc534c | 309 | sq_newslot(v,-3,SQFalse); |
jhnwkmn | 0:97a4f8cc534c | 310 | sq_pop(v,1); |
jhnwkmn | 0:97a4f8cc534c | 311 | |
jhnwkmn | 0:97a4f8cc534c | 312 | i = 0; |
jhnwkmn | 0:97a4f8cc534c | 313 | while(globals[i].name!=0) |
jhnwkmn | 0:97a4f8cc534c | 314 | { |
jhnwkmn | 0:97a4f8cc534c | 315 | SQRegFunction &f = globals[i]; |
jhnwkmn | 0:97a4f8cc534c | 316 | sq_pushstring(v,f.name,-1); |
jhnwkmn | 0:97a4f8cc534c | 317 | sq_newclosure(v,f.f,0); |
jhnwkmn | 0:97a4f8cc534c | 318 | sq_setparamscheck(v,f.nparamscheck,f.typemask); |
jhnwkmn | 0:97a4f8cc534c | 319 | sq_setnativeclosurename(v,-1,f.name); |
jhnwkmn | 0:97a4f8cc534c | 320 | sq_newslot(v,-3,SQFalse); |
jhnwkmn | 0:97a4f8cc534c | 321 | i++; |
jhnwkmn | 0:97a4f8cc534c | 322 | } |
jhnwkmn | 0:97a4f8cc534c | 323 | //register the class in the target table |
jhnwkmn | 0:97a4f8cc534c | 324 | sq_pushstring(v,name,-1); |
jhnwkmn | 0:97a4f8cc534c | 325 | sq_pushregistrytable(v); |
jhnwkmn | 0:97a4f8cc534c | 326 | sq_pushstring(v,reg_name,-1); |
jhnwkmn | 0:97a4f8cc534c | 327 | sq_get(v,-2); |
jhnwkmn | 0:97a4f8cc534c | 328 | sq_remove(v,-2); |
jhnwkmn | 0:97a4f8cc534c | 329 | sq_newslot(v,-3,SQFalse); |
jhnwkmn | 0:97a4f8cc534c | 330 | |
jhnwkmn | 0:97a4f8cc534c | 331 | sq_settop(v,top); |
jhnwkmn | 0:97a4f8cc534c | 332 | return SQ_OK; |
jhnwkmn | 0:97a4f8cc534c | 333 | } |
jhnwkmn | 0:97a4f8cc534c | 334 | sq_settop(v,top); |
jhnwkmn | 0:97a4f8cc534c | 335 | return SQ_ERROR; |
jhnwkmn | 0:97a4f8cc534c | 336 | } |