6 #include "sqstdstream.h" 8 #define SQSTD_FILE_TYPE_TAG (SQSTD_STREAM_TYPE_TAG | 0x00000001) 13 return (SQFILE)fopen(filename,mode);
15 return (SQFILE)_wfopen(filename,mode);
19 SQInteger
sqstd_fread(
void* buffer, SQInteger size, SQInteger count, SQFILE file)
21 return (SQInteger)fread(buffer,size,count,(FILE *)file);
24 SQInteger
sqstd_fwrite(
const SQUserPointer buffer, SQInteger size, SQInteger count, SQFILE file)
26 return (SQInteger)fwrite(buffer,size,count,(FILE *)file);
29 SQInteger
sqstd_fseek(SQFILE file, SQInteger offset, SQInteger origin)
33 case SQ_SEEK_CUR: realorigin = SEEK_CUR;
break;
34 case SQ_SEEK_END: realorigin = SEEK_END;
break;
35 case SQ_SEEK_SET: realorigin = SEEK_SET;
break;
38 return fseek((FILE *)file,(
long)offset,(
int)realorigin);
43 return ftell((FILE *)file);
48 return fflush((FILE *)file);
53 return fclose((FILE *)file);
58 return feof((FILE *)file);
81 SQInteger
Read(
void *buffer,SQInteger size) {
84 SQInteger
Write(
void *buffer,SQInteger size) {
94 SQInteger prevpos=
Tell();
96 SQInteger size=
Tell();
97 Seek(prevpos,SQ_SEEK_SET);
100 SQInteger
Seek(SQInteger offset, SQInteger origin) {
127 const SQChar *filename,*mode;
136 }
else if(
sq_gettype(v,2) == OT_USERPOINTER) {
142 f =
new SQFile(newf,owns);
145 return sq_throwerror(v, _SC(
"cannot create blob with negative size"));
152 #define _DECL_FILE_FUNC(name,nparams,typecheck) {_SC(#name),_file_##name,nparams,typecheck} 166 if(SQ_SUCCEEDED(
sq_get(v,-2))) {
176 if(SQ_SUCCEEDED(
sq_call(v,3,SQTrue,SQFalse) )) {
189 *file = fileobj->GetHandle();
201 if( ( ret=
sqstd_fread(&c,
sizeof(c),1,(FILE *)file )>0) )
209 if(sqstd_fread(&inchar,sizeof(inchar),1,(FILE *)file) != 1) \ 212 static const SQInteger utf8_lengths[16] =
220 static unsigned char byte_masks[5] = {0,0,0x1f,0x0f,0x07};
221 unsigned char inchar;
228 SQInteger codelen = utf8_lengths[c>>4];
232 tmp = c&byte_masks[codelen];
233 for(SQInteger n = 0; n < codelen-1; n++) {
236 tmp |= inchar & 0x3F;
247 if( ( ret=
sqstd_fread(&c,
sizeof(c),1,(FILE *)file )>0) )
256 if( ( ret=
sqstd_fread(&c,
sizeof(c),1,(FILE *)file )>0) ) {
257 c = ((c>>8)&0x00FF)| ((c<<8)&0xFF00);
263 SQInteger
file_read(SQUserPointer file,SQUserPointer buf,SQInteger size)
266 if( ( ret =
sqstd_fread(buf,1,size,(SQFILE)file ))!=0 )
return ret;
270 SQInteger
file_write(SQUserPointer file,SQUserPointer p,SQInteger size)
288 if(us == SQ_BYTECODE_STREAM_TAG) {
315 if(SQ_SUCCEEDED(
sq_compile(v,func,file,filename,printerror))){
330 if(SQ_SUCCEEDED(
sq_call(v,1,retval,SQTrue))) {
342 if(!file)
return sq_throwerror(v,_SC(
"cannot open the file"));
354 SQBool printerror = SQFalse;
376 SQBool printerror = SQFalse;
382 if(SQ_SUCCEEDED(
sqstd_dofile(v,filename,SQTrue,printerror)))
387 #define _DECL_GLOBALIO_FUNC(name,nparams,typecheck) {_SC(#name),_g_io_##name,nparams,typecheck} #define SQSTD_FILE_TYPE_TAG
SQRESULT sq_get(HSQUIRRELVM v, SQInteger idx)
SQRESULT sq_getinstanceup(HSQUIRRELVM v, SQInteger idx, SQUserPointer *p, SQUserPointer typetag)
SQInteger Seek(SQInteger offset, SQInteger origin)
SQInteger sqstd_fflush(SQFILE file)
SQInteger _g_io_writeclosuretofile(HSQUIRRELVM v)
void sq_pop(HSQUIRRELVM v, SQInteger nelemstopop)
SQInteger sqstd_ftell(SQFILE file)
SQInteger sqstd_fread(void *buffer, SQInteger size, SQInteger count, SQFILE file)
SQInteger sqstd_fseek(SQFILE file, SQInteger offset, SQInteger origin)
SQRESULT sq_throwerror(HSQUIRRELVM v, const SQChar *err)
static SQRegFunction iolib_funcs[]
void sq_pushnull(HSQUIRRELVM v)
SQInteger _g_io_loadfile(HSQUIRRELVM v)
void sq_pushroottable(HSQUIRRELVM v)
void sq_pushregistrytable(HSQUIRRELVM v)
SQInteger sqstd_feof(SQFILE file)
SQRESULT sq_writeclosure(HSQUIRRELVM v, SQWRITEFUNC w, SQUserPointer up)
#define _DECL_FILE_FUNC(name, nparams, typecheck)
SQRESULT sqstd_createfile(HSQUIRRELVM v, SQFILE file, SQBool own)
static SQInteger _io_file_lexfeed_UTF8(SQUserPointer file)
SQInteger sq_gettop(HSQUIRRELVM v)
void sq_pushinteger(HSQUIRRELVM v, SQInteger n)
SQRESULT sq_call(HSQUIRRELVM v, SQInteger params, SQBool retval, SQBool raiseerror)
void sq_push(HSQUIRRELVM v, SQInteger idx)
SQInteger _g_io_dofile(HSQUIRRELVM v)
SQRESULT sqstd_getfile(HSQUIRRELVM v, SQInteger idx, SQFILE *file)
SQRESULT sqstd_writeclosuretofile(HSQUIRRELVM v, const SQChar *filename)
static SQInteger _io_file_lexfeed_UCS2_LE(SQUserPointer file)
void sq_settop(HSQUIRRELVM v, SQInteger newtop)
SQRESULT sq_compile(HSQUIRRELVM v, SQLEXREADFUNC read, SQUserPointer p, const SQChar *sourcename, SQBool raiseerror)
SQInteger sqstd_fwrite(const SQUserPointer buffer, SQInteger size, SQInteger count, SQFILE file)
static SQInteger _file__typeof(HSQUIRRELVM v)
static SQInteger _file_releasehook(SQUserPointer p, SQInteger)
void sq_remove(HSQUIRRELVM v, SQInteger idx)
SQInteger Read(void *buffer, SQInteger size)
void sq_pushstring(HSQUIRRELVM v, const SQChar *s, SQInteger len)
SQRESULT sq_setinstanceup(HSQUIRRELVM v, SQInteger idx, SQUserPointer p)
SQObjectType sq_gettype(HSQUIRRELVM v, SQInteger idx)
SQInteger sqstd_fclose(SQFILE file)
static SQRegFunction _file_methods[]
SQRESULT sq_getstring(HSQUIRRELVM v, SQInteger idx, const SQChar **c)
SQRESULT sqstd_register_iolib(HSQUIRRELVM v)
SQRESULT declare_stream(HSQUIRRELVM v, const SQChar *name, SQUserPointer typetag, const SQChar *reg_name, SQRegFunction *methods, SQRegFunction *globals)
SQRESULT sq_readclosure(HSQUIRRELVM v, SQREADFUNC r, SQUserPointer up)
SQRESULT sq_getuserpointer(HSQUIRRELVM v, SQInteger idx, SQUserPointer *p)
#define _DECL_GLOBALIO_FUNC(name, nparams, typecheck)
SQRESULT sq_getbool(HSQUIRRELVM v, SQInteger idx, SQBool *b)
SQRESULT sqstd_dofile(HSQUIRRELVM v, const SQChar *filename, SQBool retval, SQBool printerror)
SQFile(SQFILE file, bool owns)
static SQInteger _file_constructor(HSQUIRRELVM v)
SQInteger file_write(SQUserPointer file, SQUserPointer p, SQInteger size)
SQRESULT sqstd_loadfile(HSQUIRRELVM v, const SQChar *filename, SQBool printerror)
static SQInteger _io_file_lexfeed_UCS2_BE(SQUserPointer file)
SQInteger file_read(SQUserPointer file, SQUserPointer buf, SQInteger size)
void sq_setreleasehook(HSQUIRRELVM v, SQInteger idx, SQRELEASEHOOK hook)
SQInteger Write(void *buffer, SQInteger size)
static SQInteger _io_file_lexfeed_ASCII(SQUserPointer file)
void sq_pushuserpointer(HSQUIRRELVM v, SQUserPointer p)
SQFILE sqstd_fopen(const SQChar *filename, const SQChar *mode)
bool Open(const SQChar *filename, const SQChar *mode)