فهرست منبع

Add functions:

readfile(file_name) reads the content of a file and return a string
writefile(file_name, data) open file_name for write and write data to it

Both function raise an exception if they can not open the file.
mingodad 10 سال پیش
والد
کامیت
d1aebcf669
1فایلهای تغییر یافته به همراه38 افزوده شده و 0 حذف شده
  1. 38 0
      SquiLu/sqstdlib/sqstdio.cpp

+ 38 - 0
SquiLu/sqstdlib/sqstdio.cpp

@@ -515,6 +515,42 @@ SQInteger _g_io_dofile(HSQUIRRELVM v)
 	return SQ_ERROR; //propagates the error
 }
 
+SQInteger _g_io_readfile(HSQUIRRELVM v)
+{
+    SQ_FUNC_VARS_NO_TOP(v);
+    SQ_GET_STRING(v, 2, filename);
+    SQFile fs;
+    if(fs.Open(filename, "rb"))
+    {
+        SQInteger size,res;
+        SQChar *data;
+        size = fs.Len();
+        data = sq_getscratchpad(v,size);
+        res = fs.Read(data,size);
+        if(res <= 0)
+            return sq_throwerror(v,_SC("no data left to read"));
+        sq_pushstring(v,data,res);
+        return 1;
+    }
+	return sq_throwerror(v,_SC("could not open file %s"), filename);
+}
+
+SQInteger _g_io_writefile(HSQUIRRELVM v)
+{
+    SQ_FUNC_VARS_NO_TOP(v);
+    SQ_GET_STRING(v, 2, filename);
+    SQ_GET_STRING(v, 3, data);
+    SQFile fs;
+    if(fs.Open(filename, "wb"))
+    {
+        SQInteger res = fs.Write(data, data_size);
+        if(res != data_size)
+            return sq_throwerror(v,_SC("could not write to file"));
+        return 0;
+    }
+	return sq_throwerror(v,_SC("could not open file %s"), filename);
+}
+
 #define _DECL_GLOBALIO_FUNC(name,nparams,typecheck) {_SC(#name),_g_io_##name,nparams,typecheck}
 static SQRegFunction iolib_funcs[]={
 	_DECL_GLOBALIO_FUNC(loadfile,-2,_SC(".sbb")),
@@ -523,6 +559,8 @@ static SQRegFunction iolib_funcs[]={
 	_DECL_GLOBALIO_FUNC(dostring,-2,_SC(".sb")),
 	_DECL_GLOBALIO_FUNC(loadstring,2,_SC(".s")),
 	_DECL_GLOBALIO_FUNC(dumpclosure,3,_SC(".sc")),
+	_DECL_GLOBALIO_FUNC(readfile,2,_SC(".s")),
+	_DECL_GLOBALIO_FUNC(writefile,3,_SC(".ss")),
 	{0,0}
 };