src/saxdb.h (saxdb_close_context): Add "close_file" argument.
src/saxdb.c (saxdb_write_db): Use saxdb_{open,close}_context() rather than
putting it on the stack.
(write_database): Likewise.
(saxdb_close_context): Implement the close_file argument.
src/modcmd.c (cmd_dump_messages): Use the close_file argument.
src/mod-helpserv.c (helpserv_log_request): Likewise.
saxdb_write_string(ctx, KEY_REQUEST_CLOSEREASON, reason);
saxdb_write_string_list(ctx, KEY_REQUEST_TEXT, req->text);
saxdb_end_record(ctx);
saxdb_write_string(ctx, KEY_REQUEST_CLOSEREASON, reason);
saxdb_write_string_list(ctx, KEY_REQUEST_TEXT, req->text);
saxdb_end_record(ctx);
- saxdb_close_context(ctx);
- fflush(reqlog_f);
+ saxdb_close_context(ctx, 0);
return 0;
}
if ((res = setjmp(*saxdb_jmp_buf(ctx))) != 0) {
return 0;
}
if ((res = setjmp(*saxdb_jmp_buf(ctx))) != 0) {
- saxdb_close_context(ctx);
- fclose(pf);
+ saxdb_close_context(ctx, 1);
reply("MCMSG_MESSAGE_DUMP_FAILED", strerror(res));
return 0;
} else {
for (it = dict_first(lang_C->messages); it; it = iter_next(it))
saxdb_write_string(ctx, iter_key(it), iter_data(it));
reply("MCMSG_MESSAGE_DUMP_FAILED", strerror(res));
return 0;
} else {
for (it = dict_first(lang_C->messages); it; it = iter_next(it))
saxdb_write_string(ctx, iter_key(it), iter_data(it));
- saxdb_close_context(ctx);
- fclose(pf);
+ saxdb_close_context(ctx, 1);
reply("MCMSG_MESSAGES_DUMPED", fname);
return 1;
}
reply("MCMSG_MESSAGES_DUMPED", fname);
return 1;
}
static int
saxdb_write_db(struct saxdb *db) {
static int
saxdb_write_db(struct saxdb *db) {
- struct saxdb_context ctx;
+ struct saxdb_context *ctx;
+ FILE *output;
char tmp_fname[MAXLEN];
int res, res2;
unsigned long start, finish;
assert(db->filename);
sprintf(tmp_fname, "%s.new", db->filename);
char tmp_fname[MAXLEN];
int res, res2;
unsigned long start, finish;
assert(db->filename);
sprintf(tmp_fname, "%s.new", db->filename);
- memset(&ctx, 0, sizeof(ctx));
- ctx.output = fopen(tmp_fname, "w+");
- int_list_init(&ctx.complex);
- if (!ctx.output) {
+ output = fopen(tmp_fname, "w+");
+ if (!output) {
log_module(MAIN_LOG, LOG_ERROR, "Unable to write to %s: %s", tmp_fname, strerror(errno));
log_module(MAIN_LOG, LOG_ERROR, "Unable to write to %s: %s", tmp_fname, strerror(errno));
- int_list_clean(&ctx.complex);
+ ctx = saxdb_open_context(output);
- if ((res = setjmp(*saxdb_jmp_buf(&ctx))) || (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 {
log_module(MAIN_LOG, LOG_ERROR, "Internal error %d while writing to %s", res2, tmp_fname);
}
if (res) {
log_module(MAIN_LOG, LOG_ERROR, "Error writing to %s: %s", tmp_fname, strerror(res));
} else {
log_module(MAIN_LOG, LOG_ERROR, "Internal error %d while writing to %s", res2, tmp_fname);
}
- int_list_clean(&ctx.complex);
- fclose(ctx.output);
+ ctx->complex.used = 0; /* Squelch asserts about unbalanced output. */
+ saxdb_close_context(ctx, 1);
remove(tmp_fname);
return 2;
}
finish = time(NULL);
remove(tmp_fname);
return 2;
}
finish = time(NULL);
- assert(ctx.complex.used == 0);
- int_list_clean(&ctx.complex);
- fclose(ctx.output);
+ saxdb_close_context(ctx, 1);
if (rename(tmp_fname, db->filename) < 0) {
log_module(MAIN_LOG, LOG_ERROR, "Unable to rename %s to %s: %s", tmp_fname, db->filename, strerror(errno));
}
if (rename(tmp_fname, db->filename) < 0) {
log_module(MAIN_LOG, LOG_ERROR, "Unable to rename %s to %s: %s", tmp_fname, db->filename, strerror(errno));
}
int
write_database(FILE *out, struct dict *db) {
int
write_database(FILE *out, struct dict *db) {
- struct saxdb_context ctx;
+ struct saxdb_context *ctx;
- ctx.output = out;
- ctx.indent = 0;
- int_list_init(&ctx.complex);
- if (!(res = setjmp(*saxdb_jmp_buf(&ctx)))) {
- write_database_helper(&ctx, db);
+ ctx = saxdb_open_context(out);
+ 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);
} else {
log_module(MAIN_LOG, LOG_ERROR, "Exception %d caught while writing to stream", res);
- int_list_clean(&ctx.complex);
+ ctx->complex.used = 0; /* Squelch asserts about unbalanced output. */
+ saxdb_close_context(ctx, 0);
- assert(ctx.complex.used == 0);
- int_list_clean(&ctx.complex);
+ saxdb_close_context(ctx, 0);
-saxdb_close_context(struct saxdb_context *ctx) {
+saxdb_close_context(struct saxdb_context *ctx, int close_file) {
assert(ctx->complex.used == 0);
int_list_clean(&ctx->complex);
assert(ctx->complex.used == 0);
int_list_clean(&ctx->complex);
+ if (close_file)
+ fclose(ctx->output);
+ else
+ fflush(ctx->output);
/* For doing db writing by hand */
struct saxdb_context *saxdb_open_context(FILE *f);
jmp_buf *saxdb_jmp_buf(struct saxdb_context *ctx);
/* For doing db writing by hand */
struct saxdb_context *saxdb_open_context(FILE *f);
jmp_buf *saxdb_jmp_buf(struct saxdb_context *ctx);
-void saxdb_close_context(struct saxdb_context *ctx);
+void saxdb_close_context(struct saxdb_context *ctx, int close_file);
#endif /* !defined(DBMGR_H) */
#endif /* !defined(DBMGR_H) */