Extra "ports" of juce-based plugins using the distrho build system
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.1KB

  1. ----------------------------------------------
  2. -- Pickle.lua
  3. -- A table serialization utility for lua
  4. -- Steve Dekorte, http://www.dekorte.com, Apr 2000
  5. -- Freeware
  6. ----------------------------------------------
  7. function pickle(t)
  8. return Pickle:clone():pickle_(t)
  9. end
  10. Pickle = {
  11. clone = function (t)
  12. local nt={};
  13. for i, v in pairs(t) do
  14. nt[i]=v
  15. end
  16. return nt
  17. end
  18. }
  19. function Pickle:pickle_(root)
  20. if type(root) ~= "table" then
  21. error("can only pickle tables, not ".. type(root).."s")
  22. end
  23. self._tableToRef = {}
  24. self._refToTable = {}
  25. local savecount = 0
  26. self:ref_(root)
  27. local s = ""
  28. while table.getn(self._refToTable) > savecount do
  29. savecount = savecount + 1
  30. local t = self._refToTable[savecount]
  31. s = s.."{\n"
  32. for i, v in pairs(t) do
  33. s = string.format("%s[%s]=%s,\n", s, self:value_(i), self:value_(v))
  34. end
  35. s = s.."},\n"
  36. end
  37. return string.format("{%s}", s)
  38. end
  39. function Pickle:value_(v)
  40. local vtype = type(v)
  41. if vtype == "string" then return string.format("%q", v)
  42. elseif vtype == "number" then return v
  43. elseif vtype == "table" then return "{"..self:ref_(v).."}"
  44. else --error("pickle a "..type(v).." is not supported")
  45. end
  46. end
  47. function Pickle:ref_(t)
  48. local ref = self._tableToRef[t]
  49. if not ref then
  50. if t == self then error("can't pickle the pickle class") end
  51. table.insert(self._refToTable, t)
  52. ref = table.getn(self._refToTable)
  53. self._tableToRef[t] = ref
  54. end
  55. return ref
  56. end
  57. ----------------------------------------------
  58. -- unpickle
  59. ----------------------------------------------
  60. function unpickle(s)
  61. if type(s) ~= "string" then
  62. error("can't unpickle a "..type(s)..", only strings")
  63. end
  64. local gentables = loadstring("return "..s)
  65. local tables = gentables()
  66. for tnum = 1, table.getn(tables) do
  67. local t = tables[tnum]
  68. local tcopy = {}; for i, v in pairs(t) do tcopy[i] = v end
  69. for i, v in pairs(tcopy) do
  70. local ni, nv
  71. if type(i) == "table" then ni = tables[i[1]] else ni = i end
  72. if type(v) == "table" then nv = tables[v[1]] else nv = v end
  73. t[i] = nil
  74. t[ni] = nv
  75. end
  76. end
  77. return tables[1]
  78. end