summaryrefslogtreecommitdiff
path: root/lib/util.c
diff options
context:
space:
mode:
authorJari Vetoniemi <mailroxas@gmail.com>2014-04-10 22:02:47 +0300
committerJari Vetoniemi <mailroxas@gmail.com>2014-04-10 22:02:47 +0300
commit45502a2fd994fdb10b77e4ac79c5f9fd8a8399e8 (patch)
tree3af00fb5ae08fd93e8bea828f6a85358751939f4 /lib/util.c
parent05212f5e44571945b40f72ae70ec7012af85fe75 (diff)
downloadbemenu-45502a2fd994fdb10b77e4ac79c5f9fd8a8399e8.tar.gz
bemenu-45502a2fd994fdb10b77e4ac79c5f9fd8a8399e8.tar.bz2
bemenu-45502a2fd994fdb10b77e4ac79c5f9fd8a8399e8.zip
Proper filtering functions.
Diffstat (limited to 'lib/util.c')
-rw-r--r--lib/util.c60
1 files changed, 43 insertions, 17 deletions
diff --git a/lib/util.c b/lib/util.c
index 5eb980d..06c89cf 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -28,30 +28,56 @@ char* _bmStrdup(const char *string)
}
/**
- * Shrink bmItem** list pointer.
+ * Portable case-insensitive strcmp.
*
- * Useful helper function for filter functions.
+ * @param hay C "string" to match against.
+ * @param needle C "string" to match.
+ */
+int _bmStrupcmp(const char *hay, const char *needle)
+{
+ size_t i, len;
+
+ if ((len = strlen(hay)) != strlen(needle))
+ return 1;
+
+ for (i = 0; i != len; ++i)
+ if (toupper(hay[i]) != toupper(needle[i]))
+ return 1;
+
+ return 0;
+}
+
+/**
+ * Portable case-insensitive strstr.
*
- * @param list Pointer to pointer to list of bmItem pointers.
- * @param osize Current size of the list.
- * @param nsize New size the list will be shrinked to.
- * @return Pointer to list of bmItem pointers.
+ * @param hay C "string" to substring against.
+ * @param needle C "string" to substring.
*/
-bmItem** _bmShrinkItemList(bmItem ***inOutList, size_t osize, size_t nsize)
+char* _bmStrupstr(const char *hay, const char *needle)
{
- assert(inOutList);
+ size_t i, r = 0, p = 0, len, len2;
- if (nsize >= osize)
- return *inOutList;
+ if (!_bmStrupcmp(hay, needle))
+ return (char*)hay;
- void *tmp = malloc(sizeof(bmItem*) * nsize);
- if (!tmp)
- return *inOutList;
+ if ((len = strlen(hay)) < (len2 = strlen(needle)))
+ return NULL;
+
+ for (i = 0; i != len; ++i) {
+ if (p == len2)
+ return (char*)hay + r;
+
+ if (toupper(hay[i]) == toupper(needle[p++])) {
+ if (!r)
+ r = i;
+ } else {
+ if (r)
+ i = r;
+ r = p = 0;
+ }
+ }
- memcpy(tmp, *inOutList, sizeof(bmItem*) * nsize);
- free(*inOutList);
- *inOutList = tmp;
- return *inOutList;
+ return (p == len2 ? (char*)hay + r : NULL);
}
/**