Debian Codegraph @

lua/include/file.lua

raw symbols SCIP
1 --[[
2 Copyright (C) 2014 HarpyWar ([email protected])
3
4 This file is a part of the PvPGN Project http://pvpgn.pro
5 Licensed under the same terms as Lua itself.
6 ]]--
7
8
9 -- Read file line by line
10 -- callback 2 is optional
11 -- You can process line with function callback1(line, callback2)
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 -- (callback) for "file_load" to load file with each line format like "key = value"
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 -- Save raw text "data" into a filename
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 -- Save file using callback
64 function file_save2(filename, callback)
65 local file = io.open(filename, "w")
66
67 callback(file)
68 file:close()
69 end
70 -- Check file for exist
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