gmon.out
test.key
test.crt
+test
tags
}
/* Getters */
-int get_enum(const char *value, const char **map, int n)
+int get_enum(const char *value, const char **map, int s, int n)
{
wasfound = 1;
for (int i = 0; i < n; i++)
- if (match(map[i], value))
+ if (match(map[i*s], value))
return i;
conf_error(value);
return 0;
int get_bool(const char *value)
{
wasfound = 1;
- return get_enum(value, booleans, N_ELEMENTS(booleans));
+ return get_map(value, booleans);
}
int get_number(const char *value)
if (match(key, "bin"))
test_bin = get_bool(value);
else if (match(key, "clr"))
- test_clr = get_enum(value, colors, N_ELEMENTS(colors));
+ test_clr = get_map(value, colors);
else if (match(key, "num"))
test_num = get_number(value);
else if (match(key, "str"))
/* Getters */
#define get_map(value, map) \
- get_enum(value, map, N_ELEMENTS(map))
+ get_enum(value, map, 1, N_ELEMENTS(map))
+#define get_mapv(value, map) \
+ get_enum(value, *map, N_ELEMENTS(*map), N_ELEMENTS(map))
-int get_enum(const char *value, const char **map, int n);
+int get_enum(const char *value, const char **map, int s, int n);
int get_bool(const char *value);
int get_number(const char *value);
char *get_string(const char *value);
# Sources
PROG ?= lamechat
-SOURCES ?= main util args conf view chat net irc xmpp
+PROG_SRC ?= main util args conf view chat net irc xmpp
+TEST ?= test
+TEST_SRC ?= test util
# OpenSSL
ifeq ($(CRYPTO),OPENSSL)
./$(PROG)
# Rules
-$(PROG): $(SOURCES:%=%.o)
+$(PROG): $(PROG_SRC:%=%.o)
+ $(GCC) -o $@ $+ $(LDFLAGS)
+
+$(TEST): $(TEST_SRC:%=%.o)
$(GCC) -o $@ $+ $(LDFLAGS)
%.o: %.c $(wildcard *.h makefile config.mk)
--- /dev/null
+#include "util.h"
+
+void util_test(void);
+
+int main(int argc, char **argv)
+{
+ util_test();
+ return 0;
+}
*old = NULL;
}
+int compare(const char *a, const char *b)
+{
+ if (a == b)
+ return 0;
+ if (!a)
+ return -1;
+ if (!b)
+ return 1;
+ return strcmp(a, b);
+}
+
int match(const char *a, const char *b)
{
if (a == b)
return !strcmp(a, b);
}
+int starts(const char *prefix, const char *str)
+{
+ if (prefix == str)
+ return 1;
+ if (!prefix || !prefix[0])
+ return 1;
+ if (!str || !str[0])
+ return 0;
+ int len = strlen(prefix);
+ return !strncmp(prefix, str, len);
+}
+
int prefix(const char *str, const char *prefix, const char **suffix)
{
- while (*str && *prefix) {
- if (*str != *prefix)
- return 0;
- str++;
+ char last = 0;
+ while (*str && *prefix && *str == *prefix) {
prefix++;
+ last = *str++;
}
- if (*prefix)
- return 0;
- if (*str && *str != ' ')
- return 0;
while (*str && *str == ' ')
- str++;
+ last = *str++;
+
+ int val = 0;
+ const char *arg = NULL;
+ if (*prefix == '\0' && (*str == '\0' || last == ' ')) {
+ arg = *str ? str : NULL;
+ val = 1;
+ }
+
if (suffix)
- *suffix = *str ? str : NULL;
- return 1;
+ *suffix = arg;
+ return val;
}
char *despace(char *str)
view_exit();
exit(-1);
}
+
+/* Test case */
+static void test_prefix(const char *str, const char *pre,
+ const int val0, const char *arg0)
+{
+ const char *arg1 = "junk";
+ int val1 = prefix(str, pre, &arg1);
+ printf("%s -- [%s]%*s [%s]%*s -> %d=%d %s=%s\n",
+ val0==val1 && match(arg0,arg1) ? "pass" : "fail",
+ str, (int)(8-strlen(str)), "",
+ pre, (int)(5-strlen(pre)), "",
+ val0, val1,
+ arg0, arg1);
+}
+
+void util_test(void)
+{
+ test_prefix("/foo", "/foo", 1, NULL);
+ test_prefix("/foo ", "/foo", 1, NULL);
+ test_prefix("/foobar", "/foo", 0, NULL);
+ test_prefix("/foo bar", "/foo", 1, "bar");
+
+ test_prefix("/foo", "/foo ", 0, NULL);
+ test_prefix("/foo ", "/foo ", 1, NULL);
+ test_prefix("/foobar", "/foo ", 0, NULL);
+ test_prefix("/foo bar", "/foo ", 1, "bar");
+}
void strsub(char *str, char find, char repl);
char *strcopy(const char *str);
void strset(char **old, const char *str);
+int compare(const char *a, const char *b);
int match(const char *a, const char *b);
+int starts(const char *prefix, const char *str);
int prefix(const char *str, const char *prefix, const char **suffix);
char *despace(char *text);
void escape(char *dst, const char *src, int len);