comment out debug prints while fixing merge conflicts
[view.love.git] / file.lua
blobf7f832b786f837cf1f7933381507d087cc35942a
1 -- primitives for saving to file and loading from file
2 function file_exists(filename)
3 local infile = App.open_for_reading(filename)
4 if infile then
5 infile:close()
6 return true
7 else
8 return false
9 end
10 end
12 function load_from_disk(State)
13 local infile = App.open_for_reading(State.filename)
14 State.lines = load_from_file(infile)
15 if infile then infile:close() end
16 end
18 function load_from_file(infile)
19 local result = {}
20 if infile then
21 local infile_next_line = infile:lines() -- works with both Lua files and LÖVE Files (https://www.love2d.org/wiki/File)
22 while true do
23 local line = infile_next_line()
24 if line == nil then break end
25 table.insert(result, {data=line})
26 end
27 end
28 if #result == 0 then
29 table.insert(result, {data=''})
30 end
31 return result
32 end
34 function save_to_disk(State)
35 local outfile = App.open_for_writing(State.filename)
36 if not outfile then
37 error('failed to write to "'..State.filename..'"')
38 end
39 for _,line in ipairs(State.lines) do
40 outfile:write(line.data)
41 outfile:write('\n')
42 end
43 outfile:close()
44 end
46 -- for tests
47 function load_array(a)
48 local result = {}
49 local next_line = ipairs(a)
50 local i,line,drawing = 0, ''
51 while true do
52 i,line = next_line(a, i)
53 if i == nil then break end
54 table.insert(result, {data=line})
55 end
56 if #result == 0 then
57 table.insert(result, {data=''})
58 end
59 return result
60 end
62 function is_absolute_path(path)
63 local os_path_separator = package.config:sub(1,1)
64 if os_path_separator == '/' then
65 -- POSIX systems permit backslashes in filenames
66 return path:sub(1,1) == '/'
67 elseif os_path_separator == '\\' then
68 if path:sub(2,2) == ':' then return true end -- DOS drive letter followed by volume separator
69 local f = path:sub(1,1)
70 return f == '/' or f == '\\'
71 else
72 error('What OS is this? LÖVE reports that the path separator is "'..os_path_separator..'"')
73 end
74 end
76 function is_relative_path(path)
77 return not is_absolute_path(path)
78 end