themes now in idf too

This commit is contained in:
p.kosyh 2011-04-18 13:39:20 +00:00
parent 667c99940a
commit 4938797918
7 changed files with 87 additions and 11 deletions

View file

@ -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);

View file

@ -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;

View file

@ -1,5 +1,6 @@
#ifndef __IDF_H_INCLUDED
#define __IDF_H_INCLUDED
#include <SDL_rwops.h>
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

View file

@ -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;

View file

@ -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);

View file

@ -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);

View file

@ -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);