diff options
| author | ruki <[email protected]> | 2015-06-20 20:11:59 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2015-06-20 20:11:59 +0800 |
| commit | 8452df53aca437c49963afdf9738022c198365a5 (patch) | |
| tree | 799eb694225028702484301434c8690a1d6efcc8 /xmake/scripts/base/io.lua | |
| parent | cd3922c36588023c8e0ffaeccb2fc2c95b05dd20 (diff) | |
replace the project target name
Diffstat (limited to 'xmake/scripts/base/io.lua')
| -rw-r--r-- | xmake/scripts/base/io.lua | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/xmake/scripts/base/io.lua b/xmake/scripts/base/io.lua index c6be077be..9c0ece688 100644 --- a/xmake/scripts/base/io.lua +++ b/xmake/scripts/base/io.lua @@ -113,6 +113,46 @@ function io.openmk(filepath) return io.open(filepath, "w") end +-- read all data from file +function io.readall(filepath) + + -- open file + local file = io.open(filepath, "r") + if not file then + -- error + return nil, string.format("open %s failed!", filepath) + end + + -- read all + local data = file:read("*all") + + -- exit file + file:close() + + -- ok? + return data +end + +-- write all data to file +function io.writall(filepath, data) + + -- open file + local file = io.open(filepath, "w") + if not file then + -- error + return false, string.format("open %s failed!", filepath) + end + + -- write all + file:write(data) + + -- exit file + file:close() + + -- ok? + return true +end + -- save object the the given filepath function io.save(filepath, object) @@ -179,6 +219,32 @@ function io.load(filepath) return result, errors end +-- gsub the given file and return replaced data +function io.gsub(filepath, pattern, replace) + + -- read all data from file + local data, errors = io.readall(filepath) + if not data then return nil, 0, errors end + + -- replace it + local count = 0 + if type(data) == "string" then + data, count = data:gsub(pattern, replace) + else + return nil, 0, string.format("data is not string!") + end + + -- replace ok? + if count ~= 0 then + -- write all data to file + local ok, errors = io.writall(filepath, data) + if not ok then return nil, 0, errors end + end + + -- ok + return data, count +end + -- cat the given file function io.cat(filepath, linecount) |
