-
Notifications
You must be signed in to change notification settings - Fork 3
/
utility.lua
135 lines (123 loc) · 3.99 KB
/
utility.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
local json = require( "json" )
--local socket = require("socket")
--local http = require("socket.http")
--local ltn12 = require("ltn12")
M = {}
M.isSimulator = ("simulator" == system.getInfo("environment"))
function M.print_r ( t )
local print_r_cache={}
local function sub_print_r(t,indent)
if (print_r_cache[tostring(t)]) then
print(indent.."*"..tostring(t))
else
print_r_cache[tostring(t)]=true
if (type(t)=="table") then
for pos,val in pairs(t) do
if (type(val)=="table") then
print(indent.."["..pos.."] => "..tostring(t).." {")
sub_print_r(val,indent..string.rep(" ",string.len(pos)+8))
print(indent..string.rep(" ",string.len(pos)+6).."}")
elseif (type(val)=="string") then
print(indent.."["..pos..'] => "'..val..'"')
else
print(indent.."["..pos.."] => "..tostring(val))
end
end
else
print(indent..tostring(t))
end
end
end
if (type(t)=="table") then
print(tostring(t).." {")
sub_print_r(t," ")
print("}")
else
sub_print_r(t," ")
end
print()
end
function M.testNetworkConnection()
print("testing connection")
if http.request( "http://redlertech.com/jackask/index.php" ) == nil then
print("cant connect to google")
return false
end
print("got a connection")
return true
end
function M.saveTable(t, filename)
local path = system.pathForFile( filename, system.DocumentsDirectory)
local file = io.open(path, "w")
--print("in saveTable. I think my table is....")
--M.print_r(t)
if file then
local contents = json.encode(t)
--print("json data is")
--print("*" .. contents .. "*")
file:write( contents )
io.close( file )
return true
else
return false
end
end
function M.loadTable(filename)
local path = system.pathForFile( filename, system.DocumentsDirectory)
local contents = ""
local myTable = {}
local file = io.open( path, "r" )
if file then
--print("trying to read ", filename)
-- read all contents of file into a string
local contents = file:read( "*a" )
myTable = json.decode(contents);
io.close( file )
--print("Loaded file")
return myTable
end
print(filename, "file not found")
return nil
end
function M.ignoreTouch(event)
print("throwing away background touch")
return true
end
function M.urlencode(str)
if (str) then
str = string.gsub (str, "\n", "\r\n")
str = string.gsub (str, "([^%w ])",
function (c) return string.format ("%%%02X", string.byte(c)) end)
str = string.gsub (str, " ", "+")
end
return str
end
function M.makeTimeStamp(dateString)
local pattern = "(%d+)%-(%d+)%-(%d+)T(%d+):(%d+):(%d+)([%+%-])(%d+)%:(%d+)"
local xyear, xmonth, xday, xhour, xminute,
xseconds, xoffset, xoffsethour, xoffsetmin = dateString:match(pattern)
local convertedTimestamp = os.time({year = xyear, month = xmonth,
day = xday, hour = xhour, min = xminute, sec = xseconds})
local offset = xoffsethour * 60 + xoffsetmin
if xoffset == "-" then offset = offset * -1 end
return convertedTimestamp + offset
end
function string:trim()
return (self:gsub("^%s*(.-)%s*$", "%1"))
end
function string:split( inSplitPattern, outResults )
if not outResults then
outResults = { }
end
local theStart = 1
local theSplitStart, theSplitEnd = string.find( self, inSplitPattern,
theStart )
while theSplitStart do
table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
theStart = theSplitEnd + 1
theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
end
table.insert( outResults, string.sub( self, theStart ) )
return outResults
end
return M