-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxmlc_reader.lua
More file actions
115 lines (91 loc) · 2.77 KB
/
xmlc_reader.lua
File metadata and controls
115 lines (91 loc) · 2.77 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
local ffi = require "ffi"
require 'common'
-- =========================== READER ============================
if not pcall(ffi.new, 'struct hws_compressed_xml_t') then
ffi.cdef[[
typedef unsigned char BYTE;
typedef unsigned long DWORD;
typedef long LONG;
struct hws_compressed_xml_t
{
BYTE nIdx;
BYTE nRail;
BYTE nChannel;
BYTE nType;
DWORD nCoord;
LONG nValue;
};
]]
end
local xmlc_reader = {}
local xmlc_reader_mt = {__index=xmlc_reader}
function xmlc_reader.new()
return setmetatable({}, xmlc_reader_mt)
end
function xmlc_reader:open(file_name)
local idx2names = {}
self.file = assert(io.open(file_name, 'rb'))
self.values = {}
if not self.check_header(self.file:read(4)) then
error('wrong file header')
end
local xmlc_item = ffi.new('struct hws_compressed_xml_t')
local xmlc_item_size = ffi.sizeof(xmlc_item)
while true do
local s = self.file:read(xmlc_item_size)
if not s then break end
ffi.copy(xmlc_item, s)
--print(xmlc_item.nIdx, xmlc_item.nRail, xmlc_item.nChannel, xmlc_item.nType, xmlc_item.nCoord, xmlc_item.nValue)
if xmlc_item.nType == 0 then -- HWS_CXML_INDEX_NAME
local name = self.file:read(xmlc_item.nValue)
assert(name)
if idx2names[xmlc_item.nIdx] then
--far.Message(string.format('%s %s', idx2names[xmlc_item.nIdx], name))
end
assert(not idx2names[xmlc_item.nIdx] or idx2names[xmlc_item.nIdx] == name)
idx2names[xmlc_item.nIdx] = name
elseif xmlc_item.nType == 2 then -- HWS_CXML_INDEXED_VALUE
local rec_item = {
name = idx2names[xmlc_item.nIdx],
rail = xmlc_item.nRail,
channel = xmlc_item.nChannel,
coord = xmlc_item.nCoord,
value = xmlc_item.nValue,
}
self.values[#self.values + 1] = rec_item
else
error('unknown item type: '..tostring(xmlc_item.nType))
end
end
return self
end
function xmlc_reader:close()
if self.file then
self.file:close()
self.file = nil
end
self.values = {}
end
function xmlc_reader.check_header(buffer)
return buffer:sub(1, 4) == 'XMLc'
end
--function xmlc_reader:Export(key, file_name)
-- local values = self.values[key]
-- if not values then return nil end
-- if not file_name then file_name = far.MkTemp() end
-- local title = string.format('%s rail=%d channel=%d', key[1], key[2], key[3])
-- local file = io.open(file_name, "w+")
-- -- file:write("\239\187\191") -- UTF-8 BOM
-- file:write(title .. '\n')
-- file:write(title:gsub('.', '=') .. '\n')
-- file:write(' N | coord | value\n')
-- file:write('-------+------------+------------\n')
-- for i, coord_value in ipairs(values) do
-- local l = string.format('%6d | %10d | %10d', i, coord_value[1], coord_value[2])
-- --far.Message(l)
-- file:write(l .. '\n')
-- end
-- file:close()
-- return file_name
--end
return xmlc_reader