1 /* see copyright notice in squirrel.h */
2 #ifndef _SQSTD_BLOBIMPL_H_
3 #define _SQSTD_BLOBIMPL_H_
5 struct SQBlob : public SQStream
10 _buf = (unsigned char *)sq_malloc(size);
11 memset(_buf, 0, _size);
18 SQInteger Write(void *buffer, SQInteger size) {
19 if(!CanAdvance(size)) {
20 GrowBufOf(_ptr + size - _size);
22 memcpy(&_buf[_ptr], buffer, size);
26 SQInteger Read(void *buffer,SQInteger size) {
28 if(!CanAdvance(size)) {
29 if((_size - _ptr) > 0)
33 memcpy(buffer, &_buf[_ptr], n);
38 if(!_owns) return false;
40 unsigned char *newbuf = (unsigned char *)sq_malloc(n);
43 memcpy(newbuf,_buf,n);
45 memcpy(newbuf,_buf,_size);
46 sq_free(_buf,_allocated);
49 if(_size > _allocated)
59 if(_size + n > _allocated) {
60 if(_size + n > _size * 2)
61 ret = Resize(_size + n);
63 ret = Resize(_size * 2);
68 bool CanAdvance(int n) {
69 if(_ptr+n>_size)return false;
72 SQInteger Seek(long offset, int origin) {
75 if(offset >= _size || offset < 0) return -1;
79 if(_ptr + offset >= _size || _ptr + offset < 0) return -1;
83 if(_size + offset >= _size || _size + offset < 0) return -1;
84 _ptr = _size + offset;
91 return _buf?true:false;
96 int Flush() { return 0; }
97 long Tell() { return _ptr; }
98 SQInteger Len() { return _size; }
99 SQUserPointer GetBuf(){ return _buf; }
108 #endif //_SQSTD_BLOBIMPL_H_