forked from RepoMirrors/bemenu
Make asserts and ifs more consistent.
This commit is contained in:
parent
cff1f37f97
commit
4d920ad9e4
@ -17,22 +17,22 @@ static ptrdiff_t getLine(char **outLine, size_t *outAllocated, FILE *stream)
|
|||||||
size_t len = 0, allocated;
|
size_t len = 0, allocated;
|
||||||
char *s, *buffer;
|
char *s, *buffer;
|
||||||
|
|
||||||
assert(outLine != NULL);
|
assert(outLine);
|
||||||
assert(outAllocated != NULL);
|
assert(outAllocated);
|
||||||
|
|
||||||
if (stream == NULL || feof(stream) || ferror(stream))
|
if (!stream || feof(stream) || ferror(stream))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
allocated = *outAllocated;
|
allocated = *outAllocated;
|
||||||
buffer = *outLine;
|
buffer = *outLine;
|
||||||
|
|
||||||
if (buffer == NULL || allocated == 0) {
|
if (!buffer || allocated == 0) {
|
||||||
if (!(buffer = calloc(1, (allocated = 1024) + 1)))
|
if (!(buffer = calloc(1, (allocated = 1024) + 1)))
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (s = buffer;;) {
|
for (s = buffer;;) {
|
||||||
if (fgets(s, allocated - (s - buffer), stream) == NULL) {
|
if (!fgets(s, allocated - (s - buffer), stream)) {
|
||||||
*outAllocated = allocated;
|
*outAllocated = allocated;
|
||||||
*outLine = buffer;
|
*outLine = buffer;
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -139,7 +139,7 @@ static void _bmDrawCursesEndWin(void)
|
|||||||
|
|
||||||
static bmKey _bmDrawCursesGetKey(unsigned int *unicode)
|
static bmKey _bmDrawCursesGetKey(unsigned int *unicode)
|
||||||
{
|
{
|
||||||
assert(unicode != NULL);
|
assert(unicode);
|
||||||
*unicode = 0;
|
*unicode = 0;
|
||||||
|
|
||||||
if (!curses.stdscr)
|
if (!curses.stdscr)
|
||||||
|
12
lib/filter.c
12
lib/filter.c
@ -17,9 +17,9 @@
|
|||||||
*/
|
*/
|
||||||
bmItem** _bmFilterDmenu(bmMenu *menu, unsigned int *count, unsigned int *selected)
|
bmItem** _bmFilterDmenu(bmMenu *menu, unsigned int *count, unsigned int *selected)
|
||||||
{
|
{
|
||||||
assert(menu != NULL);
|
assert(menu);
|
||||||
assert(count != NULL);
|
assert(count);
|
||||||
assert(selected != NULL);
|
assert(selected);
|
||||||
*count = *selected = 0;
|
*count = *selected = 0;
|
||||||
|
|
||||||
/* FIXME: not real dmenu like filtering at all */
|
/* FIXME: not real dmenu like filtering at all */
|
||||||
@ -51,9 +51,9 @@ bmItem** _bmFilterDmenu(bmMenu *menu, unsigned int *count, unsigned int *selecte
|
|||||||
*/
|
*/
|
||||||
bmItem** _bmFilterDmenuCaseInsensitive(bmMenu *menu, unsigned int *count, unsigned int *selected)
|
bmItem** _bmFilterDmenuCaseInsensitive(bmMenu *menu, unsigned int *count, unsigned int *selected)
|
||||||
{
|
{
|
||||||
assert(menu != NULL);
|
assert(menu);
|
||||||
assert(count != NULL);
|
assert(count);
|
||||||
assert(selected != NULL);
|
assert(selected);
|
||||||
*count = *selected = 0;
|
*count = *selected = 0;
|
||||||
|
|
||||||
/* FIXME: stub */
|
/* FIXME: stub */
|
||||||
|
@ -31,7 +31,7 @@ bmItem* bmItemNew(const char *text)
|
|||||||
*/
|
*/
|
||||||
void bmItemFree(bmItem *item)
|
void bmItemFree(bmItem *item)
|
||||||
{
|
{
|
||||||
assert(item != NULL);
|
assert(item);
|
||||||
|
|
||||||
if (item->text)
|
if (item->text)
|
||||||
free(item->text);
|
free(item->text);
|
||||||
@ -47,7 +47,7 @@ void bmItemFree(bmItem *item)
|
|||||||
*/
|
*/
|
||||||
int bmItemSetText(bmItem *item, const char *text)
|
int bmItemSetText(bmItem *item, const char *text)
|
||||||
{
|
{
|
||||||
assert(item != NULL);
|
assert(item);
|
||||||
|
|
||||||
char *copy = NULL;
|
char *copy = NULL;
|
||||||
if (text && !(copy = _bmStrdup(text)))
|
if (text && !(copy = _bmStrdup(text)))
|
||||||
@ -68,7 +68,7 @@ int bmItemSetText(bmItem *item, const char *text)
|
|||||||
*/
|
*/
|
||||||
const char* bmItemGetText(const bmItem *item)
|
const char* bmItemGetText(const bmItem *item)
|
||||||
{
|
{
|
||||||
assert(item != NULL);
|
assert(item);
|
||||||
return item->text;
|
return item->text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
42
lib/menu.c
42
lib/menu.c
@ -15,7 +15,7 @@ static bmItem** (*filterFunc[BM_FILTER_MODE_LAST])(bmMenu *menu, unsigned int *c
|
|||||||
|
|
||||||
static void _bmMenuFilter(bmMenu *menu)
|
static void _bmMenuFilter(bmMenu *menu)
|
||||||
{
|
{
|
||||||
assert(menu != NULL);
|
assert(menu);
|
||||||
|
|
||||||
if (menu->filteredItems)
|
if (menu->filteredItems)
|
||||||
free(menu->filteredItems);
|
free(menu->filteredItems);
|
||||||
@ -90,7 +90,7 @@ bmMenu* bmMenuNew(bmDrawMode drawMode)
|
|||||||
*/
|
*/
|
||||||
void bmMenuFree(bmMenu *menu)
|
void bmMenuFree(bmMenu *menu)
|
||||||
{
|
{
|
||||||
assert(menu != NULL);
|
assert(menu);
|
||||||
|
|
||||||
if (menu->renderApi.free)
|
if (menu->renderApi.free)
|
||||||
menu->renderApi.free();
|
menu->renderApi.free();
|
||||||
@ -112,7 +112,7 @@ void bmMenuFree(bmMenu *menu)
|
|||||||
*/
|
*/
|
||||||
void bmMenuFreeItems(bmMenu *menu)
|
void bmMenuFreeItems(bmMenu *menu)
|
||||||
{
|
{
|
||||||
assert(menu != NULL);
|
assert(menu);
|
||||||
|
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
for (i = 0; i < menu->itemsCount; ++i)
|
for (i = 0; i < menu->itemsCount; ++i)
|
||||||
@ -131,7 +131,7 @@ void bmMenuFreeItems(bmMenu *menu)
|
|||||||
*/
|
*/
|
||||||
void bmMenuSetFilterMode(bmMenu *menu, bmFilterMode mode)
|
void bmMenuSetFilterMode(bmMenu *menu, bmFilterMode mode)
|
||||||
{
|
{
|
||||||
assert(menu != NULL);
|
assert(menu);
|
||||||
|
|
||||||
bmFilterMode oldMode = mode;
|
bmFilterMode oldMode = mode;
|
||||||
menu->filterMode = (mode >= BM_FILTER_MODE_LAST ? BM_FILTER_MODE_DMENU : mode);
|
menu->filterMode = (mode >= BM_FILTER_MODE_LAST ? BM_FILTER_MODE_DMENU : mode);
|
||||||
@ -148,7 +148,7 @@ void bmMenuSetFilterMode(bmMenu *menu, bmFilterMode mode)
|
|||||||
*/
|
*/
|
||||||
bmFilterMode bmMenuGetFilterMode(const bmMenu *menu)
|
bmFilterMode bmMenuGetFilterMode(const bmMenu *menu)
|
||||||
{
|
{
|
||||||
assert(menu != NULL);
|
assert(menu);
|
||||||
return menu->filterMode;
|
return menu->filterMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,7 +160,7 @@ bmFilterMode bmMenuGetFilterMode(const bmMenu *menu)
|
|||||||
*/
|
*/
|
||||||
int bmMenuSetTitle(bmMenu *menu, const char *title)
|
int bmMenuSetTitle(bmMenu *menu, const char *title)
|
||||||
{
|
{
|
||||||
assert(menu != NULL);
|
assert(menu);
|
||||||
|
|
||||||
char *copy = NULL;
|
char *copy = NULL;
|
||||||
if (title && !(copy = _bmStrdup(title)))
|
if (title && !(copy = _bmStrdup(title)))
|
||||||
@ -181,7 +181,7 @@ int bmMenuSetTitle(bmMenu *menu, const char *title)
|
|||||||
*/
|
*/
|
||||||
const char* bmMenuGetTitle(const bmMenu *menu)
|
const char* bmMenuGetTitle(const bmMenu *menu)
|
||||||
{
|
{
|
||||||
assert(menu != NULL);
|
assert(menu);
|
||||||
return menu->title;
|
return menu->title;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -195,8 +195,8 @@ const char* bmMenuGetTitle(const bmMenu *menu)
|
|||||||
*/
|
*/
|
||||||
int bmMenuAddItemAt(bmMenu *menu, bmItem *item, unsigned int index)
|
int bmMenuAddItemAt(bmMenu *menu, bmItem *item, unsigned int index)
|
||||||
{
|
{
|
||||||
assert(menu != NULL);
|
assert(menu);
|
||||||
assert(item != NULL);
|
assert(item);
|
||||||
|
|
||||||
if (menu->itemsCount >= menu->allocatedCount && !_bmMenuGrowItems(menu))
|
if (menu->itemsCount >= menu->allocatedCount && !_bmMenuGrowItems(menu))
|
||||||
return 0;
|
return 0;
|
||||||
@ -232,7 +232,7 @@ int bmMenuAddItem(bmMenu *menu, bmItem *item)
|
|||||||
*/
|
*/
|
||||||
int bmMenuRemoveItemAt(bmMenu *menu, unsigned int index)
|
int bmMenuRemoveItemAt(bmMenu *menu, unsigned int index)
|
||||||
{
|
{
|
||||||
assert(menu != NULL);
|
assert(menu);
|
||||||
|
|
||||||
unsigned int i = index;
|
unsigned int i = index;
|
||||||
if (i >= menu->itemsCount)
|
if (i >= menu->itemsCount)
|
||||||
@ -251,8 +251,8 @@ int bmMenuRemoveItemAt(bmMenu *menu, unsigned int index)
|
|||||||
*/
|
*/
|
||||||
int bmMenuRemoveItem(bmMenu *menu, bmItem *item)
|
int bmMenuRemoveItem(bmMenu *menu, bmItem *item)
|
||||||
{
|
{
|
||||||
assert(menu != NULL);
|
assert(menu);
|
||||||
assert(item != NULL);
|
assert(item);
|
||||||
|
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
for (i = 0; i < menu->itemsCount && menu->items[i] != item; ++i);
|
for (i = 0; i < menu->itemsCount && menu->items[i] != item; ++i);
|
||||||
@ -267,7 +267,7 @@ int bmMenuRemoveItem(bmMenu *menu, bmItem *item)
|
|||||||
*/
|
*/
|
||||||
bmItem* bmMenuGetSelectedItem(const bmMenu *menu)
|
bmItem* bmMenuGetSelectedItem(const bmMenu *menu)
|
||||||
{
|
{
|
||||||
assert(menu != NULL);
|
assert(menu);
|
||||||
|
|
||||||
unsigned int count;
|
unsigned int count;
|
||||||
bmItem **items = bmMenuGetFilteredItems(menu, &count);
|
bmItem **items = bmMenuGetFilteredItems(menu, &count);
|
||||||
@ -287,7 +287,7 @@ bmItem* bmMenuGetSelectedItem(const bmMenu *menu)
|
|||||||
*/
|
*/
|
||||||
bmItem** bmMenuGetItems(const bmMenu *menu, unsigned int *nmemb)
|
bmItem** bmMenuGetItems(const bmMenu *menu, unsigned int *nmemb)
|
||||||
{
|
{
|
||||||
assert(menu != NULL);
|
assert(menu);
|
||||||
|
|
||||||
if (nmemb)
|
if (nmemb)
|
||||||
*nmemb = menu->itemsCount;
|
*nmemb = menu->itemsCount;
|
||||||
@ -307,7 +307,7 @@ bmItem** bmMenuGetItems(const bmMenu *menu, unsigned int *nmemb)
|
|||||||
*/
|
*/
|
||||||
bmItem** bmMenuGetFilteredItems(const bmMenu *menu, unsigned int *nmemb)
|
bmItem** bmMenuGetFilteredItems(const bmMenu *menu, unsigned int *nmemb)
|
||||||
{
|
{
|
||||||
assert(menu != NULL);
|
assert(menu);
|
||||||
|
|
||||||
if (nmemb)
|
if (nmemb)
|
||||||
*nmemb = (menu->filteredItems ? menu->filteredCount : menu->itemsCount);
|
*nmemb = (menu->filteredItems ? menu->filteredCount : menu->itemsCount);
|
||||||
@ -326,9 +326,9 @@ bmItem** bmMenuGetFilteredItems(const bmMenu *menu, unsigned int *nmemb)
|
|||||||
*/
|
*/
|
||||||
int bmMenuSetItems(bmMenu *menu, const bmItem **items, unsigned int nmemb)
|
int bmMenuSetItems(bmMenu *menu, const bmItem **items, unsigned int nmemb)
|
||||||
{
|
{
|
||||||
assert(menu != NULL);
|
assert(menu);
|
||||||
|
|
||||||
if (items == NULL || nmemb == 0) {
|
if (!items || nmemb == 0) {
|
||||||
bmMenuFreeItems(menu);
|
bmMenuFreeItems(menu);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -353,7 +353,7 @@ int bmMenuSetItems(bmMenu *menu, const bmItem **items, unsigned int nmemb)
|
|||||||
*/
|
*/
|
||||||
void bmMenuRender(const bmMenu *menu)
|
void bmMenuRender(const bmMenu *menu)
|
||||||
{
|
{
|
||||||
assert(menu != NULL);
|
assert(menu);
|
||||||
|
|
||||||
if (menu->renderApi.render)
|
if (menu->renderApi.render)
|
||||||
menu->renderApi.render(menu);
|
menu->renderApi.render(menu);
|
||||||
@ -370,8 +370,8 @@ void bmMenuRender(const bmMenu *menu)
|
|||||||
*/
|
*/
|
||||||
bmKey bmMenuGetKey(bmMenu *menu, unsigned int *unicode)
|
bmKey bmMenuGetKey(bmMenu *menu, unsigned int *unicode)
|
||||||
{
|
{
|
||||||
assert(menu != NULL);
|
assert(menu);
|
||||||
assert(unicode != NULL);
|
assert(unicode);
|
||||||
|
|
||||||
*unicode = 0;
|
*unicode = 0;
|
||||||
bmKey key = BM_KEY_NONE;
|
bmKey key = BM_KEY_NONE;
|
||||||
@ -390,7 +390,7 @@ bmKey bmMenuGetKey(bmMenu *menu, unsigned int *unicode)
|
|||||||
*/
|
*/
|
||||||
bmRunResult bmMenuRunWithKey(bmMenu *menu, bmKey key, unsigned int unicode)
|
bmRunResult bmMenuRunWithKey(bmMenu *menu, bmKey key, unsigned int unicode)
|
||||||
{
|
{
|
||||||
assert(menu != NULL);
|
assert(menu);
|
||||||
char *oldFilter = _bmStrdup(menu->filter);
|
char *oldFilter = _bmStrdup(menu->filter);
|
||||||
unsigned int itemsCount = (menu->filteredItems ? menu->filteredCount : menu->itemsCount);
|
unsigned int itemsCount = (menu->filteredItems ? menu->filteredCount : menu->itemsCount);
|
||||||
|
|
||||||
|
19
lib/util.c
19
lib/util.c
@ -18,6 +18,8 @@
|
|||||||
*/
|
*/
|
||||||
char* _bmStrdup(const char *string)
|
char* _bmStrdup(const char *string)
|
||||||
{
|
{
|
||||||
|
assert(string);
|
||||||
|
|
||||||
size_t len = strlen(string);
|
size_t len = strlen(string);
|
||||||
if (len == 0)
|
if (len == 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -41,6 +43,8 @@ char* _bmStrdup(const char *string)
|
|||||||
*/
|
*/
|
||||||
bmItem** _bmShrinkItemList(bmItem ***list, size_t osize, size_t nsize)
|
bmItem** _bmShrinkItemList(bmItem ***list, size_t osize, size_t nsize)
|
||||||
{
|
{
|
||||||
|
assert(list);
|
||||||
|
|
||||||
if (nsize >= osize)
|
if (nsize >= osize)
|
||||||
return *list;
|
return *list;
|
||||||
|
|
||||||
@ -62,8 +66,7 @@ bmItem** _bmShrinkItemList(bmItem ***list, size_t osize, size_t nsize)
|
|||||||
*/
|
*/
|
||||||
int _bmUtf8StringScreenWidth(const char *string)
|
int _bmUtf8StringScreenWidth(const char *string)
|
||||||
{
|
{
|
||||||
if (!string)
|
assert(string);
|
||||||
return 0;
|
|
||||||
|
|
||||||
int num_char = mbstowcs(NULL, string, 0) + 1;
|
int num_char = mbstowcs(NULL, string, 0) + 1;
|
||||||
wchar_t *wstring = malloc((num_char + 1) * sizeof (wstring[0]));
|
wchar_t *wstring = malloc((num_char + 1) * sizeof (wstring[0]));
|
||||||
@ -87,7 +90,7 @@ int _bmUtf8StringScreenWidth(const char *string)
|
|||||||
*/
|
*/
|
||||||
size_t _bmUtf8RuneNext(const char *string, size_t start)
|
size_t _bmUtf8RuneNext(const char *string, size_t start)
|
||||||
{
|
{
|
||||||
assert(string != NULL);
|
assert(string);
|
||||||
|
|
||||||
size_t len = strlen(string), i = start;
|
size_t len = strlen(string), i = start;
|
||||||
if (len == 0 || len <= i || !*string)
|
if (len == 0 || len <= i || !*string)
|
||||||
@ -106,7 +109,7 @@ size_t _bmUtf8RuneNext(const char *string, size_t start)
|
|||||||
*/
|
*/
|
||||||
size_t _bmUtf8RunePrev(const char *string, size_t start)
|
size_t _bmUtf8RunePrev(const char *string, size_t start)
|
||||||
{
|
{
|
||||||
assert(string != NULL);
|
assert(string);
|
||||||
|
|
||||||
size_t len = strlen(string), i = start;
|
size_t len = strlen(string), i = start;
|
||||||
if (i == 0 || len < start || !*string)
|
if (i == 0 || len < start || !*string)
|
||||||
@ -125,7 +128,7 @@ size_t _bmUtf8RunePrev(const char *string, size_t start)
|
|||||||
*/
|
*/
|
||||||
size_t _bmUtf8RuneWidth(const char *rune, unsigned int u8len)
|
size_t _bmUtf8RuneWidth(const char *rune, unsigned int u8len)
|
||||||
{
|
{
|
||||||
assert(rune != NULL);
|
assert(rune);
|
||||||
char mb[5] = { 0, 0, 0, 0, 0 };
|
char mb[5] = { 0, 0, 0, 0, 0 };
|
||||||
memcpy(mb, rune, (u8len > 4 ? 4 : u8len));
|
memcpy(mb, rune, (u8len > 4 ? 4 : u8len));
|
||||||
return _bmUtf8StringScreenWidth(mb);
|
return _bmUtf8StringScreenWidth(mb);
|
||||||
@ -141,7 +144,7 @@ size_t _bmUtf8RuneWidth(const char *rune, unsigned int u8len)
|
|||||||
*/
|
*/
|
||||||
size_t _bmUtf8RuneRemove(char *string, size_t start, size_t *runeWidth)
|
size_t _bmUtf8RuneRemove(char *string, size_t start, size_t *runeWidth)
|
||||||
{
|
{
|
||||||
assert(string != NULL);
|
assert(string);
|
||||||
|
|
||||||
if (runeWidth)
|
if (runeWidth)
|
||||||
*runeWidth = 0;
|
*runeWidth = 0;
|
||||||
@ -173,7 +176,7 @@ size_t _bmUtf8RuneRemove(char *string, size_t start, size_t *runeWidth)
|
|||||||
*/
|
*/
|
||||||
size_t _bmUtf8RuneInsert(char *string, size_t bufSize, size_t start, const char *rune, unsigned int u8len, size_t *runeWidth)
|
size_t _bmUtf8RuneInsert(char *string, size_t bufSize, size_t start, const char *rune, unsigned int u8len, size_t *runeWidth)
|
||||||
{
|
{
|
||||||
assert(string != NULL);
|
assert(string);
|
||||||
|
|
||||||
if (runeWidth)
|
if (runeWidth)
|
||||||
*runeWidth = 0;
|
*runeWidth = 0;
|
||||||
@ -206,7 +209,7 @@ size_t _bmUtf8RuneInsert(char *string, size_t bufSize, size_t start, const char
|
|||||||
*/
|
*/
|
||||||
size_t _bmUnicodeInsert(char *string, size_t bufSize, size_t start, unsigned int unicode, size_t *runeWidth)
|
size_t _bmUnicodeInsert(char *string, size_t bufSize, size_t start, unsigned int unicode, size_t *runeWidth)
|
||||||
{
|
{
|
||||||
assert(string != NULL);
|
assert(string);
|
||||||
|
|
||||||
char u8len = ((unicode < 0x80) ? 1 : ((unicode < 0x800) ? 2 : ((unicode < 0x10000) ? 3 : 4)));
|
char u8len = ((unicode < 0x80) ? 1 : ((unicode < 0x800) ? 2 : ((unicode < 0x10000) ? 3 : 4)));
|
||||||
char mb[5] = { 0, 0, 0, 0 };
|
char mb[5] = { 0, 0, 0, 0 };
|
||||||
|
@ -11,7 +11,7 @@ int main(int argc, char **argv)
|
|||||||
bmDrawMode i;
|
bmDrawMode i;
|
||||||
for (i = 0; i < BM_DRAW_MODE_LAST; ++i) {
|
for (i = 0; i < BM_DRAW_MODE_LAST; ++i) {
|
||||||
bmMenu *menu = bmMenuNew(i);
|
bmMenu *menu = bmMenuNew(i);
|
||||||
assert(menu != NULL);
|
assert(menu);
|
||||||
bmMenuRender(menu);
|
bmMenuRender(menu);
|
||||||
bmMenuFree(menu);
|
bmMenuFree(menu);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user