diff --git a/DynDNS/dyndns.lua b/DynDNS/dyndns.lua index dd74e8f..05fdb31 100644 --- a/DynDNS/dyndns.lua +++ b/DynDNS/dyndns.lua @@ -74,6 +74,48 @@ function read_file(path) return content end +function pairs_by_keys(t, f) + local a = {} + for n in pairs(t) do + table.insert(a, n) + end + table.sort(a, f) + local i = 0 -- iterator variable + local iter = function () -- iterator function + i = i + 1 + if a[i] == nil then + return nil + else + return a[i], t[a[i]] + end + end + return iter +end + +function sort_table(my_table) + local t = {} + for title,value in pairs_by_keys(my_table) do + table.insert(t, { title = title, value = value }) + end + my_table = t + return my_table +end + +function compare_tables(a,b) + if #a ~= #b then return false end + local t1,t2 = {}, {} + for k,v in pairs(a) do + t1[k] = (t1[k] or 0) + 1 + end + for k,v in pairs(b) do + t2[k] = (t2[k] or 0) + 1 + end + for k,v in pairs(t1) do + if v ~= t2[k] then return false end + end + return true +end + array = {} @@ -135,9 +177,14 @@ for key,value in pairs(array) do end end -if read_file(output_file) ~= config then +config_file = read_file(output_file) + +c1 = sort_table(split(config_file, "\r\n")) +c2 = sort_table(split(config, "\r\n")) + +if not compare_tables(c1, c2) then file = io.open(output_file, "w") file:write(config) file:close() os.execute(reload_command) -end \ No newline at end of file +end