| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
function file_load(filename, callback1, callback2) |
| 13 |
local file = io.open(filename, "r") |
| 14 |
if file then |
| 15 |
for line in file:lines() do |
| 16 |
if callback2 then |
| 17 |
callback1(line, callback2) |
| 18 |
else |
| 19 |
callback1(line) |
| 20 |
end |
| 21 |
end |
| 22 |
file.close(file) |
| 23 |
TRACE("File read " .. filename) |
| 24 |
else |
| 25 |
ERROR("Could not open file " .. filename) |
| 26 |
return false |
| 27 |
end |
| 28 |
return true |
| 29 |
end |
| 30 |
|
| 31 |
|
| 32 |
function file_load_dictionary_callback(line, callback) |
| 33 |
if string:empty(line) then return 0 end |
| 34 |
|
| 35 |
local idx = 0 |
| 36 |
local a, b |
| 37 |
for v in string.split(line, "=") do |
| 38 |
if idx == 0 then |
| 39 |
a = string:trim(v) |
| 40 |
else |
| 41 |
b = string:trim(v) |
| 42 |
end |
| 43 |
idx = idx + 1 |
| 44 |
end |
| 45 |
if not string:empty(b) and not string:empty(a) then |
| 46 |
callback(a, b) |
| 47 |
end |
| 48 |
end |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
function file_save(data, filename) |
| 57 |
local file = io.open(filename, "w") |
| 58 |
|
| 59 |
file:write(data) |
| 60 |
file:close() |
| 61 |
end |
| 62 |
|
| 63 |
|
| 64 |
function file_save2(filename, callback) |
| 65 |
local file = io.open(filename, "w") |
| 66 |
|
| 67 |
callback(file) |
| 68 |
file:close() |
| 69 |
end |
| 70 |
|
| 71 |
function file_exists(filename) |
| 72 |
local f=io.open(filename, "r") |
| 73 |
if f~=nil then io.close(f) return true else return false end |
| 74 |
end |
| 75 |
|
| 76 |
|