summaryrefslogtreecommitdiff
path: root/xmake/scripts/base/io.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2015-06-09 09:34:34 +0800
committerruki <[email protected]>2015-06-09 09:34:34 +0800
commit387b3666c4d952e01b96bce7e9bc51ed86fe986f (patch)
tree22f84c87354ecf8bc5c9bb2be3d21a207dba1667 /xmake/scripts/base/io.lua
parent723ec3e970e17f48a601fe2a919b9802b5e516fa (diff)
update the configure format
Diffstat (limited to 'xmake/scripts/base/io.lua')
-rw-r--r--xmake/scripts/base/io.lua74
1 files changed, 67 insertions, 7 deletions
diff --git a/xmake/scripts/base/io.lua b/xmake/scripts/base/io.lua
index a1ac152ce..4a5a3b65a 100644
--- a/xmake/scripts/base/io.lua
+++ b/xmake/scripts/base/io.lua
@@ -88,18 +88,78 @@ function io._save_with_level(file, object, level)
return true
end
--- save object
-function io.save(file, object, prefix)
+-- save object to given file
+function io._save(file, object)
- -- save prefix
- if prefix and type(prefix) == "string" then
- file:write(prefix)
- end
-
-- save it
return io._save_with_level(file, object, 0)
end
+-- save object the the given filepath
+function io.save(filepath, object)
+
+ -- open the file
+ local file = io.open(filepath, "w")
+ if not file then
+ -- error
+ return false, string.format("open %s failed!", filepath)
+ end
+
+ -- save object to file
+ if not io._save(file, object) then
+ -- error
+ file:close()
+ return false, string.format("save %s failed!", filepath)
+ end
+
+ -- close file
+ file:close()
+
+ -- ok
+ return true
+end
+
+-- load object from the given file
+function io.load(filepath)
+
+ -- open the file
+ local file = io.open(filepath, "r")
+ if not file then
+ -- error
+ return nil, string.format("open %s failed!", filepath)
+ end
+
+ -- load data
+ local result = nil
+ local errors = nil
+ local data = file:read("*all")
+ if data and type(data) == "string" then
+
+ -- load script
+ local script = loadstring("return " .. data)
+ if script then
+
+ -- load object
+ local ok, object = pcall(script)
+ if ok and object then
+ result = object
+ elseif object then
+ -- error
+ errors = object
+ else
+ -- error
+ errors = string.format("load %s failed!", filepath)
+ end
+ end
+ end
+
+ -- close file
+ file:close()
+
+ -- ok?
+ return result, errors
+end
+
-- cat the given file
function io.cat(filepath)