summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOpportunityLiu <[email protected]>2019-07-26 11:05:33 +0800
committerOpportunityLiu <[email protected]>2019-07-28 14:45:00 +0800
commit71d253bc95fe107ee401f37a8d6405fac96651fa (patch)
tree50dc3dac1646e4c8bae1cd82eaeaa798eebd26de
parente9902d526b04319bb73dddabab17c5e339dba99a (diff)
clean up code
-rw-r--r--xmake/core/base/io.lua32
1 files changed, 14 insertions, 18 deletions
diff --git a/xmake/core/base/io.lua b/xmake/core/base/io.lua
index a7e18f38e..428fdc8c7 100644
--- a/xmake/core/base/io.lua
+++ b/xmake/core/base/io.lua
@@ -65,15 +65,19 @@ end
-- save object
function _file:save(object)
local str, errors = string.serialize(object, false)
- if str then
+ if not errors then
self:write(str)
+ return str
end
return str, errors
end
-- load object
function _file:load()
- local data = self:read("*all")
+ local data, err = self:read("*all")
+ if err then
+ return nil, err
+ end
if data and type(data) == "string" then
return data:deserialize()
end
@@ -214,30 +218,22 @@ function io.save(filepath, object, opt)
-- init option
opt = opt or {}
- -- ensure directory
- local dir = path.directory(filepath)
- if not os.isdir(dir) then
- os.mkdir(dir)
- end
-
-- open the file
- local file = io.open(filepath, "wb", opt)
- if not file then
+ local file, err = io.open(filepath, "wb", opt)
+ if err then
-- error
- return false, string.format("open %s failed!", filepath)
+ return false, err
end
-- save object to file
local ok, errors = file:save(object)
+ -- close file
+ file:close()
if not ok then
-- error
- file:close()
return false, string.format("save %s failed, %s!", filepath, errors)
end
- -- close file
- file:close()
-
-- ok
return true
end
@@ -252,10 +248,10 @@ function io.load(filepath, opt)
opt = opt or {}
-- open the file
- local file = io.open(filepath, "rb", opt)
- if not file then
+ local file, err = io.open(filepath, "rb", opt)
+ if err then
-- error
- return nil, string.format("open %s failed!", filepath)
+ return nil, err
end
-- load object