1 -- primitives for saving to file and loading from file
2 function file_exists(filename
)
3 local infile
= App
.open_for_reading(filename
)
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
18 function load_from_file(infile
)
21 local infile_next_line
= infile
:lines() -- works with both Lua files and LÖVE Files (https://www.love2d.org/wiki/File)
23 local line
= infile_next_line()
24 if line
== nil then break end
25 table.insert(result
, {data
=line
})
29 table.insert(result
, {data
=''})
34 function save_to_disk(State
)
35 local outfile
= App
.open_for_writing(State
.filename
)
37 error('failed to write to "'..State
.filename
..'"')
39 for _
,line
in ipairs(State
.lines
) do
40 outfile
:write(line
.data
)
47 function load_array(a
)
49 local next_line
= ipairs(a
)
50 local i
,line
,drawing
= 0, ''
52 i
,line
= next_line(a
, i
)
53 if i
== nil then break end
54 table.insert(result
, {data
=line
})
57 table.insert(result
, {data
=''})
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
== '\\'
72 error('What OS is this? LÖVE reports that the path separator is "'..os_path_separator
..'"')
76 function is_relative_path(path
)
77 return not is_absolute_path(path
)