From a7d4911df956d19d5f45cf914443eb1d473bceb9 Mon Sep 17 00:00:00 2001 From: Norman Clarke Date: Fri, 22 Jun 2012 17:34:57 -0300 Subject: Added save_from_table_to_string to persist module --- src/luarocks/persist.lua | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/luarocks/persist.lua b/src/luarocks/persist.lua index 0d37b04a..b7b5252d 100644 --- a/src/luarocks/persist.lua +++ b/src/luarocks/persist.lua @@ -125,6 +125,26 @@ write_table = function(out, tbl, level, field_order) out:write("}") end +--- Save the contents of a table to a string. +-- Each element of the table is saved as a global assignment. +-- Only numbers, strings and tables (containing numbers, strings +-- or other recursively processed tables) are supported. +-- @param tbl table: the table containing the data to be written +-- @param field_order table: an optional array indicating the order of top-level fields. +-- @return string or (nil, string): string if successful, or nil and a +-- message in case of errors. +function save_from_table_to_string(tbl, field_order) + local out = {buffer = {}} + function out:write(data) table.insert(self.buffer, data) end + for k, v, sub_order in util.sortedpairs(tbl, field_order) do + out:write(k.." = ") + write_value(out, v, 0, sub_order) + out:write("\n") + end + return table.concat(out.buffer) +end + + --- Save the contents of a table in a file. -- Each element of the table is saved as a global assignment. -- Only numbers, strings and tables (containing numbers, strings @@ -139,11 +159,7 @@ function save_from_table(filename, tbl, field_order) if not out then return nil, "Cannot create file at "..filename end - for k, v, sub_order in util.sortedpairs(tbl, field_order) do - out:write(k.." = ") - write_value(out, v, 0, sub_order) - out:write("\n") - end + out:write(save_from_table_to_string(tbl, field_order)) out:close() return true end -- cgit v1.2.3-55-g6feb