Make saxdb_context definition private.
[srvx.git] / src / saxdb.c
index 88f8352e614b90dc570e8975df4bf7b76db7fafa..d504702bad104bd0c18d3a82ba2cd6b5ae6a3bc9 100644 (file)
@@ -24,7 +24,7 @@
 #include "saxdb.h"
 #include "timeq.h"
 
-DEFINE_LIST(int_list, int);
+DEFINE_LIST(int_list, int)
 
 struct saxdb {
     char *name;
@@ -33,11 +33,18 @@ struct saxdb {
     saxdb_reader_func_t *reader;
     saxdb_writer_func_t *writer;
     unsigned int write_interval;
-    time_t last_write;
+    unsigned long last_write;
     unsigned int last_write_duration;
     struct saxdb *prev;
 };
 
+struct saxdb_context {
+    FILE *output;
+    unsigned int indent;
+    struct int_list complex;
+    jmp_buf jbuf;
+};
+
 #define COMPLEX(CTX) ((CTX)->complex.used ? ((CTX)->complex.list[(CTX)->complex.used-1]) : 1)
 
 static struct saxdb *last_db;
@@ -58,6 +65,7 @@ saxdb_read_db(struct saxdb *db) {
     if (!data)
         return;
     if (db->writer == saxdb_mondo_writer) {
+        free_database(mondo_db);
         mondo_db = data;
     } else {
         db->reader(data);
@@ -121,7 +129,7 @@ saxdb_write_db(struct saxdb *db) {
     struct saxdb_context ctx;
     char tmp_fname[MAXLEN];
     int res, res2;
-    time_t start, finish;
+    unsigned long start, finish;
 
     assert(db->filename);
     sprintf(tmp_fname, "%s.new", db->filename);
@@ -134,7 +142,7 @@ saxdb_write_db(struct saxdb *db) {
         return 1;
     }
     start = time(NULL);
-    if ((res = setjmp(ctx.jbuf)) || (res2 = db->writer(&ctx))) {
+    if ((res = setjmp(*saxdb_jmp_buf(&ctx))) || (res2 = db->writer(&ctx))) {
         if (res) {
             log_module(MAIN_LOG, LOG_ERROR, "Error writing to %s: %s", tmp_fname, strerror(res));
         } else {
@@ -236,7 +244,7 @@ saxdb_pre_object(struct saxdb_context *dest) {
     }
 }
 #else
-#define saxdb_pre_object(DEST) 
+#define saxdb_pre_object(DEST)
 #endif
 
 static inline void
@@ -296,12 +304,20 @@ saxdb_write_string(struct saxdb_context *dest, const char *name, const char *val
 
 void
 saxdb_write_int(struct saxdb_context *dest, const char *name, unsigned long value) {
-    unsigned char buf[16];
+    char buf[16];
     /* we could optimize this to take advantage of the fact that buf will never need escapes */
     snprintf(buf, sizeof(buf), "%lu", value);
     saxdb_write_string(dest, name, buf);
 }
 
+void
+saxdb_write_sint(struct saxdb_context *dest, const char *name, long value) {
+    char buf[16];
+    /* we could optimize this to take advantage of the fact that buf will never need escapes */
+    snprintf(buf, sizeof(buf), "%ld", value);
+    saxdb_write_string(dest, name, buf);
+}
+
 static void
 saxdb_free(void *data) {
     struct saxdb *db = data;
@@ -378,7 +394,7 @@ static MODCMD_FUNC(cmd_write) {
                 stop.tv_sec -= 1;
                 stop.tv_usec += 1000000;
             }
-            reply("MSG_DB_WROTE_DB", db->name, stop.tv_sec, stop.tv_usec);
+            reply("MSG_DB_WROTE_DB", db->name, (unsigned long)stop.tv_sec, (unsigned long)stop.tv_usec);
             written++;
         }
     }
@@ -397,7 +413,7 @@ static MODCMD_FUNC(cmd_writeall) {
         stop.tv_sec -= 1;
         stop.tv_usec += 1000000;
     }
-    reply("MSG_DB_WROTE_ALL", stop.tv_sec, stop.tv_usec);
+    reply("MSG_DB_WROTE_ALL", (unsigned long)stop.tv_sec, (unsigned long)stop.tv_usec);
     return 1;
 }
 
@@ -418,6 +434,10 @@ static MODCMD_FUNC(cmd_stats_databases) {
     tbl.contents[0][4] = "Last Duration";
     for (ii=1, it=dict_first(saxdbs); it; it=iter_next(it), ++ii) {
         struct saxdb *db = iter_data(it);
+        if (db->mondo_section) {
+            --ii;
+            continue;
+        }
         char *buf = malloc(INTERVALLEN*3);
         tbl.contents[ii] = calloc(tbl.width, sizeof(tbl.contents[ii][0]));
         tbl.contents[ii][0] = db->name;
@@ -438,6 +458,7 @@ static MODCMD_FUNC(cmd_stats_databases) {
         tbl.contents[ii][3] = buf+INTERVALLEN;
         tbl.contents[ii][4] = buf+INTERVALLEN*2;
     }
+    tbl.length = ii;
     table_send(cmd->parent->bot, user->nick, 0, 0, tbl);
     free(tbl.contents[0]);
     for (ii=1; ii<tbl.length; ++ii) {
@@ -522,7 +543,7 @@ write_database(FILE *out, struct dict *db) {
     ctx.output = out;
     ctx.indent = 0;
     int_list_init(&ctx.complex);
-    if (!(res = setjmp(ctx.jbuf))) {
+    if (!(res = setjmp(*saxdb_jmp_buf(&ctx)))) {
         write_database_helper(&ctx, db);
     } else {
         log_module(MAIN_LOG, LOG_ERROR, "Exception %d caught while writing to stream", res);
@@ -546,6 +567,12 @@ saxdb_open_context(FILE *file) {
     return ctx;
 }
 
+jmp_buf *
+saxdb_jmp_buf(struct saxdb_context *ctx) {
+    return &ctx->jbuf;
+}
+
+
 void
 saxdb_close_context(struct saxdb_context *ctx) {
     assert(ctx->complex.used == 0);