X-Git-Url: https://jasonwoof.com/gitweb/?p=vor.git;a=blobdiff_plain;f=file.c;h=3a02b81f38fbd0c958416ece2d7a0ba09a3a5d92;hp=57cfc56df1a3cf87a14bb231a8ff766e2c7e2ca8;hb=1d9107a3d43b3f57087edee14f1eaf7c1f3db54e;hpb=e10d6c0b95e2f4fe7acd75733370dae71bb3b73f diff --git a/file.c b/file.c index 57cfc56..3a02b81 100644 --- a/file.c +++ b/file.c @@ -16,29 +16,78 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include "config.h" -#include "file.h" - #include #include #include -#include +#ifndef WIN32 +# include +#endif #include #include +#include "common.h" +#include "config.h" +#include "file.h" + +#ifdef WIN32 + +#define DATA_DIR "data\\" + +char * +add_data_path(char *filename) +{ + char *s; + if(!filename) return filename; + s = malloc(sizeof(DATA_DIR)+strlen(filename)); + if(s) { + strcpy(s, DATA_DIR); + strcpy(s+sizeof(DATA_DIR)-1, filename); + } + return s; +} + +bool +find_files(void) +{ + return true; +} + +FILE *open_score_file(char *mode) +{ + return fopen("scores", mode); +} + + +#else /* !WIN32 */ + + char *g_data_dir; char *g_score_file; -mode_t g_score_mode; + +static char * +add_path(char *path, char *file) +{ + char *s; + size_t plen, flen; + + if(!path || !file) return NULL; + plen = strlen(path); + flen = strlen(file); + s = malloc(2+plen+flen); + if(!s) return NULL; + memcpy(s, path, plen); + s[plen] = '/'; + memcpy(s+plen+1, file, flen+1); + return s; +} char * -add_path(char *filename) +add_data_path(char *filename) { - static char r[MAX_PATH_LEN]; - snprintf(r, MAX_PATH_LEN, "%s/%s", g_data_dir, filename); - return r; + return add_path(g_data_dir, filename); } -int +static bool is_dir(char *dirname) { struct stat buf; @@ -46,21 +95,13 @@ is_dir(char *dirname) return S_ISDIR(buf.st_mode); } -int -is_file(char *filename) -{ - struct stat buf; - stat(filename, &buf); - return S_ISREG(buf.st_mode); -} - -int +static bool find_data_dir(void) { int i; char *data_options[3] = { - "./data", getenv("VOR_DATA"), + "data", DATA_PREFIX }; @@ -77,42 +118,33 @@ find_data_dir(void) return false; } -int +static bool find_score_file(void) { - g_score_file = add_path("scores"); - g_score_mode = 0111; - if(is_file(g_score_file)) return true; - - g_score_file = malloc(MAX_PATH_LEN); - snprintf(g_score_file, MAX_PATH_LEN, - "%s/.vor-high", getenv("HOME")); - g_score_mode = 0177; - if(is_file(g_score_file)) return true; - - return false; + char *dir, *s; + + /* in case we get called twice */ + if(g_score_file) return true; + + dir = getenv("HOME"); if(!dir) return false; + s = add_path(dir, ".vor-scores"); + if(s) { + g_score_file = s; + return true; + } else return false; } -int +bool find_files(void) { - int r; - r = find_data_dir(); - find_score_file(); - return r; + return find_data_dir() && find_score_file(); } FILE * open_score_file(char *mode) { - mode_t old_mask; - FILE *f = NULL; - - if(!g_score_file) return f; - - old_mask = umask(g_score_mode); - f = fopen(g_score_file, mode); - - umask(old_mask); - return f; + if(!g_score_file) return NULL; + return fopen(g_score_file, mode); } + +#endif /* !WIN32 */