diff options
author | Chris Robinson <[email protected]> | 2014-02-27 01:49:23 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2014-02-27 01:49:23 -0800 |
commit | a8249c3469e1b0162030f767b5ffedc20c1622a4 (patch) | |
tree | 005e53add3c862f84fad9492e2afb50d4e99802f /Alc/helpers.c | |
parent | 3145bac8c6bf3f97f5232b801eb7ecac7f95e67f (diff) |
Move OpenDataFile to helpers.c so other sources can use it
Diffstat (limited to 'Alc/helpers.c')
-rw-r--r-- | Alc/helpers.c | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/Alc/helpers.c b/Alc/helpers.c index f8a5f13b..4b3973d9 100644 --- a/Alc/helpers.c +++ b/Alc/helpers.c @@ -18,12 +18,21 @@ * Or go to http://www.gnu.org/copyleft/lgpl.html */ +#ifdef _WIN32 +#ifdef __MINGW32__ +#define _WIN32_IE 0x501 +#else +#define _WIN32_IE 0x400 +#endif +#endif + #include "config.h" #include <stdlib.h> #include <time.h> #include <errno.h> #include <stdarg.h> +#include <limits.h> #ifdef HAVE_MALLOC_H #include <malloc.h> #endif @@ -71,6 +80,10 @@ DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, #include <ieeefp.h> #endif +#ifdef _WIN32_IE +#include <shlobj.h> +#endif + #include "alMain.h" #include "atomic.h" #include "uintmap.h" @@ -94,6 +107,15 @@ extern inline ALint fastf2i(ALfloat f); extern inline ALuint fastf2u(ALfloat f); +#ifndef PATH_MAX +#ifdef MAX_PATH +#define PATH_MAX MAX_PATH +#else +#define PATH_MAX 4096 +#endif +#endif + + ALuint CPUCapFlags = 0; @@ -501,6 +523,117 @@ void al_print(const char *type, const char *func, const char *fmt, ...) } +FILE *OpenDataFile(const char *fname, const char *subdir) +{ + char buffer[PATH_MAX] = ""; + FILE *f; + +#ifdef _WIN32 + static const int ids[2] = { CSIDL_APPDATA, CSIDL_COMMON_APPDATA }; + int i; + + /* If the path is absolute, open it directly. */ + if(fname[0] != '\0' && fname[1] == ':' && (fname[2] == '\\' || fname[2] == '/')) + { + if((f=fopen(fname, "rb")) != NULL) + { + TRACE("Opened %s\n", fname); + return f; + } + WARN("Could not open %s\n", fname); + return NULL; + } + + for(i = 0;i < 2;i++) + { + size_t len; + + if(SHGetSpecialFolderPathA(NULL, buffer, ids[i], FALSE) == FALSE) + continue; + + len = strlen(buffer); + if(len > 0 && (buffer[len-1] == '\\' || buffer[len-1] == '/')) + buffer[--len] = '\0'; + snprintf(buffer+len, sizeof(buffer)-len, "/%s/%s", subdir, fname); + len = strlen(buffer); + while(len > 0) + { + --len; + if(buffer[len] == '/') + buffer[len] = '\\'; + } + + if((f=fopen(buffer, "rb")) != NULL) + { + TRACE("Opened %s\n", buffer); + return f; + } + WARN("Could not open %s\n", buffer); + } +#else + const char *str, *next; + + if(fname[0] == '/') + { + if((f=fopen(fname, "rb")) != NULL) + { + TRACE("Opened %s\n", fname); + return f; + } + WARN("Could not open %s\n", fname); + return NULL; + } + + if((str=getenv("XDG_DATA_HOME")) != NULL && str[0] != '\0') + snprintf(buffer, sizeof(buffer), "%s/%s/%s", str, subdir, fname); + else if((str=getenv("HOME")) != NULL && str[0] != '\0') + snprintf(buffer, sizeof(buffer), "%s/.local/share/%s/%s", str, subdir, fname); + if(buffer[0]) + { + if((f=fopen(buffer, "rb")) != NULL) + { + TRACE("Opened %s\n", buffer); + return f; + } + WARN("Could not open %s\n", buffer); + } + + if((str=getenv("XDG_DATA_DIRS")) == NULL || str[0] == '\0') + str = " /usr/local/share/:/usr/share/"; + + next = str; + while((str=next) != NULL && str[0] != '\0') + { + size_t len; + next = strchr(str, ':'); + + if(!next) + len = strlen(str); + else + { + len = next - str; + next++; + } + + if(len > sizeof(buffer)-1) + len = sizeof(buffer)-1; + strncpy(buffer, str, len); + buffer[len] = '\0'; + snprintf(buffer+len, sizeof(buffer)-len, "/%s/%s", subdir, fname); + + if((f=fopen(buffer, "rb")) != NULL) + { + TRACE("Opened %s\n", buffer); + return f; + } + WARN("Could not open %s\n", buffer); + } +#endif + + return NULL; +} + + void SetRTPriority(void) { ALboolean failed = AL_FALSE; |