summaryrefslogtreecommitdiff
path: root/xmake/scripts/base/io.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2015-05-12 11:31:12 +0800
committerruki <[email protected]>2015-05-12 11:31:12 +0800
commit97e6e62e5061f029b1195784671dea9d19211782 (patch)
treed29e8b9efb6b5f2be5b5495cc510f35d5587de15 /xmake/scripts/base/io.lua
parentfc66b05d2d238830106890447db7ed3808375de1 (diff)
load and save configs for the given target
Diffstat (limited to 'xmake/scripts/base/io.lua')
-rw-r--r--xmake/scripts/base/io.lua104
1 files changed, 104 insertions, 0 deletions
diff --git a/xmake/scripts/base/io.lua b/xmake/scripts/base/io.lua
new file mode 100644
index 000000000..28bc5f24d
--- /dev/null
+++ b/xmake/scripts/base/io.lua
@@ -0,0 +1,104 @@
+--!The Automatic Cross-platform Build Tool
+--
+-- XMake is free software; you can redistribute it and/or modify
+-- it under the terms of the GNU Lesser General Public License as published by
+-- the Free Software Foundation; either version 2.1 of the License, or
+-- (at your option) any later version.
+--
+-- XMake is distributed in the hope that it will be useful,
+-- but WITHOUT ANY WARRANTY; without even the implied warranty of
+-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+-- GNU Lesser General Public License for more details.
+--
+-- You should have received a copy of the GNU Lesser General Public License
+-- along with XMake;
+-- If not, see <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a>
+--
+-- Copyright (C) 2009 - 2015, ruki All rights reserved.
+--
+-- @author ruki
+-- @file io.lua
+--
+
+-- define module: io
+local io = io or {}
+
+-- load modules
+local utils = require("base/utils")
+
+-- save object with the level
+function io._save_with_level(file, object, level)
+
+ -- check
+ assert(object)
+
+ -- save string
+ if type(object) == "string" then
+ file:write(string.format("%q", object))
+ -- save boolean
+ elseif type(object) == "boolean" then
+ file:write(tostring(object))
+ -- save number
+ elseif type(object) == "number" then
+ file:write(object)
+ -- save table
+ elseif type(object) == "table" then
+
+ -- save head
+ file:write("\n")
+ for l = 1, level do
+ file:write(" ")
+ end
+ file:write("{\n")
+
+ -- save body
+ local i = 0
+ for k, v in pairs(object) do
+
+ -- save spaces
+ for l = 1, level do
+ file:write(" ")
+ end
+
+ -- save separator
+ file:write(utils.ifelse(i == 0, " ", ", "), k, " = ")
+
+ -- save this key: value
+ if not io._save_with_level(file, v, level + 1) then
+ return false
+ end
+
+ -- save newline
+ file:write("\n")
+ i = i + 1
+ end
+
+ -- save tail
+ for l = 1, level do
+ file:write(" ")
+ end
+ file:write("}\n")
+ else
+ -- error
+ utils.error("invalid object type: %s", type(object))
+ return false
+ end
+
+ -- ok
+ return true
+end
+
+-- save object
+function io.save(file, object, prefix)
+
+ -- save prefix
+ if prefix and type(prefix) == "string" then
+ file:write(prefix)
+ end
+
+ -- save it
+ return io._save_with_level(file, object, 0)
+end
+
+-- return module: io
+return io