-
Notifications
You must be signed in to change notification settings - Fork 2
/
io_ext.lua
78 lines (63 loc) · 1.93 KB
/
io_ext.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
local http = require("socket.http")
local ltn12 = require("ltn12")
local M = {}
-- From http://docs.coronalabs.com/api/type/File/write.html
local copyFile = function( srcName, srcPath, dstName, dstPath, overwrite )
local results = true -- assume no errors
-- Copy the source file to the destination file
local rfilePath = system.pathForFile( srcName, srcPath )
local wfilePath = system.pathForFile( dstName, dstPath )
local rfh = io.open( rfilePath, "rb" )
local wfh = io.open( wfilePath, "wb" )
if not wfh then
print( "writeFileName open error!" )
results = false -- error
else
-- Read the file from the Resource directory and write it to the destination directory
local data = rfh:read( "*a" )
if not data then
print( "read error!" )
results = false -- error
else
if not wfh:write( data ) then
print( "write error!" )
results = false -- error
end
end
end
-- Clean up our file handles
rfh:close()
wfh:close()
return results
end
M.copyFile = copyFile
-- Taken from http://stackoverflow.com/questions/4990990/lua-check-if-a-file-exists
local fileExists = function( filename )
local f = io.open( filename, "r" )
if f ~= nil then io.close( f ) return true else return false end
end
M.fileExists = fileExists
local json = require "json"
local parseJson = function( filename )
local file = io.open( filename, "r" )
if file then
local contents = file:read( "*a" )
result = json.decode( contents )
io.close( file )
return result
else
return {}
end
end
M.parseJson = parseJson
local parseRemoteJson = function( remoteUrl )
local tempFilename = system.pathForFile( "temp.json", system.TemporaryDirectory )
local tempFile = io.open( tempFilename, "w+b" )
http.request {
url = remoteUrl,
sink = ltn12.sink.file( tempFile )
}
return parseJson( tempFilename )
end
M.parseRemoteJson = parseRemoteJson
return M