2012-12-06 04:36:45 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* System.Collections.Specialized.ListDictionary
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <winpr/collections.h>
|
2013-10-15 17:51:53 +04:00
|
|
|
#include <winpr/memory.h>
|
2012-12-06 04:36:45 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* C equivalent of the C# ListDictionary Class:
|
|
|
|
* http://msdn.microsoft.com/en-us/library/system.collections.specialized.listdictionary.aspx
|
2013-07-06 02:23:26 +04:00
|
|
|
*
|
|
|
|
* Internal implementation uses a singly-linked list
|
2012-12-06 04:36:45 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Properties
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the number of key/value pairs contained in the ListDictionary.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int ListDictionary_Count(wListDictionary* listDictionary)
|
|
|
|
{
|
2013-07-06 02:23:26 +04:00
|
|
|
int count = 0;
|
|
|
|
wListDictionaryItem* item;
|
|
|
|
|
2013-08-13 01:48:32 +04:00
|
|
|
if (listDictionary->synchronized)
|
|
|
|
EnterCriticalSection(&listDictionary->lock);
|
2013-07-06 02:23:26 +04:00
|
|
|
|
2013-08-13 01:48:32 +04:00
|
|
|
if (listDictionary->head)
|
2013-07-06 02:23:26 +04:00
|
|
|
{
|
2013-08-13 01:48:32 +04:00
|
|
|
item = listDictionary->head;
|
|
|
|
|
|
|
|
while (item)
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
item = item->next;
|
|
|
|
}
|
2013-07-06 02:23:26 +04:00
|
|
|
}
|
|
|
|
|
2013-08-13 01:48:32 +04:00
|
|
|
if (listDictionary->synchronized)
|
|
|
|
LeaveCriticalSection(&listDictionary->lock);
|
|
|
|
|
2013-07-06 02:23:26 +04:00
|
|
|
return count;
|
2012-12-06 04:36:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a value indicating whether the ListDictionary has a fixed size.
|
|
|
|
*/
|
|
|
|
|
|
|
|
BOOL ListDictionary_IsFixedSized(wListDictionary* listDictionary)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a value indicating whether the ListDictionary is read-only.
|
|
|
|
*/
|
|
|
|
|
|
|
|
BOOL ListDictionary_IsReadOnly(wListDictionary* listDictionary)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a value indicating whether the ListDictionary is synchronized (thread safe).
|
|
|
|
*/
|
|
|
|
|
|
|
|
BOOL ListDictionary_IsSynchronized(wListDictionary* listDictionary)
|
|
|
|
{
|
|
|
|
return listDictionary->synchronized;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Methods
|
|
|
|
*/
|
|
|
|
|
2013-10-18 00:30:36 +04:00
|
|
|
/**
|
|
|
|
* Gets the list of keys as an array
|
|
|
|
*/
|
|
|
|
|
|
|
|
int ListDictionary_GetKeys(wListDictionary* listDictionary, ULONG_PTR** ppKeys)
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
int count;
|
|
|
|
ULONG_PTR* pKeys;
|
|
|
|
wListDictionaryItem* item;
|
|
|
|
|
|
|
|
if (!ppKeys)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (listDictionary->synchronized)
|
|
|
|
EnterCriticalSection(&listDictionary->lock);
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
|
|
|
|
if (listDictionary->head)
|
|
|
|
{
|
|
|
|
item = listDictionary->head;
|
|
|
|
|
|
|
|
while (item)
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
item = item->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pKeys = (ULONG_PTR*) malloc(sizeof(ULONG_PTR*) * count);
|
|
|
|
ZeroMemory(pKeys, sizeof(ULONG_PTR*) * count);
|
|
|
|
|
|
|
|
index = 0;
|
|
|
|
|
|
|
|
if (listDictionary->head)
|
|
|
|
{
|
|
|
|
item = listDictionary->head;
|
|
|
|
|
|
|
|
while (item)
|
|
|
|
{
|
|
|
|
pKeys[index++] = (ULONG_PTR) item->key;
|
|
|
|
item = item->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*ppKeys = pKeys;
|
|
|
|
|
|
|
|
if (listDictionary->synchronized)
|
|
|
|
LeaveCriticalSection(&listDictionary->lock);
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2012-12-06 04:36:45 +04:00
|
|
|
/**
|
|
|
|
* Adds an entry with the specified key and value into the ListDictionary.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void ListDictionary_Add(wListDictionary* listDictionary, void* key, void* value)
|
|
|
|
{
|
2013-07-06 02:23:26 +04:00
|
|
|
wListDictionaryItem* item;
|
|
|
|
wListDictionaryItem* lastItem;
|
|
|
|
|
2013-08-13 01:48:32 +04:00
|
|
|
if (listDictionary->synchronized)
|
|
|
|
EnterCriticalSection(&listDictionary->lock);
|
|
|
|
|
2013-07-06 02:23:26 +04:00
|
|
|
item = (wListDictionaryItem*) malloc(sizeof(wListDictionaryItem));
|
|
|
|
|
|
|
|
item->key = key;
|
|
|
|
item->value = value;
|
2012-12-06 04:36:45 +04:00
|
|
|
|
2013-07-06 02:23:26 +04:00
|
|
|
item->next = NULL;
|
|
|
|
|
|
|
|
if (!listDictionary->head)
|
|
|
|
{
|
|
|
|
listDictionary->head = item;
|
|
|
|
}
|
2013-08-13 01:48:32 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
lastItem = listDictionary->head;
|
2013-07-06 02:23:26 +04:00
|
|
|
|
2013-08-13 01:48:32 +04:00
|
|
|
while (lastItem->next)
|
|
|
|
lastItem = lastItem->next;
|
2013-07-06 02:23:26 +04:00
|
|
|
|
2013-08-13 01:48:32 +04:00
|
|
|
lastItem->next = item;
|
|
|
|
}
|
2013-07-06 02:23:26 +04:00
|
|
|
|
2013-08-13 01:48:32 +04:00
|
|
|
if (listDictionary->synchronized)
|
|
|
|
LeaveCriticalSection(&listDictionary->lock);
|
2012-12-06 04:36:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all entries from the ListDictionary.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void ListDictionary_Clear(wListDictionary* listDictionary)
|
|
|
|
{
|
2013-07-06 02:23:26 +04:00
|
|
|
wListDictionaryItem* item;
|
|
|
|
wListDictionaryItem* nextItem;
|
|
|
|
|
2013-08-13 01:48:32 +04:00
|
|
|
if (listDictionary->synchronized)
|
|
|
|
EnterCriticalSection(&listDictionary->lock);
|
2013-07-06 02:23:26 +04:00
|
|
|
|
2013-08-13 01:48:32 +04:00
|
|
|
if (listDictionary->head)
|
2013-07-06 02:23:26 +04:00
|
|
|
{
|
2013-08-13 01:48:32 +04:00
|
|
|
item = listDictionary->head;
|
|
|
|
|
|
|
|
while (item)
|
|
|
|
{
|
|
|
|
nextItem = item->next;
|
2013-10-15 17:51:53 +04:00
|
|
|
if (listDictionary->object.fnObjectFree)
|
|
|
|
listDictionary->object.fnObjectFree(item);
|
2013-08-13 01:48:32 +04:00
|
|
|
free(item);
|
|
|
|
item = nextItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
listDictionary->head = NULL;
|
2013-07-06 02:23:26 +04:00
|
|
|
}
|
|
|
|
|
2013-08-13 01:48:32 +04:00
|
|
|
if (listDictionary->synchronized)
|
|
|
|
LeaveCriticalSection(&listDictionary->lock);
|
2012-12-06 04:36:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether the ListDictionary contains a specific key.
|
|
|
|
*/
|
|
|
|
|
|
|
|
BOOL ListDictionary_Contains(wListDictionary* listDictionary, void* key)
|
|
|
|
{
|
2013-08-13 01:48:32 +04:00
|
|
|
BOOL status = FALSE;
|
2013-07-06 02:23:26 +04:00
|
|
|
wListDictionaryItem* item;
|
|
|
|
|
2013-08-13 01:48:32 +04:00
|
|
|
if (listDictionary->synchronized)
|
|
|
|
EnterCriticalSection(&listDictionary->lock);
|
2013-07-06 02:23:26 +04:00
|
|
|
|
2013-08-13 01:48:32 +04:00
|
|
|
if (listDictionary->head)
|
2013-07-06 02:23:26 +04:00
|
|
|
{
|
2013-08-13 01:48:32 +04:00
|
|
|
item = listDictionary->head;
|
2013-07-06 02:23:26 +04:00
|
|
|
|
2013-08-13 01:48:32 +04:00
|
|
|
while (item)
|
|
|
|
{
|
|
|
|
if (item->key == key)
|
|
|
|
break;
|
|
|
|
|
|
|
|
item = item->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = (item) ? TRUE : FALSE;
|
2013-07-06 02:23:26 +04:00
|
|
|
}
|
|
|
|
|
2013-08-13 01:48:32 +04:00
|
|
|
if (listDictionary->synchronized)
|
|
|
|
LeaveCriticalSection(&listDictionary->lock);
|
|
|
|
|
|
|
|
return status;
|
2012-12-06 04:36:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the entry with the specified key from the ListDictionary.
|
|
|
|
*/
|
|
|
|
|
2013-10-18 01:27:47 +04:00
|
|
|
void* ListDictionary_Remove(wListDictionary* listDictionary, void* key)
|
2012-12-06 04:36:45 +04:00
|
|
|
{
|
2013-10-18 01:27:47 +04:00
|
|
|
void* value = NULL;
|
2013-07-06 02:23:26 +04:00
|
|
|
wListDictionaryItem* item;
|
|
|
|
wListDictionaryItem* prevItem;
|
|
|
|
|
2013-08-13 01:48:32 +04:00
|
|
|
if (listDictionary->synchronized)
|
|
|
|
EnterCriticalSection(&listDictionary->lock);
|
2013-07-06 02:23:26 +04:00
|
|
|
|
2013-08-13 01:48:32 +04:00
|
|
|
if (listDictionary->head)
|
2013-07-06 02:23:26 +04:00
|
|
|
{
|
2013-08-13 01:48:32 +04:00
|
|
|
item = listDictionary->head;
|
2013-07-06 02:23:26 +04:00
|
|
|
|
2013-08-13 01:48:32 +04:00
|
|
|
if (listDictionary->head->key == key)
|
2013-07-06 02:23:26 +04:00
|
|
|
{
|
2013-08-13 01:48:32 +04:00
|
|
|
listDictionary->head = listDictionary->head->next;
|
2013-10-15 16:11:14 +04:00
|
|
|
value = item->value;
|
2013-07-06 02:23:26 +04:00
|
|
|
free(item);
|
|
|
|
}
|
2013-08-13 01:48:32 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (item->next)
|
|
|
|
{
|
|
|
|
prevItem = item;
|
|
|
|
item = item->next;
|
|
|
|
|
|
|
|
while (item)
|
|
|
|
{
|
|
|
|
if (item->key == key)
|
|
|
|
{
|
|
|
|
prevItem->next = item->next;
|
2013-10-15 16:11:14 +04:00
|
|
|
value = item->value;
|
2013-08-13 01:48:32 +04:00
|
|
|
free(item);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-10-18 01:27:47 +04:00
|
|
|
prevItem = item;
|
2013-08-13 01:48:32 +04:00
|
|
|
item = item->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-06 02:23:26 +04:00
|
|
|
}
|
2013-08-13 01:48:32 +04:00
|
|
|
|
|
|
|
if (listDictionary->synchronized)
|
|
|
|
LeaveCriticalSection(&listDictionary->lock);
|
2013-10-18 01:27:47 +04:00
|
|
|
|
2013-10-15 16:11:14 +04:00
|
|
|
return value;
|
2013-07-06 02:23:26 +04:00
|
|
|
}
|
|
|
|
|
2013-10-15 18:15:44 +04:00
|
|
|
/**
|
|
|
|
* Removes the first (head) entry from the list
|
|
|
|
*/
|
|
|
|
|
|
|
|
void *ListDictionary_Remove_Head(wListDictionary* listDictionary)
|
|
|
|
{
|
|
|
|
wListDictionaryItem* item;
|
|
|
|
void *value = NULL;
|
|
|
|
|
|
|
|
if (listDictionary->synchronized)
|
|
|
|
EnterCriticalSection(&listDictionary->lock);
|
|
|
|
|
|
|
|
if (listDictionary->head)
|
|
|
|
{
|
|
|
|
item = listDictionary->head;
|
|
|
|
listDictionary->head = listDictionary->head->next;
|
|
|
|
value = item->value;
|
|
|
|
free(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listDictionary->synchronized)
|
|
|
|
LeaveCriticalSection(&listDictionary->lock);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2013-07-06 02:23:26 +04:00
|
|
|
/**
|
|
|
|
* Get an item value using key
|
|
|
|
*/
|
|
|
|
|
|
|
|
void* ListDictionary_GetItemValue(wListDictionary* listDictionary, void* key)
|
|
|
|
{
|
2013-08-13 01:48:32 +04:00
|
|
|
void* value = NULL;
|
2013-08-30 16:19:50 +04:00
|
|
|
wListDictionaryItem* item = NULL;
|
2013-07-06 02:23:26 +04:00
|
|
|
|
2013-08-13 01:48:32 +04:00
|
|
|
if (listDictionary->synchronized)
|
|
|
|
EnterCriticalSection(&listDictionary->lock);
|
2013-07-06 02:23:26 +04:00
|
|
|
|
2013-08-13 01:48:32 +04:00
|
|
|
if (listDictionary->head)
|
2013-07-06 02:23:26 +04:00
|
|
|
{
|
2013-08-13 01:48:32 +04:00
|
|
|
item = listDictionary->head;
|
|
|
|
|
|
|
|
while (item)
|
|
|
|
{
|
|
|
|
if (item->key == key)
|
|
|
|
break;
|
2013-07-06 02:23:26 +04:00
|
|
|
|
2013-08-13 01:48:32 +04:00
|
|
|
item = item->next;
|
|
|
|
}
|
2013-07-06 02:23:26 +04:00
|
|
|
}
|
|
|
|
|
2013-08-13 01:48:32 +04:00
|
|
|
value = (item) ? item->value : NULL;
|
|
|
|
|
|
|
|
if (listDictionary->synchronized)
|
|
|
|
LeaveCriticalSection(&listDictionary->lock);
|
|
|
|
|
|
|
|
return value;
|
2013-07-06 02:23:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set an item value using key
|
|
|
|
*/
|
|
|
|
|
|
|
|
BOOL ListDictionary_SetItemValue(wListDictionary* listDictionary, void* key, void* value)
|
|
|
|
{
|
2013-08-13 01:48:32 +04:00
|
|
|
BOOL status = FALSE;
|
2013-07-06 02:23:26 +04:00
|
|
|
wListDictionaryItem* item;
|
|
|
|
|
2013-08-13 01:48:32 +04:00
|
|
|
if (listDictionary->synchronized)
|
|
|
|
EnterCriticalSection(&listDictionary->lock);
|
2013-07-06 02:23:26 +04:00
|
|
|
|
2013-08-13 01:48:32 +04:00
|
|
|
if (listDictionary->head)
|
2013-07-06 02:23:26 +04:00
|
|
|
{
|
2013-08-13 01:48:32 +04:00
|
|
|
item = listDictionary->head;
|
2013-07-06 02:23:26 +04:00
|
|
|
|
2013-08-13 01:48:32 +04:00
|
|
|
while (item)
|
|
|
|
{
|
|
|
|
if (item->key == key)
|
|
|
|
break;
|
|
|
|
|
|
|
|
item = item->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item)
|
|
|
|
item->value = value;
|
|
|
|
|
|
|
|
status = (item) ? TRUE : FALSE;
|
2013-07-06 02:23:26 +04:00
|
|
|
}
|
|
|
|
|
2013-08-13 01:48:32 +04:00
|
|
|
if (listDictionary->synchronized)
|
|
|
|
LeaveCriticalSection(&listDictionary->lock);
|
2013-07-06 02:23:26 +04:00
|
|
|
|
2013-08-13 01:48:32 +04:00
|
|
|
return status;
|
2012-12-06 04:36:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construction, Destruction
|
|
|
|
*/
|
|
|
|
|
|
|
|
wListDictionary* ListDictionary_New(BOOL synchronized)
|
|
|
|
{
|
|
|
|
wListDictionary* listDictionary = NULL;
|
|
|
|
|
|
|
|
listDictionary = (wListDictionary*) malloc(sizeof(wListDictionary));
|
|
|
|
|
|
|
|
if (listDictionary)
|
|
|
|
{
|
|
|
|
listDictionary->synchronized = synchronized;
|
2013-07-06 02:23:26 +04:00
|
|
|
|
|
|
|
listDictionary->head = NULL;
|
2013-08-13 01:48:32 +04:00
|
|
|
|
|
|
|
InitializeCriticalSectionAndSpinCount(&listDictionary->lock, 4000);
|
2013-11-04 05:46:40 +04:00
|
|
|
|
|
|
|
ZeroMemory(&(listDictionary->object), sizeof(wObject));
|
2012-12-06 04:36:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return listDictionary;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListDictionary_Free(wListDictionary* listDictionary)
|
|
|
|
{
|
2013-07-06 02:23:26 +04:00
|
|
|
if (listDictionary)
|
|
|
|
{
|
|
|
|
ListDictionary_Clear(listDictionary);
|
2013-08-13 01:48:32 +04:00
|
|
|
DeleteCriticalSection(&listDictionary->lock);
|
2013-07-06 02:23:26 +04:00
|
|
|
free(listDictionary);
|
|
|
|
}
|
2012-12-06 04:36:45 +04:00
|
|
|
}
|
|
|
|
|