aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHisham Muhammad <hisham@gobolinux.org>2012-07-11 12:47:33 -0700
committerHisham Muhammad <hisham@gobolinux.org>2012-07-11 12:47:33 -0700
commit2c5ea058d859fda9a139c35c4af787e898fd502e (patch)
tree9a1da6f02f9f15c7ebb4bdb871f0be3d6895da99
parente825a01d504bfeac265618257530c73357be9c10 (diff)
parenta040b9e5e41ceee016bc7b8820a0e6738666fdc6 (diff)
downloadluarocks-2c5ea058d859fda9a139c35c4af787e898fd502e.tar.gz
luarocks-2c5ea058d859fda9a139c35c4af787e898fd502e.tar.bz2
luarocks-2c5ea058d859fda9a139c35c4af787e898fd502e.zip
Merge pull request #79 from norman/save-to-string
Added save_from_table_to_string to persist module
-rw-r--r--src/luarocks/persist.lua34
1 files changed, 29 insertions, 5 deletions
diff --git a/src/luarocks/persist.lua b/src/luarocks/persist.lua
index 0d37b04a..132c15b7 100644
--- a/src/luarocks/persist.lua
+++ b/src/luarocks/persist.lua
@@ -125,6 +125,34 @@ write_table = function(out, tbl, level, field_order)
125 out:write("}") 125 out:write("}")
126end 126end
127 127
128--- Writes a rockspec table to an io-like object.
129-- @param out userdata: a file object, open for writing.
130-- @param tbl table: the table to be written.
131-- @param field_order table: optional prioritization table
132-- @return userdata The file object originally passed in as the `out` parameter.
133local function write_rockspec(out, tbl, field_order)
134 for k, v, sub_order in util.sortedpairs(tbl, field_order) do
135 out:write(k.." = ")
136 write_value(out, v, 0, sub_order)
137 out:write("\n")
138 end
139 return out
140end
141
142--- Save the contents of a table to a string.
143-- Each element of the table is saved as a global assignment.
144-- Only numbers, strings and tables (containing numbers, strings
145-- or other recursively processed tables) are supported.
146-- @param tbl table: the table containing the data to be written
147-- @param field_order table: an optional array indicating the order of top-level fields.
148-- @return string
149function save_from_table_to_string(tbl, field_order)
150 local out = {buffer = {}}
151 function out:write(data) table.insert(self.buffer, data) end
152 write_rockspec(out, tbl, field_order)
153 return table.concat(out.buffer)
154end
155
128--- Save the contents of a table in a file. 156--- Save the contents of a table in a file.
129-- Each element of the table is saved as a global assignment. 157-- Each element of the table is saved as a global assignment.
130-- Only numbers, strings and tables (containing numbers, strings 158-- Only numbers, strings and tables (containing numbers, strings
@@ -139,11 +167,7 @@ function save_from_table(filename, tbl, field_order)
139 if not out then 167 if not out then
140 return nil, "Cannot create file at "..filename 168 return nil, "Cannot create file at "..filename
141 end 169 end
142 for k, v, sub_order in util.sortedpairs(tbl, field_order) do 170 write_rockspec(out, tbl, field_order)
143 out:write(k.." = ")
144 write_value(out, v, 0, sub_order)
145 out:write("\n")
146 end
147 out:close() 171 out:close()
148 return true 172 return true
149end 173end