prg_str_decomp_with_options()

摘要

将由特定字符分隔字段组成的源字符串分解为字符串列表,并可选择控制字符串的处理方式。

语法

prg_str_decomp_with_options (string, separators, options)
参数 类型 说明
string const char * 要分解为字段的源字符串 [该字符串应包含一系列字段,字段之间以 separators 参数中列出的任一分隔字符分隔。传入的字符串不受此函数影响。
separators const char * 用于分隔源字符串字段的分隔字符 [该字符串应包含一组用作源字符串字段定界符的字符。常见选择包括竖线(“|”)、逗号(“,”)、句点(“.”)或分号(“;”)字符。
options int 分解选项(见 Details)。

返回类型

PrgT_List *

指向从源字符串中提取出的字段列表(即字符串列表)的指针。该列表指针通常直接赋给声明为 PrgT_List* 的变量。即使列表中不含任何字段值,也会返回有效的列表指针。若发生可恢复错误,则返回 PRGC_NIL。

头文件

prg_string_funcs.h

示例

/* open and read in an address/stream translation table file */
table_list_ptr = prg_gdf_read ("addr_to_strm");

/* determine how many rows the table contains */
num_rows = prg_list_size (table_list_ptr);

/* loop through the table’s rows */
for (i = 0; i < num_rows; i++)
	{
	/* obtain a list of the columnar fields in each row */
	/* some fields might contain single quotes */
	field_list_ptr = prg_str_decomp_with_options (prg_list_access (table_list_ptr, i), ":", PRGC_STR_DECOMP_OPT_IGNORE_QUOTES);

	/* extract the values of the two fields, and insert into the table matrix */
	trans_tab [i].address = atoi (prg_list_access (field_list_ptr, 0));
	trans_tab [i].stream = atoi (prg_list_access (field_list_ptr, 1));

	/* deallocate the field list contents and list */
	prg_list_free (field_list_ptr);
	prg_mem_free (field_list_ptr);
	}
/* deallocate the table list contents and list */
prg_list_free (table_list_ptr);
prg_mem_free (table_list_ptr);

详情

该函数与 prg_str_decomp() 完成相同的工作,但能够对返回的字符串进行可选的修改。这些修改通过 options 参数指定。

options 必须是 PRGC_STR_DECOMP_OPT_NONE,或以下符号常量的 OR 组合:

  • PRGC_STR_DECOMP_OPT_INCLUDE_EMPTY_FIELDS—为空字段包含空字符串
  • PRGC_STR_DECOMP_OPT_UNIQUE—返回的列表中只包含唯一字符串
  • PRGC_STR_DECOMP_OPT_UNIQUE_CASE_INSENS—返回的列表中只包含唯一字符串,但不考虑大小写
  • PRGC_STR_DECOMP_OPT_IGNORE_QUOTES—查找分隔字符时忽略单引号和双引号
  • PRGC_STR_DECOMP_OPT_REMOVE_ESCAPES—在将字符串放入列表之前移除其中的反斜杠
  • PRGC_STR_DECOMP_OPT_REMOVE_QUOTES—在将字符串放入列表之前移除其中的引号。(此选项隐含 _REMOVE_ESCAPES 并覆盖 _IGNORE_QUOTES。)
  • PRGC_STR_DECOMP_OPT_LEAVE_LEADING_WHITESPACE—保留字符串开头的空白

用途

提供了一种从源字符串中提取多个字段的机制,并可选择控制字符串的处理方式。这在处理表格信息时尤为有用:源字符串代表表格的一行,字符串的各字段代表该行在各列中的“单元格”。表格可以通过 prg_gdf_read() 从 GDF 读入字符串列表,用 prg_list_access() 按行顺序循环扫描,并用 prg_str_decomp_with_options() 将每行字符串分解为字段(Example 小节演示了这一技术)。

错误

  • 可恢复错误:内存分配失败。
  • 程序中止:段违例。(由于字符串指针非法或字符串畸形)

参考

本站文档由 北京智网畅联科技有限公司 整理、翻译并提供 · Documentation compiled and provided by Beijing ZNetLink Technology Co., Ltd.

交流群 · QQ Group:OPNET/智网妙算网络仿真交流群(群号 486383494)

原文档 © 2017 Riverbed Technology · 本站为公益翻译,由 AI 辅助完成,请仔细甄别

Original documentation © 2017 Riverbed Technology. Community translation, AI-assisted. Contact: jiazequn@znetlink.com