Yay finished shopping list!

This commit is contained in:
Greyscale 2009-09-20 14:58:50 -07:00
parent 3949280010
commit bfd9d47505
2 changed files with 219 additions and 31 deletions

View File

@ -18,3 +18,5 @@ if not exist "%BUILDDIR%" (
:: Compile. :: Compile.
echo Starting compiler: echo Starting compiler:
%SPCOMP% -i%SOURCEDIR% -i%SOURCEDIR%/include -i%SMINCLUDES% -o%BUILDDIR%/zombiereloaded.smx %SOURCEDIR%\zombiereloaded.sp %SPCOMP% -i%SOURCEDIR% -i%SOURCEDIR%/include -i%SMINCLUDES% -o%BUILDDIR%/zombiereloaded.smx %SOURCEDIR%\zombiereloaded.sp
pause

View File

@ -39,9 +39,7 @@
* *
* List Options: * List Options:
* *
* Parse: Take a string and dump out a list. Simple. * Construct: Take a string and dump out a list. Simple.
* Set: Easy to do without this API, but maybe we can simplify it a little.
* Add: Should be a simple format. May be in the API just to simplify it a bit.
* Remove: Removes an item from the list. Must remove from the end to preserve list order. * Remove: Removes an item from the list. Must remove from the end to preserve list order.
* *
* Scenario: * Scenario:
@ -53,57 +51,245 @@
* *
*/ */
/**
* @section String length defines.
*/
#define SHOPPINGLIST_MAXITEMS 32
#define SHOPPINGLIST_MAXLEN 32
/**
* @endsection
*/
/** /**
* Takes a given string and outputs a re-formatted shopping list styled list. See description above. * Takes a given string and outputs a re-formatted shopping list styled list. See description above.
* Note: This is for display purposes only, meaning there will be no API made to format the output of this function. * Note: This is for display purposes only, meaning there will be no API made to format the output of this function.
* If you plan on the list being dynamic, store the raw string and use the API to edit those. * If you plan on the list being dynamic, store the raw string and use the API to edit those.
* *
* @param list Raw string to be formatted. * @param list Raw string to be formatted.
* @param list_maxlen The maximum length of the raw string.
* @param shoppinglist Outputted shopping list. * @param shoppinglist Outputted shopping list.
* @param slist_maxlen The maximum length of the shopping list. * @param slist_maxlen The maximum length of the shopping list.
* @param in_token The token used to separate each item in the raw string. Ex: "Apple, Orange" Token: "," * @param in_token The token used to separate each item in the raw string. Ex: "Apple, Orange" Token: ","
* @param out_token What to separate each item with in the shopping list. Ex: Token: "\n" List: "Apple\nOrange" * @param out_token What to separate each item with in the shopping list. Ex: Token: "\n" List: "Apple\nOrange"
* @param quantityformat How to show the quantity of an item for multi-listed items. %d is the number. Ex: "Apples x2" quantityformat: " x%d" * @param quantityformat How to show the quantity of an item for multi-listed items. ## is the number. Ex: "Apples x2" quantityformat: " x##"
*/ */
stock ShoppingListFormat(const String:list[], list_maxlen, String:shoppinglist[], slist_maxlen, const String:in_token[] = ",", const String:out_token[] = ",", const String:quantityformat[] = " x%d") stock ShoppingListConstruct(const String:list[], String:shoppinglist[], slist_maxlen, const String:in_token[] = ",", const String:out_token[] = ",", const String:quantityformat[] = " x##")
{ {
// Put each item in a list.
new String:items[SHOPPINGLIST_MAXITEMS][SHOPPINGLIST_MAXLEN];
ExplodeString(list, in_token, items, SHOPPINGLIST_MAXITEMS, SHOPPINGLIST_MAXLEN);
// Put each item in a trie where we count each item.
new Handle:trieItems = CreateTrie();
new item_count;
new validstrings;
// x = Array index.
for (new x = 0; x < SHOPPINGLIST_MAXITEMS; x++)
{
// Trim off any whitespace.
TrimString(items[x]);
// If there are no more items left, exit the loop.
if (!items[x][0])
break;
// Reset item_count to 0, retrieve value, if it already exists, and set value + 1.
item_count = 0;
GetTrieValue(trieItems, items[x], item_count);
SetTrieValue(trieItems, items[x], ++item_count);
validstrings++;
} }
/** // Initialize 'shoppinglist'.
* Set the list to a given string array of items. strcopy(shoppinglist, slist_maxlen, "");
*
* @param list The variable to store new list in. decl String:item_formatted[SHOPPINGLIST_MAXLEN];
* @param maxlen The maximum length of the finished list. decl String:strQuantity[8];
* @param items The items to construct into the list.
* @param maxitems The number of array indexes from 0 to process for the list. // x = Array index.
* @param token The token to use to append the item. Set to "" to auto-detect an existing token, if for (new x = 0; x < validstrings; x++)
* there is only one item in the list, this parameter won't be used.
*/
stock ShoppingListConstruct(String:list[], maxlen, const String:items[][], maxitems, const String:token)
{ {
// Get quantity count, in string and int.
GetTrieValue(trieItems, items[x], item_count);
// If the item_count is 0, this item is already in the shopping list.
if (item_count == 0)
continue;
IntToString(item_count, strQuantity, sizeof(strQuantity));
strcopy(item_formatted, sizeof(item_formatted), items[x]);
// Format the quantity on if there was more than 1.
if (item_count > 1)
{
Format(item_formatted, sizeof(item_formatted), "%s%s", item_formatted, quantityformat);
ReplaceString(item_formatted, sizeof(item_formatted), "##", strQuantity);
}
// Format the item onto the list.
if (!shoppinglist[0])
{
strcopy(shoppinglist, slist_maxlen, item_formatted);
}
else
{
Format(shoppinglist, slist_maxlen, "%s%s%s", shoppinglist, out_token, item_formatted);
}
SetTrieValue(trieItems, items[x], 0);
}
// Destroy the trie handle.
CloseHandle(trieItems);
} }
/** /**
* Add an item to the list. * Add an item to the list.
* Notes:
* For max number of items, and the max length of each item, see SHOPPINGLIST_* defines at the top.
* *
* @param list The list to add item to. * @param list The list to remove item from.
* @param maxlen The maximum length of the finished list. * @param maxlen The maximum length of the finished list.
* @param item The item to add to the list. * @param item The item to remove from the list.
* @param token The token to use to append the item. Set to "" to auto-detect an existing token, if * @param token The token to remove with the item.
* this is the first item in the list, this parameter won't be used. * @return True if the item was found and removed, false if not found.
*/ */
stock ShoppingListAppend(String:list[], maxlen, const String:item[], const String:token) public bool:ShoppingListRemove(String:list[], maxlen, const String:item[], const String:token[])
{ {
// Explode the items into an array.
new String:items[SHOPPINGLIST_MAXITEMS][SHOPPINGLIST_MAXLEN];
ExplodeString(list, token, items, SHOPPINGLIST_MAXITEMS, SHOPPINGLIST_MAXLEN);
new validstrings;
// x = Array index.
for (new x = SHOPPINGLIST_MAXITEMS - 1; x >= 0; x--)
{
// Trim off any whitespace.
TrimString(items[x]);
if (StrEqual(item, items[x], false))
{
// Nullify the matching string.
items[x][0] = 0;
// Re-copy each index after the matching string to overwrite it.
for (new y = x; y < SHOPPINGLIST_MAXITEMS - 1; y++)
items[y] = items[y + 1];
// Implode the array of strings back together into a full string.
ImplodeStrings(items, validstrings + x, token, list, maxlen);
return true;
} }
// If this is a valid string, count it.
if (items[x][0])
validstrings++;
}
return false;
}
/**
*
*
* BROKEN STUFF
* Attempted versions of the above code.
* The working version is mostly (99%) Frus's code.
*
*
*/
/** /**
* Add an item to the list. * Add an item to the list.
* *
* @param list The list to remove item from. * @param list The list to remove item from.
* @param maxlen The maximum length of the finished list. * @param maxlen The maximum length of the finished list.
* @param item The item to remove from the list. * @param item The item to remove from the list.
* @param token The token to remove with the item.
* @return True if the item was found and removed, false if not found.
*/ */
stock ShoppingListRemove(String:list[], maxlen, const String:item[]) /*stock bool:ShoppingListRemove(String:list[], maxlen, const String:item[], const String:token[])
{ {
new itemindex = -1;
new itemindex2 = -1;
new pos;
while (itemindex == itemindex2)
{
itemindex2 = pos = StrContains(list[pos], item, false);
if (itemindex2 != -1)
{
itemindex = itemindex2;
pos += 1;
PrintToServer("%d %d %d %s", itemindex, itemindex2, pos, list[pos]);
} }
else
break;
}
if (itemindex == -1)
return false;
PrintToServer("position %d", itemindex);
return true;
}*/
/**
* Add an item to the list.
*
* @param list The list to remove item from.
* @param maxlen The maximum length of the finished list.
* @param maxitems The maximum number of shopping items.
* @param maxitemlen The maximum length of EACH item in the string.
* @param item The item to remove from the list.
* @param token The token to remove with the item.
* @return True if the item was found and removed, false if not found.
*/
/*stock bool:ShoppingListRemove(String:list[], maxlen, maxitems, maxitemlen, const String:item[], const String:token[])
{
// Default initialized value = false.
new bool:success;
// Put each item in a list.
new String:items[maxitems][maxitemlen];
ExplodeString(list, token, items, maxitems, maxitemlen);
// x = List index.
for (new x = maxitems - 1; x >= 0; x--)
{
PrintToServer("%d %s", x, items[x]);
if (StrEqual(items[x], item, false))
{
strcopy(items[x], maxitemlen, "");
success = true;
break;
}
}
new String:items_after[maxitems][maxitemlen];
new count;
// x = List index.
for (new x = 0; x < maxitems; x++)
{
PrintToServer("%d \"%s\"", x, items[x]);
if (!items[x][0])
continue;
// Move valid item to new array, and increment count after.
strcopy(items_after[count++], maxitemlen, items[x]);
}
// Join the valid strings back together.
ImplodeStrings(items_after, count, token, list, maxlen);
return success;
}*/