-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpreference.lua
More file actions
387 lines (264 loc) · 10.2 KB
/
preference.lua
File metadata and controls
387 lines (264 loc) · 10.2 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
--[[
Save_And_Load Module
Author: Satheesh
Version: 1.1
License: MIT
Web: http:www.timeplusq.com
--Change Log
1.1
Fixed a minor bug where a subtable with numerical key raises an error.
http://developer.coronalabs.com/code/save-data-files-tablesnumbersstringsboolean#comment-120973
FUNCTIONS
preference.save
preference.getValue
preference.getAllValues
preference.print
preference.printAll
USAGE AND EXAMPLES
preference = require "preference"
preference.save{a=1}
value = preference.getValue("a")
preference.save{b="1"}
value = preference.getValue("b")
preference.save{c=true}
value = preference.getValue("c")
preference.save{d = {1,"2",true}}
value = preference.getValue("d")
--]]
local preference = {}
--Localise
local system = system
local type = type
local tonumber = tonumber
local pairs = pairs
local print = print
local pairs = pairs
local io = io
--constants
local separator = "||__||"
local elementSeparator = "!!!"
local function saveFile( fileName, fileData )
--Path for file
local path = system.pathForFile( fileName, system.DocumentsDirectory )
local file = io.open( path, "w" )
if file then
file:write( fileData )
io.close( file )
end
end
local function loadFile( fileName )
local path = system.pathForFile( fileName, system.DocumentsDirectory )
local file = io.open( path, "r" )
if file then
local fileData = file:read( "*a" )
io.close( file )
return fileData
else
return false
end
end
local function formatValueForStoring(value,params)
params = params or {}
local elementIsInsideATable = params.elementIsInsideATable
local separator = elementIsInsideATable and elementSeparator or separator
local dataType = type(value)
local value2
if dataType == "number" then
value2 = value..separator.."n"
elseif dataType == "string" then
value2 = value..separator.."s"
elseif dataType == "boolean" then
value2 = (value and 1 or 0)..separator.."b"
elseif dataType == "table" then
value2 = convertTableToString(value)
else
value2 = dataType.."_cannot_be_stored "..separator.."s"
dataType = "string"
end
if elementIsInsideATable then
local index = params.index
value2 = index..separator..value2 --tab["1"]
if type(index) == "number" then
local endd = value2:sub(-1,-1)
local start = value2:sub(1,-2) --tab[1] if index is number append a n as penultimate character
local mid = "n"
value2 = start..mid..endd
end
end
return value2
end
function convertTableToString(tab)
local finalString = "<t>\n"
for i,v in pairs(tab) do
local value2 = formatValueForStoring(v,{elementIsInsideATable = true,index = i})
finalString = finalString..value2.."\n"
end
finalString = finalString.."<#t>"
return finalString
end
local function saveModule(fileName,value)
local allFiles = preference.__allFiles
local allFilesAsString = ""
--store value in global
preference.__allFiles[fileName] = value
--Store value in file
saveFile(fileName,formatValueForStoring(value))
--Store filename in preference file
for fileName,v in pairs(allFiles) do
allFilesAsString = allFilesAsString..fileName.."\n"
end
saveFile("preference.bin",allFilesAsString)
end
local function convertStringToTableElement(str)
local index,value
local separator = elementSeparator
if str:find("<t>") then
local s,e = str:find(separator)
index = str:sub(1,s-1)
value = {}
elseif str:find("<#t>") then
value = "eot"
elseif str:find("<#tn>") then
value = "eot"
else
local dataType = str:sub(-1,-1)
local s,e = str:find(separator)
index = str:sub(1,s-1)
local s2,e2 = str:find(separator,e+1)
value = str:sub(e+1,s2-1)
if dataType == "n" then
value = tonumber(value)
elseif dataType == "s" then
elseif dataType == "b" then
value = (tonumber(value) == 1)
end
end
return index,value
end
local function convertStringToTable(str)
local finalTable = {}
local start = 5
local endd = -5
local tableData = str:sub(start,endd)
local start = 1
local tableHierarchy = 1
local tableStart,tableEnd
local tableIndex
repeat
local pos = tableData:find("\n",start);
if pos then
local elementAsString = tableData:sub(start,pos-1)
local index,value = convertStringToTableElement(elementAsString)
if tableHierarchy == 1 then
if elementAsString:sub(-2,-2)=="n" then --Table index is a number
index = tonumber(index)
end
finalTable[index] = value
end
if type(value) == "table" then
tableHierarchy = tableHierarchy + 1
if tableHierarchy == 2 then
tableStart = tableData:find("<t>",start)
tableIndex = index
end
end
if value == "eot" then
tableHierarchy = tableHierarchy - 1
if tableHierarchy == 1 then
tableEnd = pos-1
local subTableString = tableData:sub(tableStart,tableEnd )
local subTable = convertStringToTable(subTableString)
--index is a number and value is subtable
if elementAsString:find("<#tn>") then
finalTable[tableIndex] = nil
tableIndex = tonumber(tableIndex)
end
finalTable[tableIndex] = subTable
end
end
start = pos+1
end
until(not pos)
return finalTable
end
local function parse(fileData)
local finalString
if fileData:sub(1,3)=="<t>" then
finalString = convertStringToTable(fileData)
-- dump(finalString)
else
local dataType = fileData:sub(-1,-1)
local index = fileData:find(separator)
local value = fileData:sub(1,index-1)
if dataType == "n" then
finalString = tonumber(value)
elseif dataType == "s" then
finalString = value
elseif dataType == "b" then
finalString = (tonumber(value) == 1)
end
end
return finalString
end
local function loadModule(fileName)
local finalString = parse(loadFile(fileName))
return finalString
end
local function initialize()
preference.__allFiles = {}
local allFiles = preference.__allFiles
local allOptions = loadFile("preference.bin")
if not allOptions then
saveFile("preference.bin","")
allOptions = ""
end
local start = 1
repeat
local pos = allOptions:find("\n",start);
if pos then
local option = allOptions:sub(start,pos-1)
allFiles[option] = true
start = pos+1
end
until(not pos)
end
function preference.save(params)
for fileName,value in pairs(params) do
saveModule(fileName,value)
end
end
function preference.getValue(fileName)
local value
local allOptions = preference.__allFiles
if allOptions[fileName] == true then
value = loadModule(fileName)
allOptions[fileName] = value
else
value = allOptions[fileName]
end
return value
end
function preference.getAllValues()
local allOptions = preference.__allFiles
local finalTable = {}
for fileName,value in pairs(allOptions) do
if value==true then
value = loadModule(fileName)
end
finalTable[fileName] = value
end
return finalTable
end
function preference.print(fileName)
local value = loadModule(fileName)
print(value)
end
function preference.printAll(fileName)
local allOptions = preference.__allFiles
for fileName,v in pairs(allOptions) do
local value = loadModule(fileName)
print(fileName,value)
end
end
initialize()
return preference