Game Development Community

getWords with custom delimiters

by Thomas "elfprince13" Dickerson · 10/21/2009 (2:23 am) · 1 comments

add's 4 functions, along the lines of getWord/getField/getRecord, et. al, but with the ability to specify your own delimiter set. This is similar to nextToken, but nextToken's greedy token-grabbing behavior makes it unsuitable for applications where the ability to locate empty records is important.

First, find in consoleFunctions.cc

ConsoleFunction(getWord, const char *, 3, 3, "(string text, int index)")
{
   argc;
   return getUnit(argv[1], dAtoi(argv[2]), " tn");
}

before, it add this

ConsoleFunction(getDelimitedItem, const char *, 4, 4, "(string text, string delimeters, int index"){
	argc;
	return getUnit(argv[1], dAtoi(argv[3]), argv[2]);
}

ConsoleFunction(getDelimitedItems, const char *, 4, 5, "(string text, string delimeters, int index, int endIndex=INF"){
	argc;
	U32 endIndex;
	if(argc==4)
		endIndex = 1000000;
	else
		endIndex = dAtoi(argv[4]);
	return getUnits(argv[1], dAtoi(argv[3]), endIndex, argv[2]);
}


ConsoleFunction(setDelimitedItem, const char *, 5, 5, "newText = setDelimitedItem(text, delimiters, index, replace)")
{
	argc;
	return setUnit(argv[1], dAtoi(argv[3]), argv[4], argv[2]);
}

ConsoleFunction(removeDelimitedItem, const char *, 4, 4, "newText = removeDelimitedItem(text, delimiters, index)")
{
	argc;
	return removeUnit(argv[1], dAtoi(argv[3]), argv[2]);
}

ConsoleFunction(getDelimitedItemCount, S32, 3, 3, "getDelimitedItemCount(text, delimeters)")
{
	argc;
	return getUnitCount(argv[1], argv[2]);
}

About the author

Computer Science and Physics major at Saint Michael's College. Lead developer + project coordinator for FreeBuild. Administrator, Cemetech tech community. Webmaster for the Village2Village Projects and the Vermont Sustainable Heating Initiative.


#1
10/21/2009 (5:30 am)
Wow, I didn't know about getUnit. Very useful, thank you!