]>
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 | |
49 | ConvertBytes2String = function(bytes)\r | |
9b989c45 | 50 | local s = {}\r |
5b1311fb | 51 | for i = 1, #(bytes) do\r |
9b989c45 | 52 | s[i] = string.format("%02X",bytes[i]) \r |
5b1311fb MHS |
53 | end\r |
54 | return table.concat(s)\r | |
55 | end, \r | |
9b989c45 | 56 | \r |
57 | ConvertStringToBytes = function(s)\r | |
58 | local t={}\r | |
59 | for k in s:gmatch"(%x%x)" do\r | |
60 | table.insert(t,tonumber(k,16))\r | |
61 | end\r | |
62 | return t\r | |
63 | end,\r | |
64 | \r | |
65 | -- function convertStringToBytes(str)\r | |
66 | -- local bytes = {}\r | |
67 | -- local strLength = string.len(str)\r | |
68 | -- for i=1,strLength do\r | |
69 | -- table.insert(bytes, string.byte(str, i))\r | |
70 | -- end\r | |
71 | \r | |
72 | -- return bytes\r | |
73 | -- end\r | |
74 | \r | |
75 | -- function convertBytesToString(bytes)\r | |
76 | -- local bytesLength = table.getn(bytes)\r | |
77 | -- local str = ""\r | |
78 | -- for i=1,bytesLength do\r | |
79 | -- str = str .. string.char(bytes[i])\r | |
80 | -- end\r | |
81 | \r | |
82 | -- return str\r | |
83 | -- end\r | |
84 | \r | |
85 | -- function convertHexStringToBytes(str)\r | |
86 | -- local bytes = {}\r | |
87 | -- local strLength = string.len(str)\r | |
88 | -- for k=2,strLength,2 do\r | |
89 | -- local hexString = "0x" .. string.sub(str, (k - 1), k)\r | |
90 | -- table.insert(bytes, hex.to_dec(hexString))\r | |
91 | -- end\r | |
92 | \r | |
93 | -- return bytes\r | |
94 | -- end\r | |
95 | \r | |
96 | -- function convertBytesToHexString(bytes)\r | |
97 | -- local str = ""\r | |
98 | -- local bytesLength = table.getn(bytes)\r | |
99 | -- for i=1,bytesLength do\r | |
100 | -- local hexString = string.sub(hex.to_hex(bytes[i]), 3)\r | |
101 | -- if string.len(hexString) == 1 then\r | |
102 | -- hexString = "0" .. hexString\r | |
103 | -- end\r | |
104 | -- str = str .. hexString\r | |
105 | -- end\r | |
106 | \r | |
107 | -- return str\r | |
108 | -- end\r | |
109 | \r | |
5b1311fb MHS |
110 | }\r |
111 | return Utils |