]>
Commit | Line | Data |
---|---|---|
5b1311fb MHS |
1 | --[[\r |
2 | This may be moved to a separate library at some point (Holiman)\r | |
3 | --]]\r | |
4 | local Utils = \r | |
5 | {\r | |
6 | -- Asks the user for Yes or No\r | |
7 | confirm = function(message, ...)\r | |
8 | local answer\r | |
9 | message = message .. " [y/n] ?"\r | |
10 | repeat\r | |
11 | io.write(message)\r | |
12 | io.flush()\r | |
13 | answer=io.read()\r | |
14 | if answer == 'Y' or answer == "y" then\r | |
15 | return true\r | |
16 | elseif answer == 'N' or answer == 'n' then \r | |
17 | return false\r | |
18 | end\r | |
19 | until false\r | |
20 | end,\r | |
21 | ---\r | |
22 | -- Asks the user for input\r | |
23 | input = function (message , default)\r | |
24 | local answer\r | |
25 | if default ~= nil then\r | |
26 | message = message .. " (default: ".. default.. " )"\r | |
27 | end\r | |
28 | message = message .." \n > "\r | |
29 | io.write(message)\r | |
30 | io.flush()\r | |
31 | answer=io.read()\r | |
32 | if answer == '' then answer = default end\r | |
33 | \r | |
34 | return answer\r | |
35 | end,\r | |
36 | --\r | |
37 | -- Converts DECIMAL to HEX\r | |
38 | ConvertDec2Hex = function(IN)\r | |
39 | local B,K,OUT,I,D=16,"0123456789ABCDEF","",0\r | |
40 | while IN>0 do\r | |
41 | I=I+1\r | |
42 | IN,D=math.floor(IN/B),math.mod(IN,B)+1\r | |
43 | OUT=string.sub(K,D,D)..OUT\r | |
44 | end\r | |
45 | return OUT\r | |
46 | end,\r | |
47 | ---\r | |
48 | -- Convert Byte array to string of hex\r | |
cd5767d4 | 49 | ConvertBytes2HexString = function(bytes)\r |
50 | if #bytes == 0 then\r | |
51 | return ''\r | |
52 | end\r | |
53 | local s={}\r | |
5b1311fb | 54 | for i = 1, #(bytes) do\r |
9b989c45 | 55 | s[i] = string.format("%02X",bytes[i]) \r |
5b1311fb MHS |
56 | end\r |
57 | return table.concat(s)\r | |
58 | end, \r | |
cd5767d4 | 59 | -- Convert byte array to string with ascii\r |
60 | ConvertBytesToAsciiString = function(bytes)\r | |
61 | if #bytes == 0 then\r | |
62 | return ''\r | |
63 | end\r | |
64 | local s={}\r | |
65 | for i = 1, #(bytes) do\r | |
66 | s[i] = string.char(bytes[i]) \r | |
67 | end\r | |
68 | return table.concat(s) \r | |
69 | end, \r | |
70 | ConvertHexStringToBytes = function(s)\r | |
9b989c45 | 71 | local t={}\r |
cd5767d4 | 72 | if s == nil then return t end\r |
73 | if #s == 0 then return t end\r | |
9b989c45 | 74 | for k in s:gmatch"(%x%x)" do\r |
75 | table.insert(t,tonumber(k,16))\r | |
76 | end\r | |
77 | return t\r | |
78 | end,\r | |
cd5767d4 | 79 | ConvertAsciiStringToBytes = function(s)\r |
80 | local t={}\r | |
81 | if s == nil then return t end\r | |
82 | if #s == 0 then return t end\r | |
83 | \r | |
84 | for k in s:gmatch"(.)" do\r | |
85 | table.insert(t, string.byte(k))\r | |
86 | end\r | |
87 | return t\r | |
88 | end,\r | |
9b989c45 | 89 | \r |
90 | -- function convertStringToBytes(str)\r | |
91 | -- local bytes = {}\r | |
92 | -- local strLength = string.len(str)\r | |
93 | -- for i=1,strLength do\r | |
94 | -- table.insert(bytes, string.byte(str, i))\r | |
95 | -- end\r | |
96 | \r | |
97 | -- return bytes\r | |
98 | -- end\r | |
99 | \r | |
100 | -- function convertBytesToString(bytes)\r | |
101 | -- local bytesLength = table.getn(bytes)\r | |
102 | -- local str = ""\r | |
103 | -- for i=1,bytesLength do\r | |
104 | -- str = str .. string.char(bytes[i])\r | |
105 | -- end\r | |
106 | \r | |
107 | -- return str\r | |
108 | -- end\r | |
109 | \r | |
110 | -- function convertHexStringToBytes(str)\r | |
111 | -- local bytes = {}\r | |
112 | -- local strLength = string.len(str)\r | |
113 | -- for k=2,strLength,2 do\r | |
114 | -- local hexString = "0x" .. string.sub(str, (k - 1), k)\r | |
115 | -- table.insert(bytes, hex.to_dec(hexString))\r | |
116 | -- end\r | |
117 | \r | |
118 | -- return bytes\r | |
119 | -- end\r | |
120 | \r | |
121 | -- function convertBytesToHexString(bytes)\r | |
122 | -- local str = ""\r | |
123 | -- local bytesLength = table.getn(bytes)\r | |
124 | -- for i=1,bytesLength do\r | |
125 | -- local hexString = string.sub(hex.to_hex(bytes[i]), 3)\r | |
126 | -- if string.len(hexString) == 1 then\r | |
127 | -- hexString = "0" .. hexString\r | |
128 | -- end\r | |
129 | -- str = str .. hexString\r | |
130 | -- end\r | |
131 | \r | |
132 | -- return str\r | |
133 | -- end\r | |
134 | \r | |
5b1311fb MHS |
135 | }\r |
136 | return Utils |