sqstdstream.cpp 7.4 KB

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