Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Shorten everything with macros

  • Loading branch information...
commit ceb00f22974af6f89594215ccec5a95208899d2e 1 parent 1ee895d
@txus authored
View
5 src/shitdb/bstrlib.h
@@ -39,6 +39,11 @@ typedef const struct tagbstring * const_bstring;
/* Copy functions */
#define cstr2bstr bfromcstr
+
+/* Custom defines */
+#define S(A) bfromcstr(A)
+#define eql(A, B) bstrcmp(A, S(B)) == 0
+
extern bstring bfromcstr (const char * str);
extern bstring bfromcstralloc (int mlen, const char * str);
extern bstring blk2bstr (const void * blk, int len);
View
2  src/shitdb/object.c
@@ -94,7 +94,7 @@ Object_to_string(Object *object)
}
}
- return bfromcstr(str);
+ return S(str);
}
Object*
View
38 src/shitdb/server.c
@@ -11,23 +11,23 @@
int Command_arity(bstring cmd)
{
- if(bstrcmp(cmd, bfromcstr("GET")) == 0) {
+ if(eql(cmd, "GET")) {
return 1;
- } else if (bstrcmp(cmd, bfromcstr("SET")) == 0) {
+ } else if (eql(cmd, "SET")) {
return 2;
- } else if (bstrcmp(cmd, bfromcstr("APUSH")) == 0) {
+ } else if (eql(cmd, "APUSH")) {
return 2;
- } else if (bstrcmp(cmd, bfromcstr("APOP")) == 0) {
+ } else if (eql(cmd, "APOP")) {
return 1;
- } else if (bstrcmp(cmd, bfromcstr("AAT")) == 0) {
+ } else if (eql(cmd, "AAT")) {
return 2;
- } else if (bstrcmp(cmd, bfromcstr("ACOUNT")) == 0) {
+ } else if (eql(cmd, "ACOUNT")) {
return 1;
- } else if (bstrcmp(cmd, bfromcstr("HSET")) == 0) {
+ } else if (eql(cmd, "HSET")) {
return 3;
- } else if (bstrcmp(cmd, bfromcstr("HGET")) == 0) {
+ } else if (eql(cmd, "HGET")) {
return 2;
- } else if (bstrcmp(cmd, bfromcstr("QUIT")) == 0) {
+ } else if (eql(cmd, "QUIT")) {
return 0;
} else {
return -1;
@@ -48,24 +48,24 @@ int Server_execute(DB *db, bstring command, Object *result)
ptr++;
- if(bstrcmp(cmd, bfromcstr("GET")) == 0) {
+ if(eql(cmd, "GET")) {
Object *ret = DB_get(db, *ptr);
memcpy(result, ret, sizeof(Object));
- } else if (bstrcmp(cmd, bfromcstr("SET")) == 0) {
+ } else if (eql(cmd, "SET")) {
bstring key = *ptr;
ptr++;
Object *value = String_to_object(*ptr);
check(value, "Invalid value to SET.");
DB_set(db, key, value);
- } else if (bstrcmp(cmd, bfromcstr("APUSH")) == 0) {
+ } else if (eql(cmd, "APUSH")) {
bstring key = *ptr;
ptr++;
Object *value = String_to_object(*ptr);
check(value, "Invalid value to APUSH.");
DB_apush(db, key, value);
- } else if (bstrcmp(cmd, bfromcstr("AAT")) == 0) {
+ } else if (eql(cmd, "AAT")) {
bstring key = *ptr;
ptr++;
int index = atoi(bdata(*ptr));
@@ -73,17 +73,17 @@ int Server_execute(DB *db, bstring command, Object *result)
Object *ret = DB_aat(db, key, index);
memcpy(result, ret, sizeof(Object));
- } else if (bstrcmp(cmd, bfromcstr("ACOUNT")) == 0) {
+ } else if (eql(cmd, "ACOUNT")) {
bstring key = *ptr;
Object *ret = DB_acount(db, key);
memcpy(result, ret, sizeof(Object));
- } else if (bstrcmp(cmd, bfromcstr("APOP")) == 0) {
+ } else if (eql(cmd, "APOP")) {
bstring key = *ptr;
ptr++;
Object *ret = DB_apop(db, key);
memcpy(result, ret, sizeof(Object));
- } else if (bstrcmp(cmd, bfromcstr("HSET")) == 0) {
+ } else if (eql(cmd, "HSET")) {
bstring key = *ptr;
ptr++;
bstring hkey = *ptr;
@@ -92,14 +92,14 @@ int Server_execute(DB *db, bstring command, Object *result)
check(value, "Invalid value to HSET.");
DB_hset(db, key, hkey, value);
- } else if (bstrcmp(cmd, bfromcstr("HGET")) == 0) {
+ } else if (eql(cmd, "HGET")) {
bstring key = *ptr;
ptr++;
bstring hkey = *ptr;
Object *ret = DB_hget(db, key, hkey);
memcpy(result, ret, sizeof(Object));
- } else if (bstrcmp(cmd, bfromcstr("QUIT")) == 0) {
+ } else if (eql(cmd, "QUIT")) {
return -1;
}
return 0;
@@ -178,7 +178,7 @@ void Server_start(DB *db, int port)
char buf[256];
while((received = recv(accept_fd, buf, 255, 0))) {
Object *result = Object_allocate();
- rc = Server_execute(db, chomp(bfromcstr(buf)), result);
+ rc = Server_execute(db, chomp(S(buf)), result);
if(rc == 0) {
if (result->type != tNil) {
bstring ret = Object_to_string(result);
View
48 tests/db_tests.c
@@ -12,34 +12,34 @@ char *test_create()
char *test_set()
{
- Object *str = Object_create_string(bfromcstr("bar"));
+ Object *str = Object_create_string(S("bar"));
Object *number = Object_create_integer(42);
- Object *array = String_to_object(bfromcstr("[42, \"hey\"]"));
+ Object *array = String_to_object(S("[42, \"hey\"]"));
- DB_set(db, bfromcstr("foo"), str);
- DB_set(db, bfromcstr("number"), number);
- DB_set(db, bfromcstr("array"), array);
+ DB_set(db, S("foo"), str);
+ DB_set(db, S("number"), number);
+ DB_set(db, S("array"), array);
return NULL;
}
char *test_get()
{
- Object *bar = DB_get(db, bfromcstr("foo"));
+ Object *bar = DB_get(db, S("foo"));
bstring barstr = Object_to_string(bar);
- mu_assert(bstrcmp(barstr, bfromcstr("\"bar\"")) == 0, "Strings mismatch");
+ mu_assert(eql(barstr, "\"bar\""), "Strings mismatch");
- Object *number = DB_get(db, bfromcstr("number"));
+ Object *number = DB_get(db, S("number"));
bstring numberstr = Object_to_string(number);
- mu_assert(bstrcmp(numberstr, bfromcstr("42")) == 0, "Integer mismatch");
+ mu_assert(eql(numberstr, "42"), "Integer mismatch");
- Object *ary = DB_get(db, bfromcstr("array"));
+ Object *ary = DB_get(db, S("array"));
bstring arystr = Object_to_string(ary);
- mu_assert(bstrcmp(arystr, bfromcstr("[42, \"hey\"]")) == 0, "Array mismatch");
+ mu_assert(eql(arystr, "[42, \"hey\"]"), "Array mismatch");
return NULL;
}
@@ -47,33 +47,33 @@ char *test_get()
char *test_apush()
{
Object *integer = Object_create_integer(999);
- DB_apush(db, bfromcstr("newarray"), integer);
+ DB_apush(db, S("newarray"), integer);
return NULL;
}
char *test_apop()
{
- Object *integer = DB_apop(db, bfromcstr("newarray"));
+ Object *integer = DB_apop(db, S("newarray"));
bstring integerstr = Object_to_string(integer);
- mu_assert(bstrcmp(integerstr, bfromcstr("999")) == 0, "Wrong popped value.");
+ mu_assert(eql(integerstr, "999"), "Wrong popped value.");
return NULL;
}
char *test_aat()
{
- Object *integer = DB_aat(db, bfromcstr("array"), 0);
+ Object *integer = DB_aat(db, S("array"), 0);
bstring integerstr = Object_to_string(integer);
- mu_assert(bstrcmp(integerstr, bfromcstr("42")) == 0, "Wrong value at index.");
+ mu_assert(eql(integerstr, "42"), "Wrong value at index.");
return NULL;
}
char *test_acount()
{
- Object *count = DB_acount(db, bfromcstr("array"));
+ Object *count = DB_acount(db, S("array"));
bstring countstr = Object_to_string(count);
- mu_assert(bstrcmp(countstr, bfromcstr("2")) == 0, "Wrong array count.");
+ mu_assert(eql(countstr, "2"), "Wrong array count.");
return NULL;
}
@@ -82,21 +82,21 @@ char *test_hset()
Object *integer1 = Object_create_integer(999);
Object *integer2 = Object_create_integer(1);
- DB_hset(db, bfromcstr("hash"), bfromcstr("foo"), integer1);
- DB_hset(db, bfromcstr("hash"), bfromcstr("bar"), integer2);
+ DB_hset(db, S("hash"), bfromcstr("foo"), integer1);
+ DB_hset(db, S("hash"), bfromcstr("bar"), integer2);
return NULL;
}
char *test_hget()
{
- Object *integer1 = DB_hget(db, bfromcstr("hash"), bfromcstr("foo"));
- Object *integer2 = DB_hget(db, bfromcstr("hash"), bfromcstr("bar"));
+ Object *integer1 = DB_hget(db, S("hash"), bfromcstr("foo"));
+ Object *integer2 = DB_hget(db, S("hash"), bfromcstr("bar"));
bstring integer1str = Object_to_string(integer1);
bstring integer2str = Object_to_string(integer2);
- mu_assert(bstrcmp(integer1str, bfromcstr("999")) == 0, "Wrong hash value on creation");
- mu_assert(bstrcmp(integer2str, bfromcstr("1")) == 0, "Wrong hash value on update");
+ mu_assert(eql(integer1str, "999"), "Wrong hash value on creation");
+ mu_assert(eql(integer2str, "1"), "Wrong hash value on update");
return NULL;
}
View
12 tests/object_tests.c
@@ -6,19 +6,19 @@ char *test_to_string()
Object *integer = Object_create_integer(123);
bstring int_result = Object_to_string(integer);
- Object *string = Object_create_string(bfromcstr("hello"));
+ Object *string = Object_create_string(S("hello"));
bstring str_result = Object_to_string(string);
- mu_assert(bstrcmp(int_result, bfromcstr("123")) == 0, "Integer to string mismatch");
- mu_assert(bstrcmp(str_result, bfromcstr("\"hello\"")) == 0, "String obj to string mismatch");
+ mu_assert(eql(int_result, "123"), "Integer to string mismatch");
+ mu_assert(eql(str_result, "\"hello\""), "String obj to string mismatch");
return NULL;
}
char *test_to_object()
{
- bstring integer = bfromcstr("1");
- bstring string = bfromcstr("\"hello\"");
+ bstring integer = S("1");
+ bstring string = S("\"hello\"");
Object *obj_int = String_to_object(integer);
Object *obj_str = String_to_object(string);
@@ -27,7 +27,7 @@ char *test_to_object()
mu_assert(obj_int->value.as_integer == 1, "Integer ot object did not get correct value");
mu_assert(obj_str->type == tString, "String to object is not of type String");
- mu_assert(bstrcmp(obj_str->value.as_string, bfromcstr("hello")) == 0, "String to object did not get correct value");
+ mu_assert(eql(obj_str->value.as_string, "hello"), "String to object did not get correct value");
return NULL;
}
Please sign in to comment.
Something went wrong with that request. Please try again.