Example #1
0
/**
 * Parse the received XML, and search for title& snippet attributes.
 */
void MediaWiki::processSearchResults()
{
	/* for ex, try this request for "bear" in your browser:
	 *   http://en.wikipedia.org/w/api.php?action=query&format=xml&
	 *   list=search&srwhat=text&srsearch=bear
	 * the output looks like this:
	 * ...
	 * <search>
	 * <p ns="0" title="title1" snippet="text.." size="10283"
	 * wordcount="1324" timestamp="2011-04-12T14:25:24Z" />
	 * more paragraphs
	 * </search>
	 * ...
	 */

	// Search for each p paragraph, and get the title & snippet.
	MAUtil::String input = mBuffer;
	int openTag = input.find("<p ",0);

	while( openTag != -1 )
	{
		int closeTag = input.find("/>", openTag+1);
		if (closeTag != -1 && closeTag > openTag)
		{
			MAUtil::String  record = input.substr(
				openTag+2, closeTag - openTag +1);
			// A record, ex: <p ns="0" title="title1" snippet="text.."
			// size="10283" wordcount="1324" timestamp="2011-04-12T14:25:24Z" />
			// Add the title if it doesn't exist yet.
			MAUtil::String newRecord = getTitle(record);
			bool canAdd(true);
			for (int i=0; i < mWiki->titleResults.size(); i++)
			{
				if ( mWiki->titleResults[i] == newRecord )
				{
					canAdd = false;
				}
			}
			if (canAdd)
			{
				mWiki->titleResults.add(newRecord);
				mWiki->snippetResults.add(getSnippet(record));
			}
		}
		input.remove(0,closeTag);

		// Get the next tag.
		openTag = input.find("<p ",0);
	}
}
Example #2
0
	void string() {
		MAUtil::String str = "test";
		assert("String::==", str == "test");
		assert("String::!=", str != "fest");
		assert("String::<", !(str < "fest") && (MAUtil::String("fest") < str));
		assert("String::>", !(MAUtil::String("fest") > str) && (str > "fest"));
		assert("String::<=", str <= "test" && str <= "west");
		assert("String::>=", str >= "test" && str >= "fest");
		assert("String::+", (str + "ing") == "testing");
		str+="ing";
		assert("String::+=", str == "testing");
		assert("String::find()", str.find("ing") == 4 && str.find("1") == MAUtil::String::npos);
		str+=" string";
		assert("String::findLastOf()", str.findLastOf('g') == 13 && str.findLastOf('1') == MAUtil::String::npos);
		assert("String::findFirstOf()", str.findFirstOf('g') == 6 && str.findFirstOf('1') == MAUtil::String::npos);
		assert("String::findFirstNotOf()", str.findFirstNotOf('t') == 1 && str.findFirstNotOf('1') == 0);
		str.insert(7, " MAUtil::");
		assert("String::insert(string)", str == "testing MAUtil:: string");

		str.remove(16, 2);
		assert("String::remove()", str == "testing MAUtil::tring");

		str.insert(16, 'S');
		assert("String::insert(char)", str == "testing MAUtil::String");

		assert("String::substr()", str.substr(8, 6) == "MAUtil");

		assert("String::length()", str.length() == 22);

		str.reserve(32);
		assert("String::reserve()", str == "testing MAUtil::String" && str.length() == 22);
		assert("String::capacity()", str.capacity() == 32);

		str.clear();
		assert("String::clear()", str.length() == 0 && str == "");
	}