diff options
author | Norman Clarke <norman@njclarke.com> | 2012-06-22 17:34:57 -0300 |
---|---|---|
committer | Norman Clarke <norman@njclarke.com> | 2012-06-22 17:34:57 -0300 |
commit | a7d4911df956d19d5f45cf914443eb1d473bceb9 (patch) | |
tree | 91127cf11a4c93f4477311a40b82646d6ba9b1a2 | |
parent | 770aed1446b9cc492f141c46c4be49a50d8c5f61 (diff) | |
download | luarocks-a7d4911df956d19d5f45cf914443eb1d473bceb9.tar.gz luarocks-a7d4911df956d19d5f45cf914443eb1d473bceb9.tar.bz2 luarocks-a7d4911df956d19d5f45cf914443eb1d473bceb9.zip |
Added save_from_table_to_string to persist module
-rw-r--r-- | src/luarocks/persist.lua | 26 |
1 files 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) | |||
125 | out:write("}") | 125 | out:write("}") |
126 | end | 126 | end |
127 | 127 | ||
128 | --- Save the contents of a table to a string. | ||
129 | -- Each element of the table is saved as a global assignment. | ||
130 | -- Only numbers, strings and tables (containing numbers, strings | ||
131 | -- or other recursively processed tables) are supported. | ||
132 | -- @param tbl table: the table containing the data to be written | ||
133 | -- @param field_order table: an optional array indicating the order of top-level fields. | ||
134 | -- @return string or (nil, string): string if successful, or nil and a | ||
135 | -- message in case of errors. | ||
136 | function save_from_table_to_string(tbl, field_order) | ||
137 | local out = {buffer = {}} | ||
138 | function out:write(data) table.insert(self.buffer, data) end | ||
139 | for k, v, sub_order in util.sortedpairs(tbl, field_order) do | ||
140 | out:write(k.." = ") | ||
141 | write_value(out, v, 0, sub_order) | ||
142 | out:write("\n") | ||
143 | end | ||
144 | return table.concat(out.buffer) | ||
145 | end | ||
146 | |||
147 | |||
128 | --- Save the contents of a table in a file. | 148 | --- Save the contents of a table in a file. |
129 | -- Each element of the table is saved as a global assignment. | 149 | -- Each element of the table is saved as a global assignment. |
130 | -- Only numbers, strings and tables (containing numbers, strings | 150 | -- Only numbers, strings and tables (containing numbers, strings |
@@ -139,11 +159,7 @@ function save_from_table(filename, tbl, field_order) | |||
139 | if not out then | 159 | if not out then |
140 | return nil, "Cannot create file at "..filename | 160 | return nil, "Cannot create file at "..filename |
141 | end | 161 | end |
142 | for k, v, sub_order in util.sortedpairs(tbl, field_order) do | 162 | out:write(save_from_table_to_string(tbl, field_order)) |
143 | out:write(k.." = ") | ||
144 | write_value(out, v, 0, sub_order) | ||
145 | out:write("\n") | ||
146 | end | ||
147 | out:close() | 163 | out:close() |
148 | return true | 164 | return true |
149 | end | 165 | end |