File: persist.lua

package info (click to toggle)
crawl 2%3A0.33.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 95,264 kB
  • sloc: cpp: 358,145; ansic: 27,203; javascript: 9,491; python: 8,359; perl: 3,327; java: 2,667; xml: 2,191; makefile: 1,830; sh: 611; objc: 250; cs: 15; sed: 9; lisp: 3
file content (51 lines) | stat: -rw-r--r-- 1,388 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
-- persist.lua
-- Functions to persist the lua table c_persist

-- We want to load this file immediately before calling c_save_persist() to
-- ensure that the functions haven't been overwritten by player versions.

function stringify(x)
    local t = type(x)
    if t == "nil" then
        return "nil"
    elseif t == "number" then
        return tostring(x)
    elseif t == "string" then
        return string.format("%q", x)
    elseif t == "boolean" then
        return x and "true" or "false"
    else
        error("Cannot stringify objects of type " .. t .. debug.traceback())
    end
end

function stringify_table(tab,indent_level)
  if not indent_level then
    indent_level = 0
  end
  local spaces = ""
  for i = 1,2 * indent_level + 1 do
    spaces = spaces .. " "
  end
  local res = spaces .. "{\n"
  for key,val in pairs(tab) do
    res = res .. spaces .. " [" .. stringify(key) .. "] ="
    if type(val) ~= "table" then
      res = res .. " " .. stringify(val) .. ",\n"
    elseif next(val) == nil then -- table is empty
      res = res .. " { },\n"
    else
      res = res .. "\n" .. stringify_table(val,indent_level + 1) .. ",\n"
    end
  end
  res = res .. spaces .. "}"
  return res
end

function c_save_persist()
    if next(c_persist) == nil then -- table is empty
      return ""
    end
    str = stringify_table(c_persist)
    return "c_persist =\n" .. str
end