예제 #1
0
bool LocalStore::Delete(const char* identifier)
{
	if (identifier == NULL)
	{
		return false;
	}

	Category* category = &m_rootCategory;
	Entry* entry = NULL;

	int identifierLen = strlen(identifier);
	char* identifierCopy = new char[identifierLen];
	memcpy(identifierCopy, identifier, identifierLen);

	//Loop over tokens
	char* nextToken = NULL;
	char* token = strtok(identifierCopy, ".");
	while (token != NULL)
	{
		//The next token is retrieved first so that we know which one is the last one.
		nextToken = strtok(NULL, ".");

		//If we haven't reached the last token, then the current one is a category.
		if (nextToken != NULL)
		{
			category = category->GetSubcategory(token);
		}
		//Otherwise we've reached the value.
		else
		{
			//Iterate through and delete
			List<Entry*>& entries = category->GetEntries();
			List<Entry*>::Node* n = entries.Begin();
			while (n != NULL)
			{
				if (n->GetValue()->GetName() == token)
				{
					delete n->GetValue();
					entries.Erase(n);

					delete[] identifierCopy;

					return true;
				}

				n = n->GetNext();
			}
		}

		//Next token becomes current token for next loop iteration.
		token = nextToken;
	}

	delete[] identifierCopy;

	return false;
}
예제 #2
0
Entry* LocalStore::Get(const char* identifier)
{
	if (identifier == NULL)
	{
		return NULL;
	}

	Category* category = &m_rootCategory;
	Entry* entry = NULL;

	int identifierLen = strlen(identifier);
	char* identifierCopy = new char[identifierLen+1];
	memcpy(identifierCopy, identifier, identifierLen);
	identifierCopy[identifierLen] = NULL;

	//Loop over tokens
	char* nextToken = NULL;
	char* token = strtok(identifierCopy, ".");
	while (token != NULL)
	{
		//The next token is retrieved first so that we know which one is the last one.
		nextToken = strtok(NULL, ".");

		//If we haven't reached the last token, then the current one is a category.
		if (nextToken != NULL)
		{
			category = category->GetSubcategory(token);
			if (!category)
			{
				return NULL;
			}
		}
		//Otherwise we've reached the value.
		else
		{
			entry = category->GetEntryByName(token);
		}

		//Next token becomes current token for next loop iteration.
		token = nextToken;
	}

	delete[] identifierCopy;

	return entry;
}