eliminate global log_flags, revert the mode arguments for fopen.

turns out that text is default, and windows hates it if I open a binary file without 'b'
standardize on using join_path to create filenames
(use backslash on windows, even though fopen doesn't seem to care).
This commit is contained in:
Enno Rehling 2016-02-05 23:10:05 +01:00
parent e39af68dde
commit 8a3bb35987
24 changed files with 89 additions and 97 deletions

View File

@ -3595,14 +3595,15 @@ battle *make_battle(region * r)
if (battledebug) { if (battledebug) {
char zText[MAX_PATH]; char zText[MAX_PATH];
char zFilename[MAX_PATH]; char zFilename[MAX_PATH];
sprintf(zText, "%s/battles", basepath()); join_path(basepath(), "battles", zText, sizeof(zText));
if (_mkdir(zText) != 0) { if (_mkdir(zText) != 0) {
log_error("could not create subdirectory for battle logs: %s", zText); log_error("could not create subdirectory for battle logs: %s", zText);
battledebug = false; battledebug = false;
} }
else { else {
sprintf(zFilename, "%s/battle-%d-%s.log", zText, obs_count++, simplename(r)); sprintf(zFilename, "battle-%d-%s.log", obs_count++, simplename(r));
bdebug = fopen(zFilename, "w"); join_path(zText, zFilename, zText, sizeof(zText));
bdebug = fopen(zText, "w");
if (!bdebug) if (!bdebug)
log_error("battles cannot be debugged"); log_error("battles cannot be debugged");
else { else {

View File

@ -1,6 +1,7 @@
#include "bind_config.h" #include "bind_config.h"
#include <platform.h> #include <platform.h>
#include <kernel/config.h>
#include <kernel/jsonconf.h> #include <kernel/jsonconf.h>
#include <util/log.h> #include <util/log.h>
#include <util/language.h> #include <util/language.h>
@ -58,11 +59,11 @@ int config_read(const char *filename, const char * relpath)
json_relpath = relpath; json_relpath = relpath;
if (relpath) { if (relpath) {
_snprintf(name, sizeof(name), "%s/%s", relpath, filename); join_path(relpath, filename, name, sizeof(name));
F = fopen(name, "rt"); F = fopen(name, "r");
} }
else { else {
F = fopen(filename, "rt"); F = fopen(filename, "r");
} }
if (F) { if (F) {
long size; long size;

View File

@ -32,7 +32,7 @@ int eressea_read_orders(const char * filename) {
} }
int eressea_export_json(const char * filename, int flags) { int eressea_export_json(const char * filename, int flags) {
FILE *F = fopen(filename, "wt"); FILE *F = fopen(filename, "w");
if (F) { if (F) {
stream out = { 0 }; stream out = { 0 };
int err; int err;
@ -46,7 +46,7 @@ int eressea_export_json(const char * filename, int flags) {
} }
int eressea_import_json(const char * filename) { int eressea_import_json(const char * filename) {
FILE *F = fopen(filename, "rt"); FILE *F = fopen(filename, "r");
if (F) { if (F) {
stream out = { 0 }; stream out = { 0 };
int err; int err;

View File

@ -32,7 +32,7 @@ without prior permission by the authors of Eressea.
static int tolua_storage_create(lua_State * L) static int tolua_storage_create(lua_State * L)
{ {
const char *filename = tolua_tostring(L, 1, 0); const char *filename = tolua_tostring(L, 1, 0);
const char *type = tolua_tostring(L, 2, "r"); const char *type = tolua_tostring(L, 2, "rb");
gamedata *data; gamedata *data;
data = gamedata_open(filename, type); data = gamedata_open(filename, type);

View File

@ -1502,7 +1502,7 @@ report_computer(const char *filename, report_context * ctx, const char *charset)
const char *mailto = LOC(f->locale, "mailto"); const char *mailto = LOC(f->locale, "mailto");
const attrib *a; const attrib *a;
seen_region *sr = NULL; seen_region *sr = NULL;
FILE *F = fopen(filename, "wt"); FILE *F = fopen(filename, "w");
if (era < 0) { if (era < 0) {
era = config_get_int("world.era", 1); era = config_get_int("world.era", 1);

View File

@ -848,7 +848,7 @@ static void handlekey(state * st, int c)
break; break;
case 'B': case 'B':
if (!new_players) { if (!new_players) {
sprintf(sbuffer, "%s/newfactions", basepath()); join_path(basepath(), "newfactions", sbuffer, sizeof(sbuffer));
new_players = read_newfactions(sbuffer); new_players = read_newfactions(sbuffer);
} }
cnormalize(&st->cursor, &nx, &ny); cnormalize(&st->cursor, &nx, &ny);
@ -1063,7 +1063,7 @@ static void handlekey(state * st, int c)
break; break;
case 'A': case 'A':
if (!new_players) { if (!new_players) {
sprintf(sbuffer, "%s/newfactions", basepath()); join_path(basepath(), "newfactions", sbuffer, sizeof(sbuffer));
new_players = read_newfactions(sbuffer); new_players = read_newfactions(sbuffer);
} }
seed_players(&new_players, false); seed_players(&new_players, false);
@ -1236,11 +1236,10 @@ void run_mapper(void)
WINDOW *hwininfo; WINDOW *hwininfo;
WINDOW *hwinmap; WINDOW *hwinmap;
int width, height, x, y; int width, height, x, y;
int split = 20, old_flags = log_flags; int split = 20;
state *st; state *st;
point tl; point tl;
log_flags &= ~(LOG_CPERROR | LOG_CPWARNING);
init_curses(); init_curses();
curs_set(1); curs_set(1);
@ -1332,7 +1331,6 @@ void run_mapper(void)
set_readline(NULL); set_readline(NULL);
curs_set(1); curs_set(1);
endwin(); endwin();
log_flags = old_flags;
state_close(st); state_close(st);
} }

View File

@ -659,11 +659,16 @@ void set_basepath(const char *path)
#define PATH_DELIM '/' #define PATH_DELIM '/'
#endif #endif
char * join_path(const char *p1, const char *p2, char *dst, size_t len) { char * join_path(const char *p1, const char *p2, char *dst, size_t len) {
size_t sz; size_t sz;
assert(p1 && p2); assert(p1 && p2);
assert(p2 != dst);
if (dst == p1) {
sz = strlen(p1);
}
else {
sz = strlcpy(dst, p1, len); sz = strlcpy(dst, p1, len);
}
assert(sz < len); assert(sz < len);
dst[sz++] = PATH_DELIM; dst[sz++] = PATH_DELIM;
strlcpy(dst + sz, p2, len - sz); strlcpy(dst + sz, p2, len - sz);

View File

@ -853,7 +853,7 @@ int writepasswd(void)
FILE *F; FILE *F;
char zText[128]; char zText[128];
sprintf(zText, "%s/passwd", basepath()); join_path(basepath(), "passwd", zText, sizeof(zText));
F = fopen(zText, "w"); F = fopen(zText, "w");
if (!F) { if (!F) {
perror(zText); perror(zText);

View File

@ -23,7 +23,7 @@ static void test_group_readwrite(CuTest * tc)
FILE *F; FILE *F;
stream strm; stream strm;
F = fopen("test.dat", "w"); F = fopen("test.dat", "wb");
fstream_init(&strm, F); fstream_init(&strm, F);
binstore_init(&store, &strm); binstore_init(&store, &strm);
test_cleanup(); test_cleanup();
@ -36,7 +36,7 @@ static void test_group_readwrite(CuTest * tc)
binstore_done(&store); binstore_done(&store);
fstream_done(&strm); fstream_done(&strm);
F = fopen("test.dat", "r"); F = fopen("test.dat", "rb");
fstream_init(&strm, F); fstream_init(&strm, F);
binstore_init(&store, &strm); binstore_init(&store, &strm);
f->groups = 0; f->groups = 0;

View File

@ -828,11 +828,11 @@ static void json_include(cJSON *json) {
FILE *F; FILE *F;
if (json_relpath) { if (json_relpath) {
char name[MAX_PATH]; char name[MAX_PATH];
_snprintf(name, sizeof(name), "%s/%s", json_relpath, child->valuestring); join_path(json_relpath, child->valuestring, name, sizeof(name));
F = fopen(name, "rt"); F = fopen(name, "r");
} }
else { else {
F = fopen(child->valuestring, "rt"); F = fopen(child->valuestring, "r");
} }
if (F) { if (F) {
long pos; long pos;

View File

@ -438,7 +438,7 @@ static void test_configs(CuTest * tc)
test_cleanup(); test_cleanup();
F = fopen("test.json", "wt"); F = fopen("test.json", "w");
fwrite(building_data, 1, strlen(building_data), F); fwrite(building_data, 1, strlen(building_data), F);
fclose(F); fclose(F);
CuAssertPtrNotNull(tc, json); CuAssertPtrNotNull(tc, json);

View File

@ -552,7 +552,7 @@ int current_turn(void)
int cturn = 0; int cturn = 0;
FILE *F; FILE *F;
sprintf(zText, "%s/turn", basepath()); join_path(basepath(), "turn", zText, sizeof(zText));
F = fopen(zText, "r"); F = fopen(zText, "r");
if (!F) { if (!F) {
perror(zText); perror(zText);
@ -1166,6 +1166,7 @@ faction *readfaction(struct gamedata * data)
char name[DISPLAYSIZE]; char name[DISPLAYSIZE];
READ_INT(data->store, &n); READ_INT(data->store, &n);
assert(n > 0);
f = findfaction(n); f = findfaction(n);
if (f == NULL) { if (f == NULL) {
f = (faction *)calloc(1, sizeof(faction)); f = (faction *)calloc(1, sizeof(faction));
@ -1401,13 +1402,13 @@ int readgame(const char *filename, bool backup)
init_locales(); init_locales();
log_debug("- reading game data from %s\n", filename); log_debug("- reading game data from %s\n", filename);
sprintf(path, "%s/%s", datapath(), filename); join_path(datapath(), filename, path, sizeof(path));
if (backup) { if (backup) {
create_backup(path); create_backup(path);
} }
F = fopen(path, "r"); F = fopen(path, "rb");
if (!F) { if (!F) {
perror(path); perror(path);
return -1; return -1;
@ -1753,7 +1754,7 @@ int writegame(const char *filename)
/* make sure we don't overwrite an existing file (hard links) */ /* make sure we don't overwrite an existing file (hard links) */
unlink(path); unlink(path);
#endif #endif
F = fopen(path, "w"); F = fopen(path, "wb");
if (!F) { if (!F) {
perror(path); perror(path);
return -1; return -1;

View File

@ -15,10 +15,10 @@ static void test_readwrite_data(CuTest * tc)
const char *filename = "test.dat"; const char *filename = "test.dat";
char path[MAX_PATH]; char path[MAX_PATH];
test_cleanup(); test_cleanup();
sprintf(path, "%s/%s", datapath(), filename);
CuAssertIntEquals(tc, 0, writegame(filename)); CuAssertIntEquals(tc, 0, writegame(filename));
CuAssertIntEquals(tc, 0, readgame(filename, false)); CuAssertIntEquals(tc, 0, readgame(filename, false));
CuAssertIntEquals(tc, RELEASE_VERSION, global.data_version); CuAssertIntEquals(tc, RELEASE_VERSION, global.data_version);
join_path(datapath(), filename, path, sizeof(path));
CuAssertIntEquals(tc, 0, remove(path)); CuAssertIntEquals(tc, 0, remove(path));
test_cleanup(); test_cleanup();
} }
@ -42,15 +42,15 @@ static void test_readwrite_unit(CuTest * tc)
u = test_create_unit(f, r); u = test_create_unit(f, r);
join_path(datapath(), filename, path, sizeof(path)); join_path(datapath(), filename, path, sizeof(path));
data = gamedata_open(path, "w"); data = gamedata_open(path, "wb");
CuAssertPtrNotNull(tc, data); // TODO: intermittent test CuAssertPtrNotNull(tc, data); // TODO: intermittent test (even after the 'b' fix!)
write_unit(data, u); write_unit(data, u);
gamedata_close(data); gamedata_close(data);
free_gamedata(); free_gamedata();
f = test_create_faction(0); f = test_create_faction(0);
renumber_faction(f, fno); renumber_faction(f, fno);
data = gamedata_open(path, "r"); data = gamedata_open(path, "rb");
u = read_unit(data); u = read_unit(data);
gamedata_close(data); gamedata_close(data);

View File

@ -4442,8 +4442,7 @@ void update_subscriptions(void)
FILE *F; FILE *F;
char zText[MAX_PATH]; char zText[MAX_PATH];
strlcpy(zText, basepath(), sizeof(zText)); join_path(basepath(), "subscriptions", zText, sizeof(zText));
strlcat(zText, "/subscriptions", sizeof(zText));
F = fopen(zText, "r"); F = fopen(zText, "r");
if (F == NULL) { if (F == NULL) {
log_warning(0, "could not open %s.\n", zText); log_warning(0, "could not open %s.\n", zText);
@ -4463,17 +4462,6 @@ void update_subscriptions(void)
} }
} }
fclose(F); fclose(F);
sprintf(zText, "subscriptions.%u", turn);
F = fopen(zText, "w");
if (F) {
faction *f;
for (f = factions; f != NULL; f = f->next) {
fprintf(F, "%s:%u:%s:%s:%u:\n",
itoa36(f->no), f->subscription, f->email, dbrace(f->race), f->lastorders);
}
fclose(F);
}
} }
bool bool

View File

@ -296,7 +296,7 @@ int main(int argc, char **argv)
/* parse arguments again, to override ini file */ /* parse arguments again, to override ini file */
parse_args(argc, argv, &err); parse_args(argc, argv, &err);
log_open(logfile); log_open(logfile, LOG_CPERROR | LOG_CPWARNING | LOG_CPDEBUG | LOG_FLUSH);
locale_init(); locale_init();
#ifdef CRTDBG #ifdef CRTDBG

View File

@ -152,7 +152,7 @@ void score(void)
allscores = 1; allscores = 1;
} }
sprintf(path, "%s/score", basepath()); join_path(basepath(), "score", path, sizeof(path));
scoreFP = fopen(path, "w"); scoreFP = fopen(path, "w");
if (scoreFP) { if (scoreFP) {
const unsigned char utf8_bom[4] = { 0xef, 0xbb, 0xbf, 0 }; const unsigned char utf8_bom[4] = { 0xef, 0xbb, 0xbf, 0 };
@ -177,7 +177,7 @@ void score(void)
alliance *a; alliance *a;
const item_type *token = it_find("conquesttoken"); const item_type *token = it_find("conquesttoken");
sprintf(path, "%s/score.alliances", basepath()); join_path(basepath(), "score.alliances", path, sizeof(path));
scoreFP = fopen(path, "w"); scoreFP = fopen(path, "w");
if (scoreFP) { if (scoreFP) {
const unsigned char utf8_bom[4] = { 0xef, 0xbb, 0xbf, 0 }; const unsigned char utf8_bom[4] = { 0xef, 0xbb, 0xbf, 0 };

View File

@ -1403,7 +1403,7 @@ report_template(const char *filename, report_context * ctx, const char *charset)
const resource_type *rsilver = get_resourcetype(R_SILVER); const resource_type *rsilver = get_resourcetype(R_SILVER);
faction *f = ctx->f; faction *f = ctx->f;
region *r; region *r;
FILE *F = fopen(filename, "wt"); FILE *F = fopen(filename, "w");
stream strm = { 0 }, *out = &strm; stream strm = { 0 }, *out = &strm;
seen_region *sr = NULL; seen_region *sr = NULL;
char buf[8192], *bufp; char buf[8192], *bufp;
@ -2067,7 +2067,7 @@ const char *charset)
unsigned char op; unsigned char op;
int maxh, bytes, ix = want(O_STATISTICS); int maxh, bytes, ix = want(O_STATISTICS);
int wants_stats = (f->options & ix); int wants_stats = (f->options & ix);
FILE *F = fopen(filename, "wt"); FILE *F = fopen(filename, "w");
stream strm = { 0 }, *out = &strm; stream strm = { 0 }, *out = &strm;
seen_region *sr = NULL; seen_region *sr = NULL;
char buf[8192]; char buf[8192];

View File

@ -1558,11 +1558,13 @@ int write_reports(faction * f, time_t ltime)
if (f->options & rtype->flag) { if (f->options & rtype->flag) {
int error; int error;
do { do {
char filename[MAX_PATH]; char filename[32];
sprintf(filename, "%s/%d-%s.%s", reportpath(), turn, factionid(f), char path[MAX_PATH];
sprintf(filename, "%d-%s.%s", turn, factionid(f),
rtype->extension); rtype->extension);
join_path(reportpath(), filename, path, sizeof(path));
error = 0; error = 0;
if (rtype->write(filename, &ctx, encoding) == 0) { if (rtype->write(path, &ctx, encoding) == 0) {
gotit = true; gotit = true;
} }
if (errno) { if (errno) {
@ -1657,15 +1659,6 @@ int reports(void)
if (mailit) if (mailit)
fclose(mailit); fclose(mailit);
free_seen(); free_seen();
#ifdef GLOBAL_REPORT
{
const char *str = config_get("globalreport");
if (str != NULL) {
sprintf(path, "%s/%s.%u.cr", reportpath(), str, turn);
global_report(path);
}
}
#endif
return retval; return retval;
} }

View File

@ -152,7 +152,7 @@ static void writeturn(void)
char zText[MAX_PATH]; char zText[MAX_PATH];
FILE *f; FILE *f;
sprintf(zText, "%s/datum", basepath()); join_path(basepath(), "datum", zText, sizeof(zText));
f = fopen(zText, "w"); f = fopen(zText, "w");
if (!f) { if (!f) {
perror(zText); perror(zText);
@ -160,7 +160,7 @@ static void writeturn(void)
} }
fputs(gamedate2(default_locale), f); fputs(gamedate2(default_locale), f);
fclose(f); fclose(f);
sprintf(zText, "%s/turn", basepath()); join_path(basepath(), "turn", zText, sizeof(zText));
f = fopen(zText, "w"); f = fopen(zText, "w");
if (!f) { if (!f) {
perror(zText); perror(zText);
@ -178,10 +178,10 @@ void report_summary(summary * s, summary * o, bool full)
char zText[MAX_PATH]; char zText[MAX_PATH];
if (full) { if (full) {
sprintf(zText, "%s/parteien.full", basepath()); join_path(basepath(), "parteien.full", zText, sizeof(zText));
} }
else { else {
sprintf(zText, "%s/parteien", basepath()); join_path(basepath(), "parteien", zText, sizeof(zText));
} }
F = fopen(zText, "w"); F = fopen(zText, "w");
if (!F) { if (!F) {

View File

@ -58,10 +58,6 @@ bool list = false;
int RunAllTests(int argc, char *argv[]) int RunAllTests(int argc, char *argv[])
{ {
int flags = log_flags;
log_flags = LOG_FLUSH | LOG_CPERROR;
/* self-test */ /* self-test */
ADD_SUITE(tests); ADD_SUITE(tests);
ADD_SUITE(callback); ADD_SUITE(callback);
@ -146,7 +142,6 @@ int RunAllTests(int argc, char *argv[])
suites = s; suites = s;
} }
printf("\ntest summary: %d tests, %d failed\n", summary->count, summary->failCount); printf("\ntest summary: %d tests, %d failed\n", summary->count, summary->failCount);
log_flags = flags;
fail_count = summary->failCount; fail_count = summary->failCount;
CuSuiteDelete(summary); CuSuiteDelete(summary);
game_done(); game_done();

View File

@ -31,22 +31,23 @@ static int stdio_codepage = STDIO_CP;
static int stdio_codepage = 0; static int stdio_codepage = 0;
#endif #endif
typedef struct logger { typedef struct log_t {
void(*log)(void *data, int level, const char *module, const char *format, va_list args); void(*log)(void *data, int level, const char *module, const char *format, va_list args);
void *data; void *data;
int flags; int flags;
struct logger *next; struct log_t *next;
} logger; } log_t;
static logger *loggers; static log_t *loggers;
void log_create(int flags, void *data, log_fun call) { log_t *log_create(int flags, void *data, log_fun call) {
logger *lgr = malloc(sizeof(logger)); log_t *lgr = malloc(sizeof(log_t));
lgr->log = call; lgr->log = call;
lgr->flags = flags; lgr->flags = flags;
lgr->data = data; lgr->data = data;
lgr->next = loggers; lgr->next = loggers;
loggers = lgr; loggers = lgr;
return lgr;
} }
#define MAXLENGTH 4096 /* because I am lazy, CP437 output is limited to this many chars */ #define MAXLENGTH 4096 /* because I am lazy, CP437 output is limited to this many chars */
@ -122,10 +123,8 @@ static int check_dupe(const char *format, int type)
return 1; return 1;
} }
if (dupes) { if (dupes) {
if (log_flags & LOG_CPERROR) {
fprintf(stderr, "%s: last message repeated %d times\n", log_prefix(last_type), fprintf(stderr, "%s: last message repeated %d times\n", log_prefix(last_type),
dupes + 1); dupes + 1);
}
dupes = 0; dupes = 0;
} }
strlcpy(last_message, format, sizeof(last_message)); strlcpy(last_message, format, sizeof(last_message));
@ -167,12 +166,12 @@ static void log_stdio(void *data, int level, const char *module, const char *for
} }
} }
void log_to_file(int flags, FILE *out) { log_t *log_to_file(int flags, FILE *out) {
log_create(flags, out, log_stdio); return log_create(flags, out, log_stdio);
} }
static void log_write(int flags, const char *module, const char *format, va_list args) { static void log_write(int flags, const char *module, const char *format, va_list args) {
logger *lg; log_t *lg;
for (lg = loggers; lg; lg = lg->next) { for (lg = loggers; lg; lg = lg->next) {
int level = flags & LOG_LEVELS; int level = flags & LOG_LEVELS;
if (lg->flags & level) { if (lg->flags & level) {
@ -241,7 +240,7 @@ static FILE *logfile;
void log_close(void) void log_close(void)
{ {
while (loggers) { while (loggers) {
logger *lgr = loggers; log_t *lgr = loggers;
loggers = lgr->next; loggers = lgr->next;
free(lgr); free(lgr);
} }
@ -254,7 +253,7 @@ void log_close(void)
logfile = 0; logfile = 0;
} }
void log_open(const char *filename) log_t *log_open(const char *filename, int log_flags)
{ {
log_rotate(filename, LOG_MAXBACKUPS); log_rotate(filename, LOG_MAXBACKUPS);
logfile = fopen(filename, "a"); logfile = fopen(filename, "a");
@ -263,6 +262,14 @@ void log_open(const char *filename)
time_t ltime; time_t ltime;
time(&ltime); time(&ltime);
fprintf(logfile, "===\n=== Logfile started at %s===\n", ctime(&ltime)); fprintf(logfile, "===\n=== Logfile started at %s===\n", ctime(&ltime));
log_create(log_flags, logfile, log_stdio); return log_create(log_flags, logfile, log_stdio);
} }
return 0;
}
int log_level(log_t * log, int flags)
{
int old = log->flags;
log->flags = flags;
return old;
} }

View File

@ -17,10 +17,16 @@ extern "C" {
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
extern void log_open(const char *filename); struct log_t;
extern void log_close(void);
typedef void(*log_fun)(void *data, int level, const char *module, const char *format, va_list args);
struct log_t * log_open(const char *filename, int flags);
struct log_t * log_create(int flags, void *data, log_fun call);
struct log_t * log_to_file(int flags, FILE *out);
int log_level(struct log_t *log, int flags);
void log_close(void);
/* use macros above instead of these: */
extern void log_fatal(const char *format, ...); extern void log_fatal(const char *format, ...);
extern void log_error(const char *format, ...); extern void log_error(const char *format, ...);
extern void log_warning(const char *format, ...); extern void log_warning(const char *format, ...);
@ -36,13 +42,6 @@ extern "C" {
#define LOG_FLUSH 0x10 #define LOG_FLUSH 0x10
#define LOG_BRIEF 0x20 #define LOG_BRIEF 0x20
typedef void(*log_fun)(void *data, int level, const char *module, const char *format, va_list args);
void log_create(int flags, void *data, log_fun call);
void log_to_file(int flags, FILE *out);
extern int log_flags;
extern int log_stderr;
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

1
tests/score Normal file
View File

@ -0,0 +1 @@


3
tests/score.alliances Normal file
View File

@ -0,0 +1,3 @@
# alliance:factions:persons:score
1248287:1:0:0
1490214:1:0:2105