The Squirrel interpreter. See http://www.squirrel-lang.org/

Dependents:   Squirrel

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?

UserRevisionLine numberNew contents of line
jhnwkmn 0:97a4f8cc534c 1 /* see copyright notice in squirrel.h */
jhnwkmn 0:97a4f8cc534c 2 #ifndef _SQSTD_BLOBIMPL_H_
jhnwkmn 0:97a4f8cc534c 3 #define _SQSTD_BLOBIMPL_H_
jhnwkmn 0:97a4f8cc534c 4
jhnwkmn 0:97a4f8cc534c 5 struct SQBlob : public SQStream
jhnwkmn 0:97a4f8cc534c 6 {
jhnwkmn 0:97a4f8cc534c 7 SQBlob(SQInteger size) {
jhnwkmn 0:97a4f8cc534c 8 _size = size;
jhnwkmn 0:97a4f8cc534c 9 _allocated = size;
jhnwkmn 0:97a4f8cc534c 10 _buf = (unsigned char *)sq_malloc(size);
jhnwkmn 0:97a4f8cc534c 11 memset(_buf, 0, _size);
jhnwkmn 0:97a4f8cc534c 12 _ptr = 0;
jhnwkmn 0:97a4f8cc534c 13 _owns = true;
jhnwkmn 0:97a4f8cc534c 14 }
jhnwkmn 0:97a4f8cc534c 15 virtual ~SQBlob() {
jhnwkmn 0:97a4f8cc534c 16 sq_free(_buf, _allocated);
jhnwkmn 0:97a4f8cc534c 17 }
jhnwkmn 0:97a4f8cc534c 18 SQInteger Write(void *buffer, SQInteger size) {
jhnwkmn 0:97a4f8cc534c 19 if(!CanAdvance(size)) {
jhnwkmn 0:97a4f8cc534c 20 GrowBufOf(_ptr + size - _size);
jhnwkmn 0:97a4f8cc534c 21 }
jhnwkmn 0:97a4f8cc534c 22 memcpy(&_buf[_ptr], buffer, size);
jhnwkmn 0:97a4f8cc534c 23 _ptr += size;
jhnwkmn 0:97a4f8cc534c 24 return size;
jhnwkmn 0:97a4f8cc534c 25 }
jhnwkmn 0:97a4f8cc534c 26 SQInteger Read(void *buffer,SQInteger size) {
jhnwkmn 0:97a4f8cc534c 27 SQInteger n = size;
jhnwkmn 0:97a4f8cc534c 28 if(!CanAdvance(size)) {
jhnwkmn 0:97a4f8cc534c 29 if((_size - _ptr) > 0)
jhnwkmn 0:97a4f8cc534c 30 n = _size - _ptr;
jhnwkmn 0:97a4f8cc534c 31 else return 0;
jhnwkmn 0:97a4f8cc534c 32 }
jhnwkmn 0:97a4f8cc534c 33 memcpy(buffer, &_buf[_ptr], n);
jhnwkmn 0:97a4f8cc534c 34 _ptr += n;
jhnwkmn 0:97a4f8cc534c 35 return n;
jhnwkmn 0:97a4f8cc534c 36 }
jhnwkmn 0:97a4f8cc534c 37 bool Resize(SQInteger n) {
jhnwkmn 0:97a4f8cc534c 38 if(!_owns) return false;
jhnwkmn 0:97a4f8cc534c 39 if(n != _allocated) {
jhnwkmn 0:97a4f8cc534c 40 unsigned char *newbuf = (unsigned char *)sq_malloc(n);
jhnwkmn 0:97a4f8cc534c 41 memset(newbuf,0,n);
jhnwkmn 0:97a4f8cc534c 42 if(_size > n)
jhnwkmn 0:97a4f8cc534c 43 memcpy(newbuf,_buf,n);
jhnwkmn 0:97a4f8cc534c 44 else
jhnwkmn 0:97a4f8cc534c 45 memcpy(newbuf,_buf,_size);
jhnwkmn 0:97a4f8cc534c 46 sq_free(_buf,_allocated);
jhnwkmn 0:97a4f8cc534c 47 _buf=newbuf;
jhnwkmn 0:97a4f8cc534c 48 _allocated = n;
jhnwkmn 0:97a4f8cc534c 49 if(_size > _allocated)
jhnwkmn 0:97a4f8cc534c 50 _size = _allocated;
jhnwkmn 0:97a4f8cc534c 51 if(_ptr > _allocated)
jhnwkmn 0:97a4f8cc534c 52 _ptr = _allocated;
jhnwkmn 0:97a4f8cc534c 53 }
jhnwkmn 0:97a4f8cc534c 54 return true;
jhnwkmn 0:97a4f8cc534c 55 }
jhnwkmn 0:97a4f8cc534c 56 bool GrowBufOf(SQInteger n)
jhnwkmn 0:97a4f8cc534c 57 {
jhnwkmn 0:97a4f8cc534c 58 bool ret = true;
jhnwkmn 0:97a4f8cc534c 59 if(_size + n > _allocated) {
jhnwkmn 0:97a4f8cc534c 60 if(_size + n > _size * 2)
jhnwkmn 0:97a4f8cc534c 61 ret = Resize(_size + n);
jhnwkmn 0:97a4f8cc534c 62 else
jhnwkmn 0:97a4f8cc534c 63 ret = Resize(_size * 2);
jhnwkmn 0:97a4f8cc534c 64 }
jhnwkmn 0:97a4f8cc534c 65 _size = _size + n;
jhnwkmn 0:97a4f8cc534c 66 return ret;
jhnwkmn 0:97a4f8cc534c 67 }
jhnwkmn 0:97a4f8cc534c 68 bool CanAdvance(SQInteger n) {
jhnwkmn 0:97a4f8cc534c 69 if(_ptr+n>_size)return false;
jhnwkmn 0:97a4f8cc534c 70 return true;
jhnwkmn 0:97a4f8cc534c 71 }
jhnwkmn 0:97a4f8cc534c 72 SQInteger Seek(SQInteger offset, SQInteger origin) {
jhnwkmn 0:97a4f8cc534c 73 switch(origin) {
jhnwkmn 0:97a4f8cc534c 74 case SQ_SEEK_SET:
jhnwkmn 0:97a4f8cc534c 75 if(offset > _size || offset < 0) return -1;
jhnwkmn 0:97a4f8cc534c 76 _ptr = offset;
jhnwkmn 0:97a4f8cc534c 77 break;
jhnwkmn 0:97a4f8cc534c 78 case SQ_SEEK_CUR:
jhnwkmn 0:97a4f8cc534c 79 if(_ptr + offset > _size || _ptr + offset < 0) return -1;
jhnwkmn 0:97a4f8cc534c 80 _ptr += offset;
jhnwkmn 0:97a4f8cc534c 81 break;
jhnwkmn 0:97a4f8cc534c 82 case SQ_SEEK_END:
jhnwkmn 0:97a4f8cc534c 83 if(_size + offset > _size || _size + offset < 0) return -1;
jhnwkmn 0:97a4f8cc534c 84 _ptr = _size + offset;
jhnwkmn 0:97a4f8cc534c 85 break;
jhnwkmn 0:97a4f8cc534c 86 default: return -1;
jhnwkmn 0:97a4f8cc534c 87 }
jhnwkmn 0:97a4f8cc534c 88 return 0;
jhnwkmn 0:97a4f8cc534c 89 }
jhnwkmn 0:97a4f8cc534c 90 bool IsValid() {
jhnwkmn 0:97a4f8cc534c 91 return _buf?true:false;
jhnwkmn 0:97a4f8cc534c 92 }
jhnwkmn 0:97a4f8cc534c 93 bool EOS() {
jhnwkmn 0:97a4f8cc534c 94 return _ptr == _size;
jhnwkmn 0:97a4f8cc534c 95 }
jhnwkmn 0:97a4f8cc534c 96 SQInteger Flush() { return 0; }
jhnwkmn 0:97a4f8cc534c 97 SQInteger Tell() { return _ptr; }
jhnwkmn 0:97a4f8cc534c 98 SQInteger Len() { return _size; }
jhnwkmn 0:97a4f8cc534c 99 SQUserPointer GetBuf(){ return _buf; }
jhnwkmn 0:97a4f8cc534c 100 private:
jhnwkmn 0:97a4f8cc534c 101 SQInteger _size;
jhnwkmn 0:97a4f8cc534c 102 SQInteger _allocated;
jhnwkmn 0:97a4f8cc534c 103 SQInteger _ptr;
jhnwkmn 0:97a4f8cc534c 104 unsigned char *_buf;
jhnwkmn 0:97a4f8cc534c 105 bool _owns;
jhnwkmn 0:97a4f8cc534c 106 };
jhnwkmn 0:97a4f8cc534c 107
jhnwkmn 0:97a4f8cc534c 108 #endif //_SQSTD_BLOBIMPL_H_