Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
6581 | leency | 1 | -- Persistence library: |
2 | -- Memorize data for current function |
||
3 | -- memory.save(tab) and tab=memory.load() |
||
4 | -- |
||
5 | -- The data will be stored in file called |
||
6 | -- |
||
7 | -- in the lua directory |
||
8 | -- |
||
9 | -- Example 1: |
||
10 | -- |
||
11 | -- -- Load initial values or set defaults |
||
12 | -- arg = memory.load({picX=320,picY=200,scale=0}) |
||
13 | -- -- Run an inputbox |
||
14 | -- OK,arg.picX,arg.picY,arg.scale = inputbox("Image Size")", |
||
15 | -- "Width", arg.picX, 1,2048,0, |
||
16 | -- "Height", arg.picY, 1,2048,0, |
||
17 | -- "Scale", arg.scale, 0,1,0); |
||
18 | -- if OK == true then |
||
19 | -- -- Save the selected values |
||
20 | -- memory.save(arg) |
||
21 | -- end |
||
22 | |||
23 | -- Example 2: |
||
24 | -- |
||
25 | -- -- Load initial values or set defaults |
||
26 | -- arg = memory.load({x=320,y=200,scale=0}) |
||
27 | -- picX=arg.x |
||
28 | -- picY=arg.y |
||
29 | -- scale=arg.scale |
||
30 | -- -- Run an inputbox |
||
31 | -- OK,picX,picY,scale = inputbox("Image Size")", |
||
32 | -- "Width", picX, 1,2048,0, |
||
33 | -- "Height", picY, 1,2048,0, |
||
34 | -- "Scale", scale, 0,1,0); |
||
35 | -- if OK == true then |
||
36 | -- -- Save the selected values |
||
37 | -- memory.save({x=picX,y=picY,scale=scale}) |
||
38 | -- end |
||
39 | |||
40 | |||
41 | memory = |
||
42 | { |
||
43 | serialize = function(o) |
||
44 | if type(o) == "number" then |
||
45 | return tostring(o) |
||
46 | elseif type(o) == "string" then |
||
47 | return string.format("%q", o) |
||
48 | --elseif type(o) == "table" then |
||
49 | -- io.write("{\n") |
||
50 | -- for k,v in pairs(o) do |
||
51 | -- io.write(" ", k, " = ") |
||
52 | -- memory.serialize(v) |
||
53 | -- io.write(",\n") |
||
54 | -- end |
||
55 | -- io.write("}\n") |
||
56 | else |
||
57 | error("cannot serialize a " .. type(o)) |
||
58 | end |
||
59 | end; |
||
60 | |||
61 | -- Return a string identifying the calling function. |
||
62 | -- Pass 1 for parent, 2 for grandparent etc. |
||
63 | callername = function(level) |
||
64 | local w |
||
65 | local last_slash |
||
66 | local info = debug.getinfo(level+1,"Sn") |
||
67 | local caller=tostring(info.name) |
||
68 | -- Function name if possible |
||
69 | if (caller~="nil") then |
||
70 | return caller |
||
71 | end |
||
72 | -- Otherwise, get file name, without extension |
||
73 | |||
74 | -- Get part after directory name |
||
75 | last_slash=0 |
||
76 | while true do |
||
77 | local pos = string.find(info.source, "/", last_slash+1) |
||
78 | if (pos==nil) then break end |
||
79 | last_slash=pos |
||
80 | end |
||
81 | while true do |
||
82 | local pos = string.find(info.source, "\\", last_slash+1) |
||
83 | if (pos==nil) then break end |
||
84 | last_slash=pos |
||
85 | end |
||
86 | |||
87 | caller=string.sub(info.source, last_slash+1) |
||
88 | |||
89 | -- Remove file extension |
||
90 | if (string.sub(caller,-4, -1)==".lua") then |
||
91 | caller=string.sub(caller, 1, -5) |
||
92 | end |
||
93 | return caller |
||
94 | end; |
||
95 | |||
96 | -- Memorize some parameters. |
||
97 | save = function(o) |
||
98 | local caller=memory.callername(2) |
||
99 | --for k, v in pairs(o) do |
||
100 | -- messagebox(tostring(k)) |
||
101 | -- messagebox(tostring(v)) |
||
102 | --end |
||
103 | local f, e = io.open(caller..".dat", "w"); |
||
104 | if (f ~= nil) then |
||
105 | f:write("Entry {\n") |
||
106 | for k, v in pairs(o) do |
||
107 | if (type(v)=="number") then |
||
108 | f:write(" "..k.."="..memory["serialize"](v)..",\n") |
||
109 | end |
||
110 | end |
||
111 | f:write("}\n") |
||
112 | f:close() |
||
113 | end |
||
114 | end; |
||
115 | |||
116 | |||
117 | -- Recover some saved parameters. |
||
118 | load = function(o) |
||
119 | local caller=memory.callername(2) |
||
120 | local i |
||
121 | |||
122 | function Entry (b) |
||
123 | -- Adds (or replaces) values in arg with those from b |
||
124 | for k, v in pairs(b) do |
||
125 | o[k]=v |
||
126 | end |
||
127 | end |
||
128 | local f = (loadfile(caller..".dat")) |
||
129 | if (f ~= nil) then |
||
130 | f() |
||
131 | end |
||
132 | |||
133 | return o |
||
134 | end; |
||
135 | |||
136 | } |
||
137 | |||
138 | return memory |