diff options
author | Chris Robinson <[email protected]> | 2018-11-11 19:17:40 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2018-11-11 19:17:40 -0800 |
commit | 51ed335833dab0cee5fc325e3a5c1968da934a49 (patch) | |
tree | 15b47ca5a508c2cb2f1147468e1c479fc2b75439 | |
parent | 4793e5c4ae3472b1f931ec48f913614710a6d12c (diff) |
Use C++ more with helpers.cpp
-rw-r--r-- | Alc/helpers.cpp | 365 | ||||
-rw-r--r-- | Alc/hrtf.cpp | 84 | ||||
-rw-r--r-- | OpenAL32/Include/alMain.h | 8 |
3 files changed, 171 insertions, 286 deletions
diff --git a/Alc/helpers.cpp b/Alc/helpers.cpp index dede9e06..bfb458b7 100644 --- a/Alc/helpers.cpp +++ b/Alc/helpers.cpp @@ -109,6 +109,7 @@ DEFINE_PROPERTYKEY(PKEY_AudioEndpoint_GUID, 0x1da5d803, 0xd492, 0x4edd, 0x8c, 0x #include <shlobj.h> #endif +#include <atomic> #include <vector> #include <string> #include <algorithm> @@ -213,7 +214,7 @@ void FillCPUCaps(int capfilter) al_string features = AL_STRING_INIT_STATIC(); char buf[256]; - while(fgets(buf, sizeof(buf), file) != NULL) + while(fgets(buf, sizeof(buf), file) != nullptr) { if(strncmp(buf, "Features\t:", 10) != 0) continue; @@ -221,14 +222,14 @@ void FillCPUCaps(int capfilter) alstr_copy_cstr(&features, buf+10); while(VECTOR_BACK(features) != '\n') { - if(fgets(buf, sizeof(buf), file) == NULL) + if(fgets(buf, sizeof(buf), file) == nullptr) break; alstr_append_cstr(&features, buf); } break; } fclose(file); - file = NULL; + file = nullptr; if(!alstr_empty(features)) { @@ -236,7 +237,7 @@ void FillCPUCaps(int capfilter) while(isspace(str[0])) ++str; TRACE("Got features string:%s\n", str); - while((str=strstr(str, "neon")) != NULL) + while((str=strstr(str, "neon")) != nullptr) { if(isspace(*(str-1)) && (str[4] == 0 || isspace(str[4]))) { @@ -297,8 +298,8 @@ void RestoreFPUMode(const FPUCtl *ctl) #elif defined(HAVE___CONTROL87_2) int mode; - __control87_2(ctl->state, _MCW_DN, &mode, NULL); - __control87_2(ctl->sse_state, _MCW_DN, NULL, &mode); + __control87_2(ctl->state, _MCW_DN, &mode, nullptr); + __control87_2(ctl->sse_state, _MCW_DN, nullptr, &mode); #elif defined(HAVE__CONTROLFP) @@ -307,11 +308,6 @@ void RestoreFPUMode(const FPUCtl *ctl) } -static int StringSortCompare(const void *str1, const void *str2) -{ - return alstr_cmp(*(const_al_string*)str1, *(const_al_string*)str2); -} - #ifdef _WIN32 PathNamePair GetProcBinary() @@ -348,42 +344,16 @@ PathNamePair GetProcBinary() } -static WCHAR *FromUTF8(const char *str) -{ - WCHAR *out = NULL; - int len; - - if((len=MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0)) > 0) - { - out = static_cast<WCHAR*>(calloc(sizeof(WCHAR), len)); - MultiByteToWideChar(CP_UTF8, 0, str, -1, out, len); - } - return out; -} - - void *LoadLib(const char *name) { - HANDLE hdl = NULL; - WCHAR *wname; - - wname = FromUTF8(name); - if(!wname) - ERR("Failed to convert UTF-8 filename: \"%s\"\n", name); - else - { - hdl = LoadLibraryW(wname); - free(wname); - } - return hdl; + std::wstring wname{utf8_to_wstr(name)}; + return LoadLibraryW(wname.c_str()); } void CloseLib(void *handle) -{ FreeLibrary(reinterpret_cast<HMODULE>(handle)); } +{ FreeLibrary(static_cast<HMODULE>(handle)); } void *GetSymbol(void *handle, const char *name) { - void *ret; - - ret = reinterpret_cast<void*>(GetProcAddress(reinterpret_cast<HMODULE>(handle), name)); + void *ret{reinterpret_cast<void*>(GetProcAddress(static_cast<HMODULE>(handle), name))}; if(!ret) ERR("Failed to load %s\n", name); return ret; } @@ -392,23 +362,15 @@ void *GetSymbol(void *handle, const char *name) void al_print(const char *type, const char *func, const char *fmt, ...) { char str[1024]; - WCHAR *wstr; va_list ap; va_start(ap, fmt); vsnprintf(str, sizeof(str), fmt, ap); va_end(ap); - str[sizeof(str)-1] = 0; - wstr = FromUTF8(str); - if(!wstr) - fprintf(LogFile, "AL lib: %s %s: <UTF-8 error> %s", type, func, str); - else - { - fprintf(LogFile, "AL lib: %s %s: %ls", type, func, wstr); - free(wstr); - wstr = NULL; - } + + std::wstring wstr{utf8_to_wstr(str)}; + fprintf(LogFile, "AL lib: %s %s: %ls", type, func, wstr.c_str()); fflush(LogFile); } @@ -416,124 +378,93 @@ void al_print(const char *type, const char *func, const char *fmt, ...) static inline int is_slash(int c) { return (c == '\\' || c == '/'); } -static void DirectorySearch(const char *path, const char *ext, vector_al_string *results) +static void DirectorySearch(const char *path, const char *ext, std::vector<std::string> *const results) { - al_string pathstr = AL_STRING_INIT_STATIC(); - WIN32_FIND_DATAW fdata; - WCHAR *wpath; - HANDLE hdl; - - alstr_copy_cstr(&pathstr, path); - alstr_append_cstr(&pathstr, "\\*"); - alstr_append_cstr(&pathstr, ext); - - TRACE("Searching %s\n", alstr_get_cstr(pathstr)); - - wpath = FromUTF8(alstr_get_cstr(pathstr)); + std::string pathstr{path}; + pathstr += "\\*"; + pathstr += ext; + TRACE("Searching %s\n", pathstr.c_str()); - hdl = FindFirstFileW(wpath, &fdata); + std::wstring wpath{utf8_to_wstr(pathstr.c_str())}; + WIN32_FIND_DATAW fdata; + HANDLE hdl{FindFirstFileW(wpath.c_str(), &fdata)}; if(hdl != INVALID_HANDLE_VALUE) { - size_t base = VECTOR_SIZE(*results); + size_t base = results->size(); do { - al_string str = AL_STRING_INIT_STATIC(); - alstr_copy_cstr(&str, path); - alstr_append_char(&str, '\\'); - alstr_append_wcstr(&str, fdata.cFileName); - TRACE("Got result %s\n", alstr_get_cstr(str)); - VECTOR_PUSH_BACK(*results, str); + results->emplace_back(); + std::string &str = results->back(); + str = path; + str += '\\'; + str += wstr_to_utf8(fdata.cFileName); + TRACE("Got result %s\n", str.c_str()); } while(FindNextFileW(hdl, &fdata)); FindClose(hdl); - if(VECTOR_SIZE(*results) > base) - qsort(VECTOR_BEGIN(*results)+base, VECTOR_SIZE(*results)-base, - sizeof(VECTOR_FRONT(*results)), StringSortCompare); + std::sort(results->begin()+base, results->end()); } - - free(wpath); - alstr_reset(&pathstr); } -vector_al_string SearchDataFiles(const char *ext, const char *subdir) +std::vector<std::string> SearchDataFiles(const char *ext, const char *subdir) { - static const int ids[2] = { CSIDL_APPDATA, CSIDL_COMMON_APPDATA }; - static RefCount search_lock; - vector_al_string results = VECTOR_INIT_STATIC(); - size_t i; + static std::atomic<ALenum> search_lock{AL_FALSE}; - while(ATOMIC_EXCHANGE_SEQ(&search_lock, 1u) == 1u) + while(search_lock.exchange(AL_TRUE) == AL_TRUE) althrd_yield(); /* If the path is absolute, use it directly. */ + std::vector<std::string> results; if(isalpha(subdir[0]) && subdir[1] == ':' && is_slash(subdir[2])) { - al_string path = AL_STRING_INIT_STATIC(); - alstr_copy_cstr(&path, subdir); -#define FIX_SLASH(i) do { if(*(i) == '/') *(i) = '\\'; } while(0) - VECTOR_FOR_EACH(char, path, FIX_SLASH); -#undef FIX_SLASH - - DirectorySearch(alstr_get_cstr(path), ext, &results); - - alstr_reset(&path); + std::string path{subdir}; + std::replace(path.begin(), path.end(), '/', '\\'); + DirectorySearch(path.c_str(), ext, &results); } else if(subdir[0] == '\\' && subdir[1] == '\\' && subdir[2] == '?' && subdir[3] == '\\') DirectorySearch(subdir, ext, &results); else { - al_string path = AL_STRING_INIT_STATIC(); - WCHAR *cwdbuf; + std::string path; /* Search the app-local directory. */ - if((cwdbuf=_wgetenv(L"ALSOFT_LOCAL_PATH")) && *cwdbuf != '\0') + WCHAR *cwdbuf{_wgetenv(L"ALSOFT_LOCAL_PATH")}; + if(cwdbuf && *cwdbuf != '\0') { - alstr_copy_wcstr(&path, cwdbuf); - if(is_slash(VECTOR_BACK(path))) - { - VECTOR_POP_BACK(path); - *VECTOR_END(path) = 0; - } + path = wstr_to_utf8(cwdbuf); + if(is_slash(path.back())) + path.pop_back(); } - else if(!(cwdbuf=_wgetcwd(NULL, 0))) - alstr_copy_cstr(&path, "."); + else if(!(cwdbuf=_wgetcwd(nullptr, 0))) + path = "."; else { - alstr_copy_wcstr(&path, cwdbuf); - if(is_slash(VECTOR_BACK(path))) - { - VECTOR_POP_BACK(path); - *VECTOR_END(path) = 0; - } + path = wstr_to_utf8(cwdbuf); + if(is_slash(path.back())) + path.pop_back(); free(cwdbuf); } -#define FIX_SLASH(i) do { if(*(i) == '/') *(i) = '\\'; } while(0) - VECTOR_FOR_EACH(char, path, FIX_SLASH); -#undef FIX_SLASH - DirectorySearch(alstr_get_cstr(path), ext, &results); + std::replace(path.begin(), path.end(), '/', '\\'); + DirectorySearch(path.c_str(), ext, &results); /* Search the local and global data dirs. */ - for(i = 0;i < COUNTOF(ids);i++) + static constexpr int ids[2] = { CSIDL_APPDATA, CSIDL_COMMON_APPDATA }; + for(int id : ids) { WCHAR buffer[MAX_PATH]; - if(SHGetSpecialFolderPathW(NULL, buffer, ids[i], FALSE) != FALSE) - { - alstr_copy_wcstr(&path, buffer); - if(!is_slash(VECTOR_BACK(path))) - alstr_append_char(&path, '\\'); - alstr_append_cstr(&path, subdir); -#define FIX_SLASH(i) do { if(*(i) == '/') *(i) = '\\'; } while(0) - VECTOR_FOR_EACH(char, path, FIX_SLASH); -#undef FIX_SLASH - - DirectorySearch(alstr_get_cstr(path), ext, &results); - } - } + if(SHGetSpecialFolderPathW(nullptr, buffer, id, FALSE) == FALSE) + continue; - alstr_reset(&path); - } + path = wstr_to_utf8(buffer); + if(!is_slash(path.back())) + path += '\\'; + path += subdir; + std::replace(path.begin(), path.end(), '/', '\\'); - ATOMIC_STORE_SEQ(&search_lock, 0u); + DirectorySearch(path.c_str(), ext, &results); + } + } + search_lock.store(AL_FALSE); return results; } @@ -547,7 +478,7 @@ PathNamePair GetProcBinary() #ifdef __FreeBSD__ size_t pathlen; int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; - if(sysctl(mib, 4, NULL, &pathlen, NULL, 0) == -1) + if(sysctl(mib, 4, nullptr, &pathlen, nullptr, 0) == -1) WARN("Failed to sysctl kern.proc.pathname: %s\n", strerror(errno)); else { @@ -626,28 +557,23 @@ PathNamePair GetProcBinary() void *LoadLib(const char *name) { - const char *err; - void *handle; - dlerror(); - handle = dlopen(name, RTLD_NOW); - if((err=dlerror()) != NULL) - handle = NULL; + void *handle{dlopen(name, RTLD_NOW)}; + const char *err{dlerror()}; + if(err) handle = nullptr; return handle; } void CloseLib(void *handle) { dlclose(handle); } void *GetSymbol(void *handle, const char *name) { - const char *err; - void *sym; - dlerror(); - sym = dlsym(handle, name); - if((err=dlerror()) != NULL) + void *sym{dlsym(handle, name)}; + const char *err{dlerror()}; + if(err) { WARN("Failed to load %s: %s\n", name, err); - sym = NULL; + sym = nullptr; } return sym; } @@ -667,139 +593,122 @@ void al_print(const char *type, const char *func, const char *fmt, ...) } -static void DirectorySearch(const char *path, const char *ext, vector_al_string *results) +static void DirectorySearch(const char *path, const char *ext, std::vector<std::string> *const results) { - size_t extlen = strlen(ext); - DIR *dir; - TRACE("Searching %s for *%s\n", path, ext); - dir = opendir(path); - if(dir != NULL) + DIR *dir{opendir(path)}; + if(dir != nullptr) { - size_t base = VECTOR_SIZE(*results); + const size_t extlen = strlen(ext); + size_t base = results->size(); + struct dirent *dirent; - while((dirent=readdir(dir)) != NULL) + while((dirent=readdir(dir)) != nullptr) { - al_string str; - size_t len; if(strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name, "..") == 0) continue; - len = strlen(dirent->d_name); - if(len <= extlen) - continue; + size_t len{strlen(dirent->d_name)}; + if(len <= extlen) continue; if(strcasecmp(dirent->d_name+len-extlen, ext) != 0) continue; - AL_STRING_INIT(str); - alstr_copy_cstr(&str, path); - if(VECTOR_BACK(str) != '/') - alstr_append_char(&str, '/'); - alstr_append_cstr(&str, dirent->d_name); - TRACE("Got result %s\n", alstr_get_cstr(str)); - VECTOR_PUSH_BACK(*results, str); + results->emplace_back(); + std::string &str = results->back(); + str = path; + if(str.back() != '/') + str.push_back('/'); + str += dirent->d_name; + TRACE("Got result %s\n", str.c_str()); } closedir(dir); - if(VECTOR_SIZE(*results) > base) - qsort(VECTOR_BEGIN(*results)+base, VECTOR_SIZE(*results)-base, - sizeof(VECTOR_FRONT(*results)), StringSortCompare); + std::sort(results->begin()+base, results->end()); } } -vector_al_string SearchDataFiles(const char *ext, const char *subdir) +std::vector<std::string> SearchDataFiles(const char *ext, const char *subdir) { - static RefCount search_lock; - vector_al_string results = VECTOR_INIT_STATIC(); + static std::atomic<ALenum> search_lock{AL_FALSE}; - while(ATOMIC_EXCHANGE_SEQ(&search_lock, 1u) == 1u) + while(search_lock.exchange(AL_TRUE) == AL_TRUE) althrd_yield(); + std::vector<std::string> results; if(subdir[0] == '/') DirectorySearch(subdir, ext, &results); else { - al_string path = AL_STRING_INIT_STATIC(); - const char *str, *next; - /* Search the app-local directory. */ - if((str=getenv("ALSOFT_LOCAL_PATH")) && *str != '\0') + const char *str{getenv("ALSOFT_LOCAL_PATH")}; + if(str && *str != '\0') DirectorySearch(str, ext, &results); else { - size_t cwdlen = 256; - char *cwdbuf = static_cast<char*>(malloc(cwdlen)); - while(!getcwd(cwdbuf, cwdlen)) + std::vector<char> cwdbuf(256); + while(!getcwd(cwdbuf.data(), cwdbuf.size())) { - free(cwdbuf); - cwdbuf = NULL; if(errno != ERANGE) + { + cwdbuf.clear(); break; - cwdlen <<= 1; - cwdbuf = static_cast<char*>(malloc(cwdlen)); + } + cwdbuf.resize(cwdbuf.size() << 1); } - if(!cwdbuf) + if(cwdbuf.empty()) DirectorySearch(".", ext, &results); else { - DirectorySearch(cwdbuf, ext, &results); - free(cwdbuf); - cwdbuf = NULL; + DirectorySearch(cwdbuf.data(), ext, &results); + cwdbuf.clear(); } } // Search local data dir - if((str=getenv("XDG_DATA_HOME")) != NULL && str[0] != '\0') + if((str=getenv("XDG_DATA_HOME")) != nullptr && str[0] != '\0') { - alstr_copy_cstr(&path, str); - if(VECTOR_BACK(path) != '/') - alstr_append_char(&path, '/'); - alstr_append_cstr(&path, subdir); - DirectorySearch(alstr_get_cstr(path), ext, &results); + std::string path{str}; + if(path.back() != '/') + path += '/'; + path += subdir; + DirectorySearch(path.c_str(), ext, &results); } - else if((str=getenv("HOME")) != NULL && str[0] != '\0') + else if((str=getenv("HOME")) != nullptr && str[0] != '\0') { - alstr_copy_cstr(&path, str); - if(VECTOR_BACK(path) == '/') - { - VECTOR_POP_BACK(path); - *VECTOR_END(path) = 0; - } - alstr_append_cstr(&path, "/.local/share/"); - alstr_append_cstr(&path, subdir); - DirectorySearch(alstr_get_cstr(path), ext, &results); + std::string path{str}; + if(path.back() == '/') + path.pop_back(); + path += "/.local/share/"; + path += subdir; + DirectorySearch(path.c_str(), ext, &results); } // Search global data dirs - if((str=getenv("XDG_DATA_DIRS")) == NULL || str[0] == '\0') + if((str=getenv("XDG_DATA_DIRS")) == nullptr || str[0] == '\0') str = "/usr/local/share/:/usr/share/"; - next = str; - while((str=next) != NULL && str[0] != '\0') + const char *next{str}; + while((str=next) != nullptr && str[0] != '\0') { next = strchr(str, ':'); + + std::string path; if(!next) - alstr_copy_cstr(&path, str); + path = str; else + path.append(str, next++); + if(!path.empty()) { - alstr_copy_range(&path, str, next); - ++next; - } - if(!alstr_empty(path)) - { - if(VECTOR_BACK(path) != '/') - alstr_append_char(&path, '/'); - alstr_append_cstr(&path, subdir); + if(path.back() != '/') + path += '/'; + path += subdir; - DirectorySearch(alstr_get_cstr(path), ext, &results); + DirectorySearch(path.c_str(), ext, &results); } } - - alstr_reset(&path); } - ATOMIC_STORE_SEQ(&search_lock, 0u); - + search_lock.store(AL_FALSE); return results; } @@ -942,10 +851,10 @@ void alstr_append_range(al_string *str, const al_string_char_type *from, const a void alstr_copy_wcstr(al_string *str, const wchar_t *from) { int len; - if((len=WideCharToMultiByte(CP_UTF8, 0, from, -1, NULL, 0, NULL, NULL)) > 0) + if((len=WideCharToMultiByte(CP_UTF8, 0, from, -1, nullptr, 0, nullptr, nullptr)) > 0) { VECTOR_RESIZE(*str, len-1, len); - WideCharToMultiByte(CP_UTF8, 0, from, -1, &VECTOR_FRONT(*str), len, NULL, NULL); + WideCharToMultiByte(CP_UTF8, 0, from, -1, &VECTOR_FRONT(*str), len, nullptr, nullptr); VECTOR_ELEM(*str, len-1) = 0; } } @@ -953,11 +862,11 @@ void alstr_copy_wcstr(al_string *str, const wchar_t *from) void alstr_append_wcstr(al_string *str, const wchar_t *from) { int len; - if((len=WideCharToMultiByte(CP_UTF8, 0, from, -1, NULL, 0, NULL, NULL)) > 0) + if((len=WideCharToMultiByte(CP_UTF8, 0, from, -1, nullptr, 0, nullptr, nullptr)) > 0) { size_t base = alstr_length(*str); VECTOR_RESIZE(*str, base+len-1, base+len); - WideCharToMultiByte(CP_UTF8, 0, from, -1, &VECTOR_ELEM(*str, base), len, NULL, NULL); + WideCharToMultiByte(CP_UTF8, 0, from, -1, &VECTOR_ELEM(*str, base), len, nullptr, nullptr); VECTOR_ELEM(*str, base+len-1) = 0; } } @@ -965,10 +874,10 @@ void alstr_append_wcstr(al_string *str, const wchar_t *from) void alstr_copy_wrange(al_string *str, const wchar_t *from, const wchar_t *to) { int len; - if((len=WideCharToMultiByte(CP_UTF8, 0, from, (int)(to-from), NULL, 0, NULL, NULL)) > 0) + if((len=WideCharToMultiByte(CP_UTF8, 0, from, (int)(to-from), nullptr, 0, nullptr, nullptr)) > 0) { VECTOR_RESIZE(*str, len, len+1); - WideCharToMultiByte(CP_UTF8, 0, from, (int)(to-from), &VECTOR_FRONT(*str), len+1, NULL, NULL); + WideCharToMultiByte(CP_UTF8, 0, from, (int)(to-from), &VECTOR_FRONT(*str), len+1, nullptr, nullptr); VECTOR_ELEM(*str, len) = 0; } } @@ -976,11 +885,11 @@ void alstr_copy_wrange(al_string *str, const wchar_t *from, const wchar_t *to) void alstr_append_wrange(al_string *str, const wchar_t *from, const wchar_t *to) { int len; - if((len=WideCharToMultiByte(CP_UTF8, 0, from, (int)(to-from), NULL, 0, NULL, NULL)) > 0) + if((len=WideCharToMultiByte(CP_UTF8, 0, from, (int)(to-from), nullptr, 0, nullptr, nullptr)) > 0) { size_t base = alstr_length(*str); VECTOR_RESIZE(*str, base+len, base+len+1); - WideCharToMultiByte(CP_UTF8, 0, from, (int)(to-from), &VECTOR_ELEM(*str, base), len+1, NULL, NULL); + WideCharToMultiByte(CP_UTF8, 0, from, (int)(to-from), &VECTOR_ELEM(*str, base), len+1, nullptr, nullptr); VECTOR_ELEM(*str, base+len) = 0; } } diff --git a/Alc/hrtf.cpp b/Alc/hrtf.cpp index b2dc1ec8..9b57ede7 100644 --- a/Alc/hrtf.cpp +++ b/Alc/hrtf.cpp @@ -944,13 +944,13 @@ struct Hrtf *LoadHrtf02(std::istream &data, const char *filename) } -void AddFileEntry(vector_EnumeratedHrtf *list, const_al_string filename) +void AddFileEntry(vector_EnumeratedHrtf *list, const std::string &filename) { /* Check if this file has already been loaded globally. */ HrtfEntry *loaded_entry{LoadedHrtfs}; while(loaded_entry) { - if(alstr_cmp_cstr(filename, loaded_entry->filename) == 0) + if(filename == loaded_entry->filename) { const EnumeratedHrtf *iter; /* Check if this entry has already been added to the list. */ @@ -959,7 +959,7 @@ void AddFileEntry(vector_EnumeratedHrtf *list, const_al_string filename) #undef MATCH_ENTRY if(iter != VECTOR_END(*list)) { - TRACE("Skipping duplicate file entry %s\n", alstr_get_cstr(filename)); + TRACE("Skipping duplicate file entry %s\n", filename.c_str()); return; } @@ -970,34 +970,34 @@ void AddFileEntry(vector_EnumeratedHrtf *list, const_al_string filename) if(!loaded_entry) { - TRACE("Got new file \"%s\"\n", alstr_get_cstr(filename)); + TRACE("Got new file \"%s\"\n", filename.c_str()); loaded_entry = static_cast<HrtfEntry*>(al_calloc(DEF_ALIGN, - FAM_SIZE(struct HrtfEntry, filename, alstr_length(filename)+1) + FAM_SIZE(struct HrtfEntry, filename, filename.length()+1) )); loaded_entry->next = LoadedHrtfs; loaded_entry->handle = nullptr; - strcpy(loaded_entry->filename, alstr_get_cstr(filename)); + strcpy(loaded_entry->filename, filename.c_str()); LoadedHrtfs = loaded_entry; } /* TODO: Get a human-readable name from the HRTF data (possibly coming in a * format update). */ - const char *name{strrchr(alstr_get_cstr(filename), '/')}; - if(!name) name = strrchr(alstr_get_cstr(filename), '\\'); - if(!name) name = alstr_get_cstr(filename); - else ++name; + size_t namepos = filename.find_last_of('/')+1; + if(!namepos) namepos = filename.find_last_of('\\')+1; - const char *ext{strrchr(name, '.')}; + size_t extpos{filename.find_last_of('.')}; + if(extpos <= namepos) extpos = std::string::npos; EnumeratedHrtf entry = { AL_STRING_INIT_STATIC(), nullptr }; const EnumeratedHrtf *iter{}; int i{0}; do { - if(!ext) - alstr_copy_cstr(&entry.name, name); + if(extpos == std::string::npos) + alstr_copy_range(&entry.name, &*(filename.begin()+namepos), &*filename.end()); else - alstr_copy_range(&entry.name, name, ext); + alstr_copy_range(&entry.name, &*(filename.begin()+namepos), + &*(filename.begin()+extpos)); if(i != 0) { char str[64]; @@ -1019,12 +1019,12 @@ void AddFileEntry(vector_EnumeratedHrtf *list, const_al_string filename) /* Unfortunate that we have to duplicate AddFileEntry to take a memory buffer * for input instead of opening the given filename. */ -void AddBuiltInEntry(vector_EnumeratedHrtf *list, const_al_string filename, ALuint residx) +void AddBuiltInEntry(vector_EnumeratedHrtf *list, const std::string &filename, ALuint residx) { HrtfEntry *loaded_entry{LoadedHrtfs}; while(loaded_entry) { - if(alstr_cmp_cstr(filename, loaded_entry->filename) == 0) + if(filename == loaded_entry->filename) { const EnumeratedHrtf *iter{}; #define MATCH_ENTRY(i) (loaded_entry == (i)->hrtf) @@ -1032,7 +1032,7 @@ void AddBuiltInEntry(vector_EnumeratedHrtf *list, const_al_string filename, ALui #undef MATCH_ENTRY if(iter != VECTOR_END(*list)) { - TRACE("Skipping duplicate file entry %s\n", alstr_get_cstr(filename)); + TRACE("Skipping duplicate file entry %s\n", filename.c_str()); return; } @@ -1043,9 +1043,9 @@ void AddBuiltInEntry(vector_EnumeratedHrtf *list, const_al_string filename, ALui if(!loaded_entry) { - size_t namelen = alstr_length(filename)+32; + size_t namelen = filename.length()+32; - TRACE("Got new file \"%s\"\n", alstr_get_cstr(filename)); + TRACE("Got new file \"%s\"\n", filename.c_str()); loaded_entry = static_cast<HrtfEntry*>(al_calloc(DEF_ALIGN, FAM_SIZE(struct HrtfEntry, filename, namelen) @@ -1053,27 +1053,18 @@ void AddBuiltInEntry(vector_EnumeratedHrtf *list, const_al_string filename, ALui loaded_entry->next = LoadedHrtfs; loaded_entry->handle = nullptr; snprintf(loaded_entry->filename, namelen, "!%u_%s", - residx, alstr_get_cstr(filename)); + residx, filename.c_str()); LoadedHrtfs = loaded_entry; } /* TODO: Get a human-readable name from the HRTF data (possibly coming in a * format update). */ - const char *name{strrchr(alstr_get_cstr(filename), '/')}; - if(!name) name = strrchr(alstr_get_cstr(filename), '\\'); - if(!name) name = alstr_get_cstr(filename); - else ++name; - - const char *ext{strrchr(name, '.')}; EnumeratedHrtf entry{AL_STRING_INIT_STATIC(), nullptr}; const EnumeratedHrtf *iter{}; int i{0}; do { - if(!ext) - alstr_copy_cstr(&entry.name, name); - else - alstr_copy_range(&entry.name, name, ext); + alstr_copy_range(&entry.name, &*filename.cbegin(), &*filename.cend()); if(i != 0) { char str[64]; @@ -1123,11 +1114,11 @@ ResData GetResource(int name) vector_EnumeratedHrtf EnumerateHrtf(const_al_string devname) { vector_EnumeratedHrtf list{VECTOR_INIT_STATIC()}; + bool usedefaults{true}; const char *pathlist{""}; if(ConfigValueStr(alstr_get_cstr(devname), nullptr, "hrtf-paths", &pathlist)) { - al_string pname = AL_STRING_INIT_STATIC(); while(pathlist && *pathlist) { const char *next, *end; @@ -1150,46 +1141,29 @@ vector_EnumeratedHrtf EnumerateHrtf(const_al_string devname) --end; if(end != pathlist) { - alstr_copy_range(&pname, pathlist, end); - - vector_al_string flist{SearchDataFiles(".mhr", alstr_get_cstr(pname))}; - for(size_t i{0};i < VECTOR_SIZE(flist);i++) - AddFileEntry(&list, VECTOR_ELEM(flist, i)); - VECTOR_FOR_EACH(al_string, flist, alstr_reset); - VECTOR_DEINIT(flist); + const std::string pname{pathlist, end}; + for(const auto &fname : SearchDataFiles(".mhr", pname.c_str())) + AddFileEntry(&list, fname); } pathlist = next; } - - alstr_reset(&pname); } else if(ConfigValueExists(alstr_get_cstr(devname), nullptr, "hrtf_tables")) ERR("The hrtf_tables option is deprecated, please use hrtf-paths instead.\n"); if(usedefaults) { - vector_al_string flist{SearchDataFiles(".mhr", "openal/hrtf")}; - for(size_t i{0};i < VECTOR_SIZE(flist);i++) - AddFileEntry(&list, VECTOR_ELEM(flist, i)); - VECTOR_FOR_EACH(al_string, flist, alstr_reset); - VECTOR_DEINIT(flist); + for(const auto &fname : SearchDataFiles(".mhr", "openal/hrtf")) + AddFileEntry(&list, fname); - al_string ename = AL_STRING_INIT_STATIC(); ResData res{GetResource(IDR_DEFAULT_44100_MHR)}; if(res.data != nullptr && res.size > 0) - { - alstr_copy_cstr(&ename, "Built-In 44100hz"); - AddBuiltInEntry(&list, ename, IDR_DEFAULT_44100_MHR); - } + AddBuiltInEntry(&list, "Built-In 44100hz", IDR_DEFAULT_44100_MHR); res = GetResource(IDR_DEFAULT_48000_MHR); if(res.data != nullptr && res.size > 0) - { - alstr_copy_cstr(&ename, "Built-In 48000hz"); - AddBuiltInEntry(&list, ename, IDR_DEFAULT_48000_MHR); - } - alstr_reset(&ename); + AddBuiltInEntry(&list, "Built-In 48000hz", IDR_DEFAULT_48000_MHR); } const char *defaulthrtf{""}; diff --git a/OpenAL32/Include/alMain.h b/OpenAL32/Include/alMain.h index b3380ae2..9826c6b2 100644 --- a/OpenAL32/Include/alMain.h +++ b/OpenAL32/Include/alMain.h @@ -99,6 +99,9 @@ #ifdef __cplusplus +#include <vector> +#include <string> + extern "C" { #endif @@ -910,12 +913,11 @@ inline void UnlockEffectSlotList(ALCcontext *context) int EventThread(void *arg); - -vector_al_string SearchDataFiles(const char *match, const char *subdir); - #ifdef __cplusplus } // extern "C" +std::vector<std::string> SearchDataFiles(const char *match, const char *subdir); + /* Simple RAII context reference. Takes the reference of the provided * ALCcontext, and decrements it when leaving scope. Movable (transfer * reference) but not copyable (no new references). |