167 lines
4.1 KiB
C
167 lines
4.1 KiB
C
#include "utils.h"
|
|
|
|
#pragma region WFString
|
|
|
|
WFERROR WFString_Alloc(WFString** strl, wchar_t* raw_data) {
|
|
WFERROR ec;
|
|
if ((ec = WFString_Alloc(strl, 0)) != WFERROR_OK) return ec;
|
|
if ((ec = WFString_SetData(*strl, raw_data)) != WFERROR_OK) return ec;
|
|
return WFERROR_OK;
|
|
}
|
|
|
|
WFERROR WFString_Alloc(WFString** strl, char* raw_data, BOOL is_utf8) {
|
|
return WFERROR();
|
|
}
|
|
|
|
WFERROR WFString_Alloc(WFString** strl, uint32_t size) {
|
|
*strl = WFNEW(WFString);
|
|
if (*strl == NULL) return WFERROR_ALLOC;
|
|
|
|
// only init capacity and raw data here, other field will be refreshed in rezise func
|
|
WFERROR ec;
|
|
(*strl)->mCapacity = 0;
|
|
(*strl)->mRawData = NULL;
|
|
ec = WFString_Resize(*strl, size);
|
|
|
|
// return resize result
|
|
return ec;
|
|
}
|
|
|
|
WFERROR WFString_Free(WFString* strl) {
|
|
if (strl == NULL) return WFERROR_OK;
|
|
if (strl->mRawData == NULL) return WFERROR_NULLPTR;
|
|
free(strl->mRawData);
|
|
free(strl);
|
|
return WFERROR_OK;
|
|
}
|
|
|
|
WFERROR WFString_Resize(WFString* strl, uint32_t new_size) {
|
|
// if remain data is not enough, we need alloc new one
|
|
if (new_size >= strl->mCapacity) {
|
|
strl->mCapacity = new_size * 2;
|
|
strl->mRealCapacity = strl->mCapacity + 1;
|
|
|
|
// alloc buffer
|
|
if (strl->mRawData != NULL)
|
|
strl->mRawData = realloc(strl->mRawData, strl->mRealCapacity * sizeof(wchar_t));
|
|
else
|
|
strl->mRawData = malloc(strl->mRealCapacity * sizeof(wchar_t));
|
|
if (strl->mRawData == NULL) return WFERROR_ALLOC;
|
|
}
|
|
|
|
// set length as new length
|
|
strl->mLength = new_size;
|
|
strl->mRealLength = strl->mLength + 1;
|
|
|
|
// set the last one is zero
|
|
strl->mRawData[strl->mRealLength - 1] = 0;
|
|
return WFERROR_OK;
|
|
}
|
|
|
|
WFERROR WFString_GetData(WFString* strl, wchar_t** pdata) {
|
|
if (strl == NULL) return WFERROR_NULLPTR;
|
|
*pdata = strl->mRawData;
|
|
return WFERROR_OK;
|
|
}
|
|
|
|
WFERROR WFString_SetData(WFString* strl, wchar_t* data) {
|
|
// get length and resize buffer
|
|
WFERROR ec;
|
|
uint32_t size = wcslen(data);
|
|
ec = WFString_Resize(strl, size);
|
|
if (ec != WFERROR_OK) return ec;
|
|
|
|
// copy data
|
|
if (size != 0)
|
|
memcpy(strl->mRawData, data, sizeof(wchar_t) * size);
|
|
|
|
return WFERROR_OK;
|
|
}
|
|
|
|
WFERROR WFString_GetLength(WFString* strl, uint32_t* len) {
|
|
if (strl == NULL) return WFERROR_NULLPTR;
|
|
*len = strl->mLength;
|
|
return WFERROR_OK;
|
|
}
|
|
|
|
WFERROR WFString_Printf(WFString* strl, wchar_t* format, ...) {
|
|
return WFERROR();
|
|
}
|
|
|
|
#pragma endregion
|
|
|
|
#pragma region WFLinkedList
|
|
|
|
WFERROR WFLinkedList_Alloc(WFLinkedList** list) {
|
|
*list = WFNEW(WFLinkedList);
|
|
if (*list == NULL) return WFERROR_ALLOC;
|
|
return WFERROR_OK;
|
|
}
|
|
|
|
WFERROR WFLinkedList_Free(WFLinkedList* list) {
|
|
return WFLinkedList_Free_Full(list, NULL);
|
|
}
|
|
|
|
WFERROR WFLinkedList_Free_Full(WFLinkedList* list, WFLinkedListNode_FreeDataFunc free_func) {
|
|
if (list == NULL) return WFERROR_OK;
|
|
|
|
// iterate full list and remove data and node
|
|
WFLinkedListNode* node = list->mHead, * free_node = NULL;
|
|
while (node != NULL) {
|
|
// free raw data
|
|
if (free_func != NULL) (*free_func)(node->mRawData);
|
|
// move to next node and free current node
|
|
free_node = node;
|
|
node = node->mNext;
|
|
free(free_node);
|
|
}
|
|
|
|
// free list self
|
|
free(list);
|
|
|
|
return WFERROR_OK;
|
|
}
|
|
|
|
WFERROR WFLinkedList_Add(WFLinkedList* list, void* data) {
|
|
if (list == NULL) return WFERROR_NULLPTR;
|
|
|
|
WFLinkedListNode* new_item = WFNEW(WFLinkedListNode);
|
|
if (new_item == NULL) return WFERROR_ALLOC;
|
|
new_item->mNext = NULL;
|
|
new_item->mRawData = data;
|
|
|
|
if (list->mLength == 0) {
|
|
list->mHead = list->mTail = new_item;
|
|
} else {
|
|
list->mTail->mNext = new_item;
|
|
list->mTail = new_item;
|
|
}
|
|
++list->mLength;
|
|
return WFERROR_OK;
|
|
}
|
|
|
|
WFERROR WFLinkedList_NodeIterator(WFLinkedList* list, WFLinkedListNode** node_ptr) {
|
|
if (list == NULL) return WFERROR_NULLPTR;
|
|
|
|
if (*node_ptr == NULL) {
|
|
// if node_ptr is NULL, it mean that wo should iterate this list from head
|
|
*node_ptr = list->mHead;
|
|
} else {
|
|
// otherwise, move to next node
|
|
*node_ptr = (*node_ptr)->mNext;
|
|
}
|
|
|
|
// if the header is null, return end of tail error to notice caller stop iterate
|
|
if (*node_ptr == NULL) return WFERROR_END_OF_TAIL;
|
|
else return WFERROR_OK;
|
|
}
|
|
|
|
WFERROR WFLinkedListNode_GetData(WFLinkedListNode* node, void** pdata) {
|
|
if (node == NULL) return WFERROR_NULLPTR;
|
|
|
|
*pdata = node->mRawData;
|
|
return WFERROR_OK;
|
|
}
|
|
|
|
#pragma endregion
|