From 29305bc302044f27e630c7d56d96fe088642388e Mon Sep 17 00:00:00 2001 From: ketmar Date: Fri, 16 Mar 2012 06:40:19 +0200 Subject: [PATCH] resource manager now will search for resource file in various places --- src/awish.c | 5 +---- src/resfile.c | 41 ++++++++++++++++++++++++++++++++++++++++- src/resfile.h | 2 +- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/awish.c b/src/awish.c index fe11534..05be863 100644 --- a/src/awish.c +++ b/src/awish.c @@ -306,10 +306,7 @@ int main (int argc, char *argv[]) { --f; } // - if (initResFile(&resfile, "RESOURCE.DAT") != 0) { - if (goobers) fprintf(stderr, "can't open RESOURCE.DAT!\n"); - //exit(1); - } + initResFile(&resfile, "RESOURCE.DAT"); atexit(quitCleanupRes); // if (loadFont() != 0) { diff --git a/src/resfile.c b/src/resfile.c index 225bbcd..00435d1 100644 --- a/src/resfile.c +++ b/src/resfile.c @@ -62,6 +62,7 @@ static const char *getMyDir (void) { p = strrchr(myDir, '\\'); if (!p) strcpy(myDir, "."); else *p = '\0'; strcat(myDir, "\\data"); + for (p = myDir; *p; ++p) if (*p == '\\') *p = '/'; #endif inited = 1; } @@ -83,6 +84,7 @@ static const char *getHomeDir (void) { #else // fuck windoze strcpy(homeDir, "."); + for (char *p = myDir; *p; ++p) if (*p == '\\') *p = '/'; #endif inited = 1; } @@ -91,9 +93,10 @@ static const char *getHomeDir (void) { #endif -int initResFile (ResFile *resfile, const char *fname) { +static int tryInitResFile (ResFile *resfile, const char *fname) { uint8_t fcnt; uint8_t sign[3]; + // FILE *fl = fopen(fname, "rb"); int rr = -1, sz; // @@ -134,6 +137,42 @@ quit: } +int initResFile (ResFile *resfile, const char *rfname) { + static char fname[8192]; + int res = -1; + // +#ifndef _WIN32 + sprintf(fname, "%s/%s", getHomeDir(), rfname); + res = tryInitResFile(resfile, fname); +#endif + if (res != 0) { + sprintf(fname, "%s/%s", getMyDir(), rfname); + res = tryInitResFile(resfile, fname); + } +#ifndef _WIN32 + if (res != 0) { + sprintf(fname, "%s/%s", DATA_DIR, rfname); + res = tryInitResFile(resfile, fname); + } +#endif + if (res != 0) { + char *t; + // + strcpy(fname, getMyDir()); + t = strrchr(fname, '/'); + if (t) t[1] = 0; + strcat(fname, rfname); + res = tryInitResFile(resfile, fname); + } + // + if (goobers) { + if (res == 0) fprintf(stderr, "using resource file '%s'\n", fname); + else fprintf(stderr, "can't find resource file '%s'\n", rfname); + } + return res; +} + + void deinitResFile (ResFile *resfile) { if (resfile) { if (resfile->fl) fclose(resfile->fl); diff --git a/src/resfile.h b/src/resfile.h index 9318448..a8b6a20 100644 --- a/src/resfile.h +++ b/src/resfile.h @@ -30,7 +30,7 @@ typedef struct { } ResFile; -extern int initResFile (ResFile *resfile, const char *fname); +extern int initResFile (ResFile *resfile, const char *rfname); extern void deinitResFile (ResFile *resfile); extern uint8_t *loadResFile (ResFile *resfile, int idx, int *rsz); -- 2.11.4.GIT