diff options
| author | Norman Clarke <norman@njclarke.com> | 2012-06-27 10:52:50 -0300 |
|---|---|---|
| committer | Norman Clarke <norman@njclarke.com> | 2012-06-27 10:57:08 -0300 |
| commit | a040b9e5e41ceee016bc7b8820a0e6738666fdc6 (patch) | |
| tree | a69f4fa9f483200e44370eac29c970a3ac100753 /src | |
| parent | a7d4911df956d19d5f45cf914443eb1d473bceb9 (diff) | |
| download | luarocks-a040b9e5e41ceee016bc7b8820a0e6738666fdc6.tar.gz luarocks-a040b9e5e41ceee016bc7b8820a0e6738666fdc6.tar.bz2 luarocks-a040b9e5e41ceee016bc7b8820a0e6738666fdc6.zip | |
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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/luarocks/persist.lua | 26 |
1 files 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) | |||
| 125 | out:write("}") | 125 | out:write("}") |
| 126 | end | 126 | end |
| 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. | ||
| 133 | local 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 | ||
| 140 | end | ||
| 141 | |||
| 128 | --- Save the contents of a table to a string. | 142 | --- Save the contents of a table to a string. |
| 129 | -- Each element of the table is saved as a global assignment. | 143 | -- Each element of the table is saved as a global assignment. |
| 130 | -- Only numbers, strings and tables (containing numbers, strings | 144 | -- Only numbers, strings and tables (containing numbers, strings |
| 131 | -- or other recursively processed tables) are supported. | 145 | -- or other recursively processed tables) are supported. |
| 132 | -- @param tbl table: the table containing the data to be written | 146 | -- @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. | 147 | -- @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 | 148 | -- @return string |
| 135 | -- message in case of errors. | ||
| 136 | function save_from_table_to_string(tbl, field_order) | 149 | function save_from_table_to_string(tbl, field_order) |
| 137 | local out = {buffer = {}} | 150 | local out = {buffer = {}} |
| 138 | function out:write(data) table.insert(self.buffer, data) end | 151 | function out:write(data) table.insert(self.buffer, data) end |
| 139 | for k, v, sub_order in util.sortedpairs(tbl, field_order) do | 152 | write_rockspec(out, tbl, field_order) |
| 140 | out:write(k.." = ") | ||
| 141 | write_value(out, v, 0, sub_order) | ||
| 142 | out:write("\n") | ||
| 143 | end | ||
| 144 | return table.concat(out.buffer) | 153 | return table.concat(out.buffer) |
| 145 | end | 154 | end |
| 146 | 155 | ||
| 147 | |||
| 148 | --- Save the contents of a table in a file. | 156 | --- Save the contents of a table in a file. |
| 149 | -- Each element of the table is saved as a global assignment. | 157 | -- Each element of the table is saved as a global assignment. |
| 150 | -- Only numbers, strings and tables (containing numbers, strings | 158 | -- Only numbers, strings and tables (containing numbers, strings |
| @@ -159,7 +167,7 @@ function save_from_table(filename, tbl, field_order) | |||
| 159 | if not out then | 167 | if not out then |
| 160 | return nil, "Cannot create file at "..filename | 168 | return nil, "Cannot create file at "..filename |
| 161 | end | 169 | end |
| 162 | out:write(save_from_table_to_string(tbl, field_order)) | 170 | write_rockspec(out, tbl, field_order) |
| 163 | out:close() | 171 | out:close() |
| 164 | return true | 172 | return true |
| 165 | end | 173 | end |
