1. 程式人生 > >[C/C++]_[字串處理]_[過濾出HTML標籤的屬性值]

[C/C++]_[字串處理]_[過濾出HTML標籤的屬性值]

typedef struct ElementAttribute ElementAttribute;

struct ElementAttribute
{
	const char* name;
	const char* value;
	ElementAttribute* previous;
};

static const char* HtmlFindSpaceFromRecursion(const char* str, const char* start)
{
	const char* p = str;
	while (p >= start)
	{
		if (*p == 0x20)
		{
			return p;
		}
		--p;
	}
	return str;
}

/**
 * 1.注意,這個函式只適合標準的屬性格式,也就是"是成對存在.
 */
static ElementAttribute* HtmlGetElementAttribute(char* str)
{
	ElementAttribute *attr = NULL;
	static char quote = '\"';
	char* p = strchr(str, quote);
	while (p)
	{
		char *start = p;
		//屬性名
		char* a_name_start = (char*) HtmlFindSpaceFromRecursion(start, str);
		*(start - 1) = 0;

		//屬性值
		p = strchr(start + 1, quote);
		*p = 0;

		ElementAttribute *next = (ElementAttribute*) malloc(
				sizeof(ElementAttribute));
		memset(next, 0, sizeof(ElementAttribute));
		next->name = a_name_start + 1;
		next->value = start + 1;
		next->previous = attr;
		attr = next;

		p = strchr(p + 1, quote);
	}
	return attr;
}

TEST(main,testElementAttribute)
{
	char* str = strdup("p height=\"1em\" width=\"0pt\" align=\"center\"");
	ElementAttribute* attr = HtmlGetElementAttribute(str);
	while(attr)
	{
		QXLOG("name: ",attr->name);
		QXLOG("value:",attr->value);
		attr = attr->previous;
	}
	free(str);
}