Skip to content

Commit

Permalink
perf(gui): save classes and status with strlist
Browse files Browse the repository at this point in the history
  • Loading branch information
lc-soft committed Mar 4, 2019
1 parent c60c26c commit f611936
Show file tree
Hide file tree
Showing 15 changed files with 373 additions and 267 deletions.
2 changes: 2 additions & 0 deletions build/windows/LCUI/LCUI.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@
<ClInclude Include="..\..\..\include\LCUI\util\rbtree.h" />
<ClInclude Include="..\..\..\include\LCUI\util\rect.h" />
<ClInclude Include="..\..\..\include\LCUI\util\string.h" />
<ClInclude Include="..\..\..\include\LCUI\util\strlist.h" />
<ClInclude Include="..\..\..\include\LCUI\util\strpool.h" />
<ClInclude Include="..\..\..\include\LCUI\util\task.h" />
<ClInclude Include="..\..\..\include\LCUI\util\time.h" />
Expand Down Expand Up @@ -408,6 +409,7 @@
<ClCompile Include="..\..\..\src\platform\windows\windows_keyboard.c" />
<ClCompile Include="..\..\..\src\platform\windows\windows_mouse.c" />
<ClCompile Include="..\..\..\src\util\charset.c" />
<ClCompile Include="..\..\..\src\util\strlist.c" />
<ClCompile Include="..\..\..\src\util\strpool.c" />
<ClCompile Include="..\..\..\src\util\task.c" />
<ClCompile Include="..\..\..\src\util\uri.c" />
Expand Down
6 changes: 6 additions & 0 deletions build/windows/LCUI/LCUI.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,9 @@
<ClInclude Include="..\..\..\include\LCUI\util\strpool.h">
<Filter>头文件\LCUI\util</Filter>
</ClInclude>
<ClInclude Include="..\..\..\include\LCUI\util\strlist.h">
<Filter>头文件\LCUI\util</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\..\src\draw\border.c">
Expand Down Expand Up @@ -533,6 +536,9 @@
<ClCompile Include="..\..\..\src\util\strpool.c">
<Filter>源文件\util</Filter>
</ClCompile>
<ClCompile Include="..\..\..\src\util\strlist.c">
<Filter>源文件\util</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Text Include="..\..\..\LICENSE.TXT" />
Expand Down
5 changes: 3 additions & 2 deletions include/LCUI/gui/widget_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#ifndef LCUI_WIDGET_BASE_H
#define LCUI_WIDGET_BASE_H

#include <LCUI/util/strlist.h>
#include <LCUI/gui/css_library.h>

LCUI_BEGIN_HEADER
Expand Down Expand Up @@ -178,8 +179,8 @@ typedef struct LCUI_WidgetRec_ {
size_t index; /**< 部件索引位置 */
char *id; /**< ID */
char *type; /**< 类型 */
char **classes; /**< 类列表 */
char **status; /**< 状态列表 */
strlist_t classes; /**< 类列表 */
strlist_t status; /**< 状态列表 */
wchar_t *title; /**< 标题 */
LCUI_Rect2F padding; /**< 内边距框 */
LCUI_Rect2F margin; /**< 外边距框 */
Expand Down
1 change: 1 addition & 0 deletions include/LCUI/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <LCUI/util/steptimer.h>
#include <LCUI/util/string.h>
#include <LCUI/util/strpool.h>
#include <LCUI/util/strlist.h>
#include <LCUI/util/parse.h>
#include <LCUI/util/event.h>
#include <LCUI/util/logger.h>
Expand Down
2 changes: 1 addition & 1 deletion include/LCUI/util/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ AUTOMAKE_OPTIONS=foreign
# Headers to install
pkginclude_HEADERS = dict.h rbtree.h linkedlist.h string.h rect.h dirent.h \
time.h event.h steptimer.h parse.h logger.h math.h task.h uri.h charset.h \
strpool.h
strpool.h strlist.h
pkgincludedir=$(prefix)/include/LCUI/util
33 changes: 0 additions & 33 deletions include/LCUI/util/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,39 +87,6 @@ LCUI_API int cmdsplit(const char *cmd, char ***outargv);
*/
LCUI_API int strsplit(const char *instr, const char *sep, char ***outstrs);

/**
* 向字符串组添加字符串
* @param[in][out] strlist 字符串组
* @param[in] str 字符串
*/
LCUI_API int strsadd(char ***strlist, const char *str);

/**
* 判断字符串组中是否包含指定字符串
* @param[in][out] strlist 字符串组
* @param[in] str 字符串
* @returns 如果包含则返回 1, 否则返回 0
*/
LCUI_API int strshas(char **strlist, const char *str);

/**
* 从字符串组中删除指定字符串
* @param[in][out] strlist 字符串组
* @param[in] str 字符串
* @returns 如果删除成功则返回 1, 否则返回 0
*/
LCUI_API int strsdel(char ***strlist, const char *str);

/**
* 向已排序的字符串组添加字符串
* @param[in][out] strlist 字符串组
* @param[in] str 字符串
*/
LCUI_API int sortedstrsadd(char ***strlist, const char *str);

/** 释放字符串组 */
LCUI_API void freestrs(char **strs);

LCUI_END_HEADER

#endif
75 changes: 75 additions & 0 deletions include/LCUI/util/strlist.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* strlist.h -- string list
*
* Copyright (c) 2019, Liu chao <lc-soft@live.cn> All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of LCUI nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef LCUI_UTIL_STRLIST_H
#define LCUI_UTIL_STRLIST_H

typedef char** strlist_t;

LCUI_API int sortedstrlist_add(strlist_t *strlist, const char *str);

LCUI_API int strlist_add_one(strlist_t *strlist, const char *str);

LCUI_API int strlist_remove_one(strlist_t *strlist, const char *str);

/**
* 向字符串组添加字符串
* @param[in][out] strlist 字符串组
* @param[in] str 字符串
*/
LCUI_API int strlist_add(strlist_t *strlist, const char *str);

/**
* 判断字符串组中是否包含指定字符串
* @param[in][out] strlist 字符串组
* @param[in] str 字符串
* @returns 如果包含则返回 1, 否则返回 0
*/
LCUI_API int strlist_has(strlist_t strlist, const char *str);

/**
* 从字符串组中移除指定字符串
* @param[in][out] strlist 字符串组
* @param[in] str 字符串
* @returns 如果删除成功则返回 1, 否则返回 0
*/
LCUI_API int strlist_remove(strlist_t *strlist, const char *str);

/**
* 向已排序的字符串组添加字符串
* @param[in][out] strlist 字符串组
* @param[in] str 字符串
*/
LCUI_API int sortedstrlist_add(strlist_t *strlist, const char *str);

/** 释放字符串组 */
LCUI_API void strlist_free(strlist_t strs);

#endif
14 changes: 7 additions & 7 deletions src/gui/css_library.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* css_library.c -- CSS library operation module.
*
* Copyright (c) 2018, Liu chao <lc-soft@live.cn> All rights reserved.
Expand Down Expand Up @@ -367,12 +367,12 @@ static void SelectorNode_Copy(LCUI_SelectorNode dst, LCUI_SelectorNode src)
dst->fullname = src->fullname ? strdup2(src->fullname) : NULL;
if (src->classes) {
for (i = 0; src->classes[i]; ++i) {
sortedstrsadd(&dst->classes, src->classes[i]);
sortedstrlist_add(&dst->classes, src->classes[i]);
}
}
if (src->status) {
for (i = 0; src->status[i]; ++i) {
sortedstrsadd(&dst->status, src->status[i]);
sortedstrlist_add(&dst->status, src->status[i]);
}
}
}
Expand All @@ -388,11 +388,11 @@ void SelectorNode_Delete(LCUI_SelectorNode node)
node->id = NULL;
}
if (node->classes) {
freestrs(node->classes);
strlist_free(node->classes);
node->classes = NULL;
}
if (node->status) {
freestrs(node->status);
strlist_free(node->status);
node->status = NULL;
}
if (node->fullname) {
Expand Down Expand Up @@ -740,12 +740,12 @@ static int SelectorNode_Save(LCUI_SelectorNode node, const char *name, int len,
node->type = str;
return TYPE_RANK;
case ':':
if (sortedstrsadd(&node->status, name) == 0) {
if (sortedstrlist_add(&node->status, name) == 0) {
return PCLASS_RANK;
}
break;
case '.':
if (sortedstrsadd(&node->classes, name) == 0) {
if (sortedstrlist_add(&node->classes, name) == 0) {
return CLASS_RANK;
}
break;
Expand Down
12 changes: 6 additions & 6 deletions src/gui/widget_class.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@

int Widget_AddClass(LCUI_Widget w, const char *class_name)
{
if (strshas(w->classes, class_name)) {
if (strlist_has(w->classes, class_name)) {
return 1;
}
if (strsadd(&w->classes, class_name) <= 0) {
if (strlist_add(&w->classes, class_name) <= 0) {
return 0;
}
Widget_HandleChildrenStyleChange(w, 0, class_name);
Expand All @@ -49,17 +49,17 @@ int Widget_AddClass(LCUI_Widget w, const char *class_name)

LCUI_BOOL Widget_HasClass(LCUI_Widget w, const char *class_name)
{
if (strshas(w->classes, class_name)) {
if (strlist_has(w->classes, class_name)) {
return TRUE;
}
return FALSE;
}

int Widget_RemoveClass(LCUI_Widget w, const char *class_name)
{
if (strshas(w->classes, class_name)) {
if (strlist_has(w->classes, class_name)) {
Widget_HandleChildrenStyleChange(w, 0, class_name);
strsdel(&w->classes, class_name);
strlist_remove(&w->classes, class_name);
Widget_UpdateStyle(w, TRUE);
return 1;
}
Expand All @@ -69,7 +69,7 @@ int Widget_RemoveClass(LCUI_Widget w, const char *class_name)
void Widget_DestroyClasses(LCUI_Widget w)
{
if (w->classes) {
freestrs(w->classes);
strlist_free(w->classes);
}
w->classes = NULL;
}
12 changes: 6 additions & 6 deletions src/gui/widget_status.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@

int Widget_AddStatus(LCUI_Widget w, const char *status_name)
{
if (strshas(w->status, status_name)) {
if (strlist_has(w->status, status_name)) {
return 0;
}
if (strsadd(&w->status, status_name) <= 0) {
if (strlist_add(&w->status, status_name) <= 0) {
return 0;
}
Widget_HandleChildrenStyleChange(w, 1, status_name);
Expand All @@ -50,17 +50,17 @@ int Widget_AddStatus(LCUI_Widget w, const char *status_name)

LCUI_BOOL Widget_HasStatus(LCUI_Widget w, const char *status_name)
{
if (strshas(w->status, status_name)) {
if (strlist_has(w->status, status_name)) {
return TRUE;
}
return FALSE;
}

int Widget_RemoveStatus(LCUI_Widget w, const char *status_name)
{
if (strshas(w->status, status_name)) {
if (strlist_has(w->status, status_name)) {
Widget_HandleChildrenStyleChange(w, 1, status_name);
strsdel(&w->status, status_name);
strlist_remove(&w->status, status_name);
Widget_UpdateStyle(w, TRUE);
return 1;
}
Expand Down Expand Up @@ -102,7 +102,7 @@ void Widget_SetDisabled(LCUI_Widget w, LCUI_BOOL disabled)
void Widget_DestroyStatus(LCUI_Widget w)
{
if (w->status) {
freestrs(w->status);
strlist_free(w->status);
}
w->status = NULL;
}
10 changes: 7 additions & 3 deletions src/gui/widget_style.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ LCUI_SelectorNode Widget_GetSelectorNode(LCUI_Widget w)
sn->type = strdup2(w->type);
}
for (i = 0; w->classes && w->classes[i]; ++i) {
sortedstrsadd(&sn->classes, w->classes[i]);
sortedstrlist_add(&sn->classes, w->classes[i]);
}
for (i = 0; w->status && w->status[i]; ++i) {
sortedstrsadd(&sn->status, w->status[i]);
sortedstrlist_add(&sn->status, w->status[i]);
}
SelectorNode_Update(sn);
return sn;
Expand All @@ -94,6 +94,7 @@ LCUI_Selector Widget_GetSelector(LCUI_Widget w)
LCUI_Selector s;
LCUI_Widget parent;
LinkedListNode *node;

s = Selector(NULL);
LinkedList_Init(&list);
for (parent = w; parent; parent = parent->parent) {
Expand Down Expand Up @@ -183,7 +184,10 @@ int Widget_HandleChildrenStyleChange(LCUI_Widget w, int type, const char *name)
if (count > 0) {
Widget_AddTaskForChildren(w, LCUI_WTASK_REFRESH_STYLE);
}
freestrs(names);
for (i = 0; names[i]; ++i) {
free(names[i]);
}
free(names);
return count;
}

Expand Down
4 changes: 2 additions & 2 deletions src/util/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ AUTOMAKE_OPTIONS=foreign
AM_CFLAGS = -I$(abs_top_srcdir)/include $(CODE_COVERAGE_CFLAGS)
noinst_LTLIBRARIES = libutil.la
libutil_la_SOURCES = rbtree.c dict.c linkedlist.c time.c event.c rect.c \
string.c strpool.c dirent.c parse.c steptimer.c logger.c math.c task.c uri.c \
charset.c
string.c strlist.c strpool.c dirent.c parse.c steptimer.c logger.c math.c \
task.c uri.c charset.c
Loading

0 comments on commit f611936

Please sign in to comment.