op_pk_name_to_index()
摘要
基于指定的数据包格式,把字段名称转换为对应的数值字段索引。
语法
op_pk_name_to_index (format_name, field_name)
| 参数 | 类型 | 说明 |
|---|---|---|
format_name |
const char * |
目标数据包格式的名称 |
field_name |
const char * |
目标字段的名称。 |
返回类型
int
指定字段的数值索引。
如果发生错误,将返回常量 OPC_FIELD_INDEX_INVALID。
示例
/* pull the next packet off the queue */
pkptr = op_subq_pk_remote (0, OPC_QPOS_HEAD);
/* determine the size of the information field */
info_size = op_pk_nfd_size (pkptr, "fd_info");
/* if the size of this field is too large, clamp it */
if (info_size > 1300)
{
/* convert the field name into a field index */
fd_index = op_pk_name_to_index ("AX.25", "fd_info");
/* set the field size to the maximum */
op_pk_fd_set_int32 (pkptr, fd_index, 1300, 16);
}
详情
该函数与 op_pk_nfd_name_to_index() 类似,但接受的是数据包格式的名称而不是数据包指针。
用途
提供一种在给定字段名称的情况下获取数据包格式中某字段数值索引的方式。这使你可以在并不实际拥有数据包的情况下计算索引值。这样,你可以预先设置一组索引变量,供后续代码配合 op_pk_fd_set()、op_pk_fd_get() 及其变体使用,以比 op_pk_nfd_ 系列函数更高效地访问字段。
错误
- 可恢复错误:数据包格式 (
<format_name>) 中不存在名为 (<field_name>) 的字段
参考
- 使用内核过程 op_pk_nfd_name_to_index() 对指定数据包(而不是数据包格式)执行同样的操作
- 使用内核过程 op_pk_fd_index_to_name() 执行反向操作,把数值字段索引转换为字段名称
- 使用内核过程 op_pk_fd_set() 通过数值索引设置数据包中某字段的值