Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 99 lines (87 sloc) 3.573 kB
fb7098d @fab13n Lua scheduler for linux: first release
fab13n authored
1
2 require 'utils.table' -- needed for table.pack. To be removed when we switch to Lua5.2
3
4 -- Make sure stdout is actually flushed every line
5 if io and io.stdout then
6 io.stdout:setvbuf("line")
7 end
8
9 local function quotestring(x)
10 local function q(k) return
11 k=='"' and '\\"' or
12 k=='\\' and '\\\\' or
13 string.format('\\%03d', k :byte())
14 end
15 return '"' .. x :gsub ('[%z\1-\9\11\12\14-\31\128-\255"\\]', q) .. '"'
16 end
17
18 ------------------------------------------------------------------------------
19 -- virtual printer: print a textual description of the objects in `...', using
20 -- function `write()' as a writer. By providing the appropriate writer, one
21 -- can make this function write on a TCP channel, in a string,... anywhere.
22 -- print_indent is the number of indentation characters used to render
23 -- tables. If nil/false, tables are rendered without carriage returns.
24 ------------------------------------------------------------------------------
25 function vprint (write, print_indent, ...)
26 local cache = { }
27 local indent_level = 0
28 local tinsert = table.insert
29 local function aux(x)
30 local t=type(x)
31 if t=="string" then write(quotestring(x))
32 elseif t=="boolean" then write(x and "true" or "false")
33 elseif t=="number" then write(tostring(x))
34 elseif t=="nil" then write("nil")
35 elseif t=="table" then
36 if cache[x] then write(tostring(table)) else
37 cache[x] = true
38 indent_level = indent_level+1
39
40 -- get the list of keys
41 local keys = {}
42 for k, _ in pairs(x) do tinsert(keys, k) end
43 local len = #keys
44
45 if print_indent and len > 1 then
46 write("{\r\n" .. string.rep(" ", print_indent * indent_level))
47 else write "{ " end
48
49
50 for i, k in ipairs(keys) do
51 -- implicit array idx
52 if k==i then -- do nothing...
53 -- keyword key
54 elseif type(k)=="string" and k:match"^[%a_][%w_]*$" then write(k .. " = ")
55 -- generic key
56 else write("["); aux(k); write("] = ") end
57 aux(x[k])
58 if i<len then
59 if print_indent then
60 write(",\r\n" .. string.rep(" ", print_indent * indent_level))
61 else write(", ") end
62 end
63 end
64 write (" }")
65
66 indent_level = indent_level-1
67 cache[x] = nil
68 end
69 else write(tostring(x)) end
70 end
71 local args = table.pack(...)
72 local nb = args.n
73 for k = 1, nb do aux(args[k]); if k<nb then write "\t" end end
74 end
75
76 ------------------------------------------------------------------------------
77 -- A more precise print (on stdout)
78 ------------------------------------------------------------------------------
79 function p (...)
80 local out = {}
81 local function write(s)
82 table.insert(out, s)
83 end
84 vprint(write, 3, ...)
85 print(table.concat(out))
86 end
87
88 ------------------------------------------------------------------------------
89 -- Build up and return a string instead of printing in some channel.
90 ------------------------------------------------------------------------------
91 function siprint(indent, ...)
92 local ins, acc = table.insert, { }
93 vprint(function(x) return ins(acc, x) end, indent, ...)
94 return table.concat(acc)
95 end
96
97 function sprint(...)
98 return siprint(false, ...)
99 end
Something went wrong with that request. Please try again.