-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtility.lua
More file actions
162 lines (138 loc) · 4.2 KB
/
Utility.lua
File metadata and controls
162 lines (138 loc) · 4.2 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
-----------------------------------------------------------------------------
--
-----------------------------------------------------------------------------
local M = { }
---[[ Requirements
local json = require( "json" )
local socket = require( "socket" )
local socketHttp = require( "socket.http" )
--]]
---[[ Public Functions
-----------------------------------------------------------------------------
-- Converts (decodes) json data to a lua table.
-- Depends on 'json' library.
-- Static function.
--
-- @param jsonData [string] json data to convert (decode.)
-- @return [lua table] converted (decoded) lua table from json data.
-----------------------------------------------------------------------------
function M.ConvertJsonToLuaTable ( jsonData )
local luaTableData = json.decode( jsonData )
return luaTableData
end
-----------------------------------------------------------------------------
-- Converts (encodes) lua table to json data.
-- Depends on 'json' library.
-- Static function.
--
-- @param luaTableData [lua table] lua table variable to convert (encode.)
-- @return [string] converted (encoded) json data from lua table.
-----------------------------------------------------------------------------
function M.ConvertLuaTableToJson ( luaTableData )
local jsonData = json.encode( luaTableData )
return jsonData
end
function M.JsonLoad ( filePath, fileName )
local firstChar = string.sub( filePath, 1, 1 )
if ( firstChar == "~" )
then
filePath = os.getenv("HOME") .. string.sub( filePath, 2 )
end
local fullFileName = ( filePath or "" ) .. ( fileName or "" )
local luaTable = nil
local jsonFile = M.ReadDataFile( filePath, fileName )
if ( jsonFile )
then
luaTable = M.ConvertJsonToLuaTable( jsonFile )
end
return luaTable
end
function M.ReadDataFile ( filePath, fileName )
local firstChar = string.sub( filePath, 1, 1 )
if ( firstChar == "~" )
then
filePath = os.getenv("HOME") .. string.sub( filePath, 2 )
end
local fullFileName = ( filePath or "" ) .. ( fileName or "" )
local dataFile = io.open( fullFileName, "rb" )
local dataContent = nil
if ( dataFile )
then
dataContent = dataFile:read( "*all" )
dataFile:close( )
end
return dataContent
end
function M.WriteDataFile ( filePath, fileName, contentToWrite )
local fullFileName = ( filePath or "" ) .. ( fileName or "" )
if ( contentToWrite )
then
local dataFile = io.open( fullFileName, "w" )
dataFile:write( contentToWrite )
dataFile:close( )
end
end
function M.DownloadFile( url, filePath, fileName )
local isSuccessful, content = pcall( socketHttp.request, url )
if ( isSuccessful )
then
isSuccessful = pcall( M.WriteDataFile, filePath, fileName, content )
end
return isSuccessful
end
function M.GetValue ( allData, field )
local value = allData
if ( allData and field )
then
for w in string.gmatch(field, "[%w_]+")
do
value = value[w]
if ( not value )
then -- there is no such filed, break loop and return nil
break
end
end
end
return value
end
function M.Sleep ( seconds )
--socket.select( nil, nil, seconds )
socket.sleep( seconds )
end
function M.SetBaseTable ( childTable, baseTable )
local inheritanceMt = { }
inheritanceMt.__index = baseTable
inheritanceMt.__newindex = function (t, k, v)
if ( baseTable[ k ] ~= nil and type( v ) ~= "function" ) then
baseTable[ k ] = v
else
rawset( t, k, v )
end
end
setmetatable( childTable, inheritanceMt )
end
function
M.NumberOfItems ( table )
local count = 0
if table ~= nil
then
for k,v in pairs(table) do count = count + 1 end
end
return count
end
function M.IsEmpty( table )
local isEmpty = true
if table ~= nil
then
for k, v in pairs( table )
do
isEmpty = false
break
end
end
return isEmpty
end
--]]
---[[ Finalization
return M
--]]