[mw-devel] MW3 r1135 - trunk/src

welshbyte at sucs.org welshbyte at sucs.org
Sun Dec 13 21:03:57 GMT 2009


Author: welshbyte
Date: 2009-12-13 21:03:57 +0000 (Sun, 13 Dec 2009)
New Revision: 1135

Modified:
   trunk/src/bb.h
   trunk/src/sqlite.c
Log:
Make USERSQL the full path of the db file and get rid of a bunch of runtime concatenations.


Modified: trunk/src/bb.h
===================================================================
--- trunk/src/bb.h	2009-12-12 14:55:18 UTC (rev 1134)
+++ trunk/src/bb.h	2009-12-13 21:03:57 UTC (rev 1135)
@@ -36,7 +36,7 @@
 #define SCRIPTHELP	"scripthelp"
 #define TALKHELP	"talkhelp"
 
-#define USERSQL		"users.db"
+#define USERSQL		STATEDIR"/users.db"
 #define USERDB_PRIVATE	"private"
 #define USERDB_PUBLIC	"public"
 #define USERDB_ROOMS	"rooms"

Modified: trunk/src/sqlite.c
===================================================================
--- trunk/src/sqlite.c	2009-12-12 14:55:18 UTC (rev 1134)
+++ trunk/src/sqlite.c	2009-12-13 21:03:57 UTC (rev 1135)
@@ -256,15 +256,12 @@
 
 char * userdb_get(const char *table, const char *user, const char *opt)
 {
-	char path[PATHSIZE];
 	struct db_result *old;
 	char *value;
 	char *out;
 
-	snprintf(path, PATHSIZE, "%s/%s", STATEDIR, USERSQL);
-
 	char *query = sqlite3_mprintf("SELECT * from %Q WHERE user=%Q AND opt=%Q", table, user, opt);
-	old  = db_query(path, query, 1);
+	old  = db_query(USERSQL, query, 1);
 	sqlite3_free(query);
 
 	if (old == NULL) return NULL;
@@ -284,38 +281,35 @@
 
 void userdb_set(const char *table, const char *user, const char *opt, const char *arg)
 {
-	char path[PATHSIZE];
 	struct db_result *old;
 
-	snprintf(path, PATHSIZE, "%s/%s", STATEDIR, USERSQL);
-
 	char *query = sqlite3_mprintf("SELECT * from %Q WHERE user=%Q AND opt=%Q", table, user, opt);
-	old  = db_query(path, query, 1);
+	old  = db_query(USERSQL, query, 1);
 	sqlite3_free(query);
 
 	/* set a new value */
 	struct db_result *res;
 	if (arg==NULL || *arg==0) {
 		char *query = sqlite3_mprintf("DELETE FROM %q WHERE user=%Q AND opt=%Q", table, user, opt);
-		db_free(db_query(path, query, 1));
+		db_free(db_query(USERSQL, query, 1));
 		sqlite3_free(query);
 	}else
 	if (old != NULL && old->rows > 0) {
 		char *query = sqlite3_mprintf("UPDATE %q SET arg=%Q WHERE user=%Q AND opt=%Q", table, arg, user, opt);
-		db_free(db_query(path, query, 1));
+		db_free(db_query(USERSQL, query, 1));
 		sqlite3_free(query);
 	} else {
 		char *query = sqlite3_mprintf("INSERT INTO %q (user, opt, arg) VALUES (%Q,%Q,%Q)", table, user, opt, arg);
-		res = db_query(path, query, 1);
+		res = db_query(USERSQL, query, 1);
 		if (res == NULL) {
 			/* it failed, try to create the table */
 			char *create = sqlite3_mprintf("CREATE TABLE %q (user text, opt text, arg text)", table);
-			res = db_query(path, create, 0);
+			res = db_query(USERSQL, create, 0);
 			sqlite3_free(create);
 			if (res != NULL) {
 				/* it worked, resubmit the insert */
 				db_free(res);
-				res = db_query(path, query, 0);
+				res = db_query(USERSQL, query, 0);
 			}
 		}
 		db_free(res);
@@ -327,15 +321,12 @@
 
 char * db_room_get(int room, const char *opt)
 {
-	char path[PATHSIZE];
 	struct db_result *old;
 	char *value;
 	char *out;
 
-	snprintf(path, PATHSIZE, "%s/%s", STATEDIR, USERSQL);
-
 	char *query = sqlite3_mprintf("SELECT * from "USERDB_ROOMS" WHERE room=%d AND opt=%Q", room, opt);
-	old  = db_query(path, query, 1);
+	old  = db_query(USERSQL, query, 1);
 	sqlite3_free(query);
 
 	if (old == NULL) return NULL;
@@ -353,38 +344,35 @@
 
 void db_room_set(int room, const char *opt, const char *arg)
 {
-	char path[PATHSIZE];
 	struct db_result *old;
 
-	snprintf(path, PATHSIZE, "%s/%s", STATEDIR, USERSQL);
-
 	char *query = sqlite3_mprintf("SELECT * from "USERDB_ROOMS" WHERE room=%d AND opt=%Q", room, opt);
-	old  = db_query(path, query, 1);
+	old  = db_query(USERSQL, query, 1);
 	sqlite3_free(query);
 
 	/* set a new value */
 	struct db_result *res;
 	if (arg==NULL || *arg==0) {
 		char *query = sqlite3_mprintf("DELETE FROM "USERDB_ROOMS" WHERE room=%d AND opt=%Q", room, opt);
-		db_free(db_query(path, query, 1));
+		db_free(db_query(USERSQL, query, 1));
 		sqlite3_free(query);
 	}else
 	if (old != NULL && old->rows > 0) {
 		char *query = sqlite3_mprintf("UPDATE "USERDB_ROOMS" SET arg=%Q WHERE room=%d AND opt=%Q", arg, room, opt);
-		db_free(db_query(path, query, 1));
+		db_free(db_query(USERSQL, query, 1));
 		sqlite3_free(query);
 	} else {
 		char *query = sqlite3_mprintf("INSERT INTO "USERDB_ROOMS" (room, opt, arg) VALUES (%d,%Q,%Q)", room, opt, arg);
-		res = db_query(path, query, 1);
+		res = db_query(USERSQL, query, 1);
 		if (res == NULL) {
 			/* it failed, try to create the table */
 			char *create = "CREATE TABLE "USERDB_ROOMS" (room integer, opt text, arg text)";
-			res = db_query(path, create, 0);
+			res = db_query(USERSQL, create, 0);
 			sqlite3_free(create);
 			if (res != NULL) {
 				/* it worked, resubmit the insert */
 				db_free(res);
-				res = db_query(path, query, 0);
+				res = db_query(USERSQL, query, 0);
 			}
 		}
 		db_free(res);




More information about the mw-devel mailing list