diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2022-09-03 15:42:51 -0700 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2022-09-03 15:42:51 -0700 |
commit | 60fe0866dd2926270ab288758dab5f05c96efaab (patch) | |
tree | 498a2fce881ac3c50b80242056537239a606a22e /source_file.lua | |
parent | f4a45e4ad9e0f0bb464cb2a453134ac32c33612d (diff) | |
parent | 84e2eafb0bde7ada2f460d8de9360e71ba9dd634 (diff) | |
download | view.love-60fe0866dd2926270ab288758dab5f05c96efaab.tar.gz |
Merge text.love
Diffstat (limited to 'source_file.lua')
-rw-r--r-- | source_file.lua | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/source_file.lua b/source_file.lua new file mode 100644 index 0000000..978e949 --- /dev/null +++ b/source_file.lua @@ -0,0 +1,89 @@ +-- primitives for saving to file and loading from file + +Fold = '\x1e' -- ASCII RS (record separator) + +function file_exists(filename) + local infile = App.open_for_reading(filename) + if infile then + infile:close() + return true + else + return false + end +end + +function load_from_disk(State) + local infile = App.open_for_reading(State.filename) + State.lines = load_from_file(infile) + 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 + local line_info = {} + if line:find(Fold) then + _, _, line_info.data, line_info.dataB = line:find('([^'..Fold..']*)'..Fold..'([^'..Fold..']*)') + else + line_info.data = line + end + table.insert(result, line_info) + end + end + if #result == 0 then + table.insert(result, {data=''}) + end + return result +end + +function save_to_disk(State) + local outfile = App.open_for_writing(State.filename) + if outfile == nil then + error('failed to write to "'..State.filename..'"') + end + for _,line in ipairs(State.lines) do + outfile:write(line.data) + if line.dataB and #line.dataB > 0 then + outfile:write(Fold) + outfile:write(line.dataB) + end + outfile:write('\n') + end + outfile:close() +end + +function file_exists(filename) + local infile = App.open_for_reading(filename) + if infile then + infile:close() + return true + else + return false + end +end + +-- for tests +function load_array(a) + local result = {} + local next_line = ipairs(a) + local i,line,drawing = 0, '' + while true do + i,line = next_line(a, i) + if i == nil then break end + local line_info = {} + if line:find(Fold) then + _, _, line_info.data, line_info.dataB = line:find('([^'..Fold..']*)'..Fold..'([^'..Fold..']*)') + else + line_info.data = line + end + table.insert(result, line_info) + end + if #result == 0 then + table.insert(result, {data=''}) + end + return result +end |