kaitai 实例值三元:不能组合输出类型
kaitai instance value ternary: can't combine output types
我为两种非常相似的 Digilent 日志文件格式创建了一个 Kaitai 结构 .ksy
。第二种格式 (openlogger
) 是第一种格式 (openscope
) 的扩展,在结构中增加了两个字段。示波器基本上是一个单通道记录器;额外的记录器字段描述了活动通道的数量(a u1
,最多 8 个)和通道到样本顺序映射(a u1 x 8
)。
我试图通过合成 num_channels
和 channel_map
的始终存在的字段来协调两种格式的界面;这对 num_channels
实例来说效果很好。但是我不知道如何为通道映射创建合适的值,下面的 .ksy
报错:
/types/body/types/header/instances/channel_order/value: can't combine output types: ArrayType(Int1Type(false)) vs CalcBytesType
我不知道如何将 if_false
部分 ([0]
) 表示为 ArrayType。
有没有更好的方法来解决这个问题?
meta:
id: dlog
file-extension: dlog
seq:
- id: endianness
type: u1
doc: 0 - little endian 1 - big endian
- id: body
type: body
types:
body:
meta:
endian:
switch-on: _root.endianness
cases:
0: le
1: be
seq:
- id: header
type: header
instances:
data:
pos: header.start_of_data
type: data
types:
header:
seq:
- id: sample_size
type: u1
- id: header_size
type: u2
- id: start_of_data
type: u2
- id: dlog_format
type: u2
enum: dlog_formats
- id: dlog_version
type: u4
- id: voltage_units
type: u8
- id: stop_reason
type: u4
enum: stop_reasons
#...
- id: num_openlogger_channels
type: u4
if: dlog_format == dlog_formats::openlogger
doc: number of channels per sample
- id: openlogger_channel_map
type: u1
repeat: expr
repeat-expr: 8
if: dlog_format == dlog_formats::openlogger
doc: channel order
instances:
num_channels:
value: 'dlog_format == dlog_formats::openlogger ? num_openlogger_channels : 1'
channel_map:
value: 'dlog_format == dlog_formats::openlogger ? openlogger_channel_map : [0]'
data:
seq:
- id: samples
type: sample
repeat: eos
types:
sample:
seq:
- id: channel
type:
switch-on: _root.body.header.sample_size
cases:
1: s1
2: s2
4: s4
repeat: expr
repeat-expr: _root.body.header.num_channels
enums:
dlog_formats:
1: openscope
3: openlogger
stop_reasons:
0: normal
1: forced
2: error
3: overflow
4: unknown
文字 [0]
被解析为字节数组:这是人们通常依赖的默认行为,因此启发式数组文字解析器将所有值符合 0..255 模式的数组视为字节数组,不是真正的数组。
如果你愿意,你仍然可以做一个真正的数组文字,通过类型转换来强制它:
[0].as<u1[]>
请注意,它可能会导致 C++98 出现问题,因为它缺少真正数组 (std::vector) 的单行初始化器。
我为两种非常相似的 Digilent 日志文件格式创建了一个 Kaitai 结构 .ksy
。第二种格式 (openlogger
) 是第一种格式 (openscope
) 的扩展,在结构中增加了两个字段。示波器基本上是一个单通道记录器;额外的记录器字段描述了活动通道的数量(a u1
,最多 8 个)和通道到样本顺序映射(a u1 x 8
)。
我试图通过合成 num_channels
和 channel_map
的始终存在的字段来协调两种格式的界面;这对 num_channels
实例来说效果很好。但是我不知道如何为通道映射创建合适的值,下面的 .ksy
报错:
/types/body/types/header/instances/channel_order/value: can't combine output types: ArrayType(Int1Type(false)) vs CalcBytesType
我不知道如何将 if_false
部分 ([0]
) 表示为 ArrayType。
有没有更好的方法来解决这个问题?
meta:
id: dlog
file-extension: dlog
seq:
- id: endianness
type: u1
doc: 0 - little endian 1 - big endian
- id: body
type: body
types:
body:
meta:
endian:
switch-on: _root.endianness
cases:
0: le
1: be
seq:
- id: header
type: header
instances:
data:
pos: header.start_of_data
type: data
types:
header:
seq:
- id: sample_size
type: u1
- id: header_size
type: u2
- id: start_of_data
type: u2
- id: dlog_format
type: u2
enum: dlog_formats
- id: dlog_version
type: u4
- id: voltage_units
type: u8
- id: stop_reason
type: u4
enum: stop_reasons
#...
- id: num_openlogger_channels
type: u4
if: dlog_format == dlog_formats::openlogger
doc: number of channels per sample
- id: openlogger_channel_map
type: u1
repeat: expr
repeat-expr: 8
if: dlog_format == dlog_formats::openlogger
doc: channel order
instances:
num_channels:
value: 'dlog_format == dlog_formats::openlogger ? num_openlogger_channels : 1'
channel_map:
value: 'dlog_format == dlog_formats::openlogger ? openlogger_channel_map : [0]'
data:
seq:
- id: samples
type: sample
repeat: eos
types:
sample:
seq:
- id: channel
type:
switch-on: _root.body.header.sample_size
cases:
1: s1
2: s2
4: s4
repeat: expr
repeat-expr: _root.body.header.num_channels
enums:
dlog_formats:
1: openscope
3: openlogger
stop_reasons:
0: normal
1: forced
2: error
3: overflow
4: unknown
文字 [0]
被解析为字节数组:这是人们通常依赖的默认行为,因此启发式数组文字解析器将所有值符合 0..255 模式的数组视为字节数组,不是真正的数组。
如果你愿意,你仍然可以做一个真正的数组文字,通过类型转换来强制它:
[0].as<u1[]>
请注意,它可能会导致 C++98 出现问题,因为它缺少真正数组 (std::vector) 的单行初始化器。