From 4938797918966ff0b55ec311ec802dde969fca4b Mon Sep 17 00:00:00 2001 From: "p.kosyh" Date: Mon, 18 Apr 2011 13:39:20 +0000 Subject: [PATCH] themes now in idf too --- src/sdl-instead/game.c | 4 ++-- src/sdl-instead/idf.c | 30 +++++++++++++++++++++++++++++ src/sdl-instead/idf.h | 3 +++ src/sdl-instead/themes.c | 14 ++++++++++++-- src/sdl-instead/unix.c | 2 +- src/sdl-instead/util.c | 41 ++++++++++++++++++++++++++++++++++------ src/sdl-instead/util.h | 4 ++++ 7 files changed, 87 insertions(+), 11 deletions(-) diff --git a/src/sdl-instead/game.c b/src/sdl-instead/game.c index 6c16e45..6f4871f 100644 --- a/src/sdl-instead/game.c +++ b/src/sdl-instead/game.c @@ -731,12 +731,12 @@ int game_use_theme(void) return -1; } - if (curgame_dir && !access(dirpath(THEME_FILE), R_OK)) { + if (curgame_dir && (!idf_access(game_idf, THEME_FILE) || !access(dirpath(THEME_FILE), R_OK))) { game_own_theme = 1; } if (game_own_theme && opt_owntheme) { theme_relative = 1; - rc = theme_load(dirpath(THEME_FILE)); + rc = theme_load(THEME_FILE); theme_relative = 0; } else if (curtheme_dir && strlowcmp(DEFAULT_THEME, curtheme_dir)) { rc = game_theme_load(curtheme_dir); diff --git a/src/sdl-instead/idf.c b/src/sdl-instead/idf.c index 37711c8..dbaa509 100644 --- a/src/sdl-instead/idf.c +++ b/src/sdl-instead/idf.c @@ -431,6 +431,7 @@ int idf_error(idff_t idf) return -1; return ferror(idf->fd); } + idff_t idf_open(idf_t idf, const char *fname) { idfd_t *dir = NULL; @@ -455,6 +456,35 @@ err: return NULL; } +int idf_access(idf_t idf, const char *fname) +{ + idfd_t *dir = NULL; + if (idf) + dir = cache_lookup(idf->dir, fname); + if (!dir) + return -1; + return 0; +} + +char *idf_gets(idff_t idf, char *b, int size) +{ + int rc, rc2; + if (!idf) + return NULL; + if (!size) + return NULL; + rc = idf_read(idf, b, 1, size); + if (rc < 0) + return NULL; + if (!rc && idf_eof(idf)) + return NULL; + b[rc - 1] = 0; + rc2 = strcspn(b, "\n"); + b[rc2] = 0; + idf_seek(idf, - (rc - rc2 - 1), SEEK_CUR); + return b; +} + SDL_RWops *RWFromIdf(idf_t idf, const char *fname) { idff_t fil = NULL; diff --git a/src/sdl-instead/idf.h b/src/sdl-instead/idf.h index 2f6f58e..b53d05c 100644 --- a/src/sdl-instead/idf.h +++ b/src/sdl-instead/idf.h @@ -1,5 +1,6 @@ #ifndef __IDF_H_INCLUDED #define __IDF_H_INCLUDED +#include struct _idf_t; struct _idff_t; @@ -20,5 +21,7 @@ extern int idf_close(idff_t fil); extern int idf_eof(idff_t idf); extern int idf_error(idff_t idf); +extern int idf_access(idf_t idf, const char *fname); +extern char *idf_gets(idff_t idf, char *b, int size); #endif \ No newline at end of file diff --git a/src/sdl-instead/themes.c b/src/sdl-instead/themes.c index 9502818..a9b178e 100644 --- a/src/sdl-instead/themes.c +++ b/src/sdl-instead/themes.c @@ -737,7 +737,17 @@ int game_theme_init(void) static int theme_parse(const char *path) { - if (parse_ini(path, cmd_parser)) { + idff_t idf = idf_open(game_idf, path); + + if (idf) { + int rc = parse_idff(idf, path, cmd_parser); + idf_close(idf); + if (rc) + fprintf(stderr, "Theme parsed with errors!\n"); + return rc; + } + + if (parse_ini(dirpath(path), cmd_parser)) { fprintf(stderr, "Theme parsed with errors!\n"); // game_theme_free(); return -1; @@ -886,7 +896,7 @@ int game_theme_load(const char *name) setdir(game_cwd); theme = theme_lookup(name); theme_relative = 0; - if (!theme || setdir(theme->path) || theme_load(dirpath(THEME_FILE))) { + if (!theme || setdir(theme->path) || theme_load(THEME_FILE)) { setdir(cwd); theme_relative = rel; return -1; diff --git a/src/sdl-instead/unix.c b/src/sdl-instead/unix.c index ceede94..4100f86 100644 --- a/src/sdl-instead/unix.c +++ b/src/sdl-instead/unix.c @@ -304,7 +304,7 @@ char *sdl_path(char *p) unix_path(p); return p; } -#if 1 +#if 0 int setdir(const char *path) { return chdir(path); diff --git a/src/sdl-instead/util.c b/src/sdl-instead/util.c index 583ee93..043057e 100644 --- a/src/sdl-instead/util.c +++ b/src/sdl-instead/util.c @@ -1,6 +1,7 @@ #include "externals.h" #include "config.h" #include "util.h" +#include "idf.h" void tolow(char *p) { @@ -98,13 +99,13 @@ int process_cmd(char *n, char *v, struct parser *cmd_parser) return -1; } -static int fgetsesc(char *oline, size_t size, FILE *fp) +static int fgetsesc(char *oline, size_t size, char *(*getl)(void *p, char *s, int size), void *fp) { int nr = 0; char line[4096]; *oline = 0; *line = 0; - while (fgets(line, sizeof(line), fp)) { + while (getl(fp, line, sizeof(line))) { int i; nr ++; i = strcspn(line, "\n\r"); @@ -152,17 +153,17 @@ static void comments_zap(char *p) *l = 0; } -int parse_ini(const char *path, struct parser *cmd_parser) +int parse_all(void *fp, char *(*getl)(void *p, char *s, int size), const char *path, struct parser *cmd_parser) { int nr; int rc = 0; int line_nr = 1; - FILE *fp; + char line[4096]; - fp = fopen(path, "rb"); if (!fp) return -1; - while ((nr = fgetsesc(line, sizeof(line), fp))) { + + while ((nr = fgetsesc(line, sizeof(line), getl, fp))) { char *p = line; char *val; int len; @@ -186,10 +187,38 @@ int parse_ini(const char *path, struct parser *cmd_parser) fprintf(stderr, "Can't process cmd '%s' on line %d in '%s': %s\n", p, line_nr - nr, path, strerror(errno)); } } + return rc; +} + +static char *file_gets(void *fd, char *s, int size) +{ + return fgets(s, size, (FILE *)fd); +} + +static char *idff_gets(void *fd, char *s, int size) +{ + return idf_gets((idff_t)fd, s, size); +} + +int parse_ini(const char *path, struct parser *cmd_parser) +{ + int rc = 0; + FILE *fp; + fp = fopen(path, "rb"); + if (!fp) + return -1; + rc = parse_all(fp, file_gets, path, cmd_parser); fclose(fp); return rc; } +int parse_idff(idff_t idff, const char *path, struct parser *cmd_parser) +{ + if (!idff) + return -1; + return parse_all(idff, idff_gets, path, cmd_parser); +} + int parse_string(const char *v, void *data) { char **p = ((char **)data); diff --git a/src/sdl-instead/util.h b/src/sdl-instead/util.h index a561245..c80ddfd 100644 --- a/src/sdl-instead/util.h +++ b/src/sdl-instead/util.h @@ -1,6 +1,8 @@ #ifndef __UTIL_H_INCLUDED #define __UTIL_H_INCLUDED +#include "idf.h" + typedef int (*parser_fn)(const char *v, void *data); struct parser { @@ -14,6 +16,8 @@ extern int is_space(int c); extern int is_empty(const char *str); extern int parse_ini(const char *path, struct parser *cmd_parser); +extern int parse_idff(idff_t idff, const char *path, struct parser *cmd_parser); + extern char *getpath(const char *d, const char *n); extern char *strip(char *s); char *getfilepath(const char *d, const char *n);