From 3ab26925e0a4a40cefa17843f09b9f6f89d7f8bb Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Sun, 15 May 2022 17:09:21 +0800 Subject: [PATCH] add some utils funcs --- wfassoc/utils.c | 166 ++++++++++++++++++++++++++++++++ wfassoc/utils.h | 44 +++++++++ wfassoc/wfassoc.h | 4 +- wfassoc/wfassoc.vcxproj | 2 + wfassoc/wfassoc.vcxproj.filters | 6 ++ 5 files changed, 221 insertions(+), 1 deletion(-) create mode 100644 wfassoc/utils.c create mode 100644 wfassoc/utils.h diff --git a/wfassoc/utils.c b/wfassoc/utils.c new file mode 100644 index 0000000..ab9c7e6 --- /dev/null +++ b/wfassoc/utils.c @@ -0,0 +1,166 @@ +#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 diff --git a/wfassoc/utils.h b/wfassoc/utils.h new file mode 100644 index 0000000..6809560 --- /dev/null +++ b/wfassoc/utils.h @@ -0,0 +1,44 @@ +#if !defined(_YYCDLL_WFASSOC_UTILS_H__IMPORTED_) +#define _YYCDLL_WFASSOC_UTILS_H__IMPORTED_ + +#include "wfassoc.h" +#include + +typedef struct _WFString { + wchar_t* mRawData; + uint32_t mLength; + uint32_t mCapacity; + + uint32_t mRealLength; + uint32_t mRealCapacity; +}WFString; + +WFERROR WFString_Alloc(WFString** strl, wchar_t* raw_data); +WFERROR WFString_Alloc(WFString** strl, char* raw_data, BOOL is_utf8); +WFERROR WFString_Alloc(WFString** strl, uint32_t size); +WFERROR WFString_Free(WFString* strl); +WFERROR WFString_Resize(WFString* strl, uint32_t new_size); +WFERROR WFString_GetData(WFString* strl, wchar_t** pdata); +WFERROR WFString_SetData(WFString* strl, wchar_t* data); +WFERROR WFString_GetLength(WFString* strl, uint32_t* len); +WFERROR WFString_Printf(WFString* strl, wchar_t* format, ...); + +typedef WFERROR(*WFLinkedListNode_FreeDataFunc)(void* data); +typedef struct _WFLinkedListNode { + void* mRawData; + WFLinkedListNode* mNext; +}WFLinkedListNode; +typedef struct _WFLinkedList { + WFLinkedListNode* mHead; + WFLinkedListNode* mTail; + uint32_t mLength; +}WFLinkedList; + +WFERROR WFLinkedList_Alloc(WFLinkedList** list); +WFERROR WFLinkedList_Free(WFLinkedList* list); +WFERROR WFLinkedList_Free_Full(WFLinkedList* list, WFLinkedListNode_FreeDataFunc free_func); +WFERROR WFLinkedList_Add(WFLinkedList* list, void* data); +WFERROR WFLinkedList_NodeIterator(WFLinkedList* list, WFLinkedListNode** node_ptr); +WFERROR WFLinkedListNode_GetData(WFLinkedListNode* node, void** pdata); + +#endif \ No newline at end of file diff --git a/wfassoc/wfassoc.h b/wfassoc/wfassoc.h index be46107..37b0b5a 100644 --- a/wfassoc/wfassoc.h +++ b/wfassoc/wfassoc.h @@ -20,6 +20,7 @@ // useful macro +#define WFNEW(type) ((type*)malloc(sizeof(type))) #define WFVERSION 0 #define WFSUCCESS(expr) (!expr) #define WFFAILED(expr) expr @@ -50,7 +51,8 @@ typedef enum _WFERROR { WFERROR_ENCODING = 4, WFERROR_WIN32_ERROR = 5, WFERROR_ASSERT_ERROR = 6, - WFERROR_ALLOC = 7 + WFERROR_ALLOC = 7, + WFERROR_END_OF_TAIL = 8 }WFERROR; /// diff --git a/wfassoc/wfassoc.vcxproj b/wfassoc/wfassoc.vcxproj index 8ac720e..4ed7e3a 100644 --- a/wfassoc/wfassoc.vcxproj +++ b/wfassoc/wfassoc.vcxproj @@ -147,9 +147,11 @@ + + diff --git a/wfassoc/wfassoc.vcxproj.filters b/wfassoc/wfassoc.vcxproj.filters index 0004cee..706fb9d 100644 --- a/wfassoc/wfassoc.vcxproj.filters +++ b/wfassoc/wfassoc.vcxproj.filters @@ -18,11 +18,17 @@ 头文件 + + 头文件 + 源文件 + + 源文件 +