/* Adds an item to the inventory, if it finds an item with less than StackSize it adds the amount * else it will create a new item with the remaining amount and set the one found to StackSize * * @return the input item with the amount set to the remainder if any, i.e. if it's not 0 then the inventory was full */ AItem* USlotContainer::AddItem(AItem* itemToAdd) { if (HasSpace() && itemToAdd->GetAmount() <= itemToAdd->GetStackSize()) { items.Add(itemToAdd); AItem* tempItem = itemToAdd->Copy(); tempItem->SetAmount(0); return tempItem; } else { AItem* existingItem = GetExistingItemWithSpace(itemToAdd); // Check all existing matching items to see if they have space while (itemToAdd->GetAmount() > 0 && existingItem != nullptr) { int amountToAdd = itemToAdd->GetAmount(); existingItem->TakeFrom(itemToAdd); // Try to find another item to add to existingItem = GetExistingItemWithSpace(itemToAdd); } // Keep adding new items until we're either full or added all items while (itemToAdd->GetAmount() > 0 && HasSpace()) { // Make a new item AItem* newItem = itemToAdd->Copy(); newItem->SetAmount(0); newItem->TakeFrom(itemToAdd); // Add the new item items.Add(newItem); } } return itemToAdd; }
/** * Given the original argv stuff, the length of our target executable, and the * filepath, build a suitable command line for CreateProcess. */ STATIC LPTSTR BuildCmdLine(int argc, TCHAR* argv[], SIZE_T szExecutable, LPTSTR lpExecutable) { int i = 1; LPTSTR lpCmdLine = NULL; SIZE_T szCmdLine = szExecutable; TCHAR** lpNewArgs = (TCHAR**)calloc(argc, sizeof(TCHAR*)); assert(lpNewArgs != NULL); // Quote the executable path if need be. XINFO(_T("Building command line..")); if(HasSpace(lpExecutable, szExecutable)) { lpNewArgs[0] = Quote(lpExecutable, &szCmdLine, FALSE); } else { lpNewArgs[0] = DupArg(lpExecutable, &szCmdLine, FALSE); } // Iterate through all args, quoting as necessary. for(; i < argc; i++) { SIZE_T szArg = (SIZE_T)_tcslen((const TCHAR*)argv[i]); if(HasSpace(argv[i], szArg)) { lpNewArgs[i] = Quote(argv[i], &szArg, TRUE); } else { lpNewArgs[i] = DupArg(argv[i], &szArg, TRUE); } // +1 for space + len(arg) szCmdLine += szArg; } // Allocate our cmdline buffer and begin adding the various parts. lpCmdLine = (LPTSTR)calloc(szCmdLine + 1, sizeof(TCHAR)); assert(lpCmdLine != NULL); _tcscpy(lpCmdLine, lpNewArgs[0]); XINFO(_T(" [0] => \"%s\""), lpNewArgs[0]); // Cleanup allocated arg. xfree(lpNewArgs[0]); lpNewArgs[0] = NULL; for(i = 1; i < argc; i++) { XINFO(_T(" [%d] => \"%s\""), i, argv[i]); _tcscat(lpCmdLine, lpNewArgs[i]); xfree(lpNewArgs[i]); lpNewArgs[i] = NULL; } XINFO(_T("Finished building..")); XINFO(_T("CmdLine: %s"), lpCmdLine); xfree(lpNewArgs); return lpCmdLine; }