2022-05-18 05:18:17 +01:00
|
|
|
-- primitives for saving to file and loading from file
|
2022-08-23 23:04:30 +01:00
|
|
|
function file_exists(filename)
|
|
|
|
local infile = App.open_for_reading(filename)
|
|
|
|
if infile then
|
|
|
|
infile:close()
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-26 03:56:39 +01:00
|
|
|
function load_from_disk(State)
|
|
|
|
local infile = App.open_for_reading(State.filename)
|
|
|
|
State.lines = load_from_file(infile)
|
2022-05-18 05:18:17 +01:00
|
|
|
if infile then infile:close() end
|
|
|
|
end
|
|
|
|
|
|
|
|
function load_from_file(infile)
|
|
|
|
local result = {}
|
|
|
|
if infile then
|
|
|
|
local infile_next_line = infile:lines() -- works with both Lua files and LÖVE Files (https://www.love2d.org/wiki/File)
|
|
|
|
while true do
|
|
|
|
local line = infile_next_line()
|
|
|
|
if line == nil then break end
|
2022-08-15 18:31:56 +01:00
|
|
|
table.insert(result, {data=line})
|
2022-05-18 05:18:17 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
if #result == 0 then
|
2022-08-15 18:31:56 +01:00
|
|
|
table.insert(result, {data=''})
|
2022-05-18 05:18:17 +01:00
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2022-07-26 03:56:39 +01:00
|
|
|
function save_to_disk(State)
|
|
|
|
local outfile = App.open_for_writing(State.filename)
|
2023-08-31 07:52:25 +01:00
|
|
|
if not outfile then
|
2022-07-26 03:56:39 +01:00
|
|
|
error('failed to write to "'..State.filename..'"')
|
2022-07-05 22:02:52 +01:00
|
|
|
end
|
2022-07-26 03:56:39 +01:00
|
|
|
for _,line in ipairs(State.lines) do
|
2022-09-05 20:47:27 +01:00
|
|
|
outfile:write(line.data)
|
|
|
|
outfile:write('\n')
|
2022-05-18 05:18:17 +01:00
|
|
|
end
|
|
|
|
outfile:close()
|
2024-03-17 17:54:26 +00:00
|
|
|
State.next_save = nil
|
2022-05-18 05:18:17 +01:00
|
|
|
end
|
|
|
|
|
2022-05-23 07:17:06 +01:00
|
|
|
-- for tests
|
|
|
|
function load_array(a)
|
|
|
|
local result = {}
|
|
|
|
local next_line = ipairs(a)
|
2022-05-23 16:13:58 +01:00
|
|
|
local i,line,drawing = 0, ''
|
2022-05-23 07:17:06 +01:00
|
|
|
while true do
|
|
|
|
i,line = next_line(a, i)
|
|
|
|
if i == nil then break end
|
2022-08-15 18:31:56 +01:00
|
|
|
table.insert(result, {data=line})
|
2022-05-23 07:17:06 +01:00
|
|
|
end
|
|
|
|
if #result == 0 then
|
2022-08-15 18:31:56 +01:00
|
|
|
table.insert(result, {data=''})
|
2022-05-23 07:17:06 +01:00
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2022-09-11 16:31:05 +01:00
|
|
|
function is_absolute_path(path)
|
|
|
|
local os_path_separator = package.config:sub(1,1)
|
|
|
|
if os_path_separator == '/' then
|
|
|
|
-- POSIX systems permit backslashes in filenames
|
|
|
|
return path:sub(1,1) == '/'
|
|
|
|
elseif os_path_separator == '\\' then
|
2022-09-26 23:17:57 +01:00
|
|
|
if path:sub(2,2) == ':' then return true end -- DOS drive letter followed by volume separator
|
2022-09-11 16:31:05 +01:00
|
|
|
local f = path:sub(1,1)
|
|
|
|
return f == '/' or f == '\\'
|
|
|
|
else
|
|
|
|
error('What OS is this? LÖVE reports that the path separator is "'..os_path_separator..'"')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function is_relative_path(path)
|
|
|
|
return not is_absolute_path(path)
|
|
|
|
end
|