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 From a040b9e5e41ceee016bc7b8820a0e6738666fdc6 Mon Sep 17 00:00:00 2001 From: Norman Clarke Date: Wed, 27 Jun 2012 10:52:50 -0300 Subject: Refactor io into separate function Avoid writing the entire spec into a string in save_from_table when we can just write it directly to a file handle. --- src/luarocks/persist.lua | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/luarocks/persist.lua b/src/luarocks/persist.lua index b7b5252d..132c15b7 100644 --- a/src/luarocks/persist.lua +++ b/src/luarocks/persist.lua @@ -125,26 +125,34 @@ write_table = function(out, tbl, level, field_order) out:write("}") end +--- Writes a rockspec table to an io-like object. +-- @param out userdata: a file object, open for writing. +-- @param tbl table: the table to be written. +-- @param field_order table: optional prioritization table +-- @return userdata The file object originally passed in as the `out` parameter. +local function write_rockspec(out, tbl, field_order) + 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 out +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. +-- @return string 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 + write_rockspec(out, tbl, field_order) 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 @@ -159,7 +167,7 @@ function save_from_table(filename, tbl, field_order) if not out then return nil, "Cannot create file at "..filename end - out:write(save_from_table_to_string(tbl, field_order)) + write_rockspec(out, tbl, field_order) out:close() return true end -- cgit v1.2.3-55-g6feb