2013-10-28 11:26:26 -07:00
|
|
|
#ifndef AL_COMPAT_H
|
|
|
|
#define AL_COMPAT_H
|
|
|
|
|
2018-03-09 17:24:03 -08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
2013-10-28 11:26:26 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <windows.h>
|
|
|
|
|
2018-11-04 15:24:24 -08:00
|
|
|
#include <array>
|
2018-11-03 12:55:02 -07:00
|
|
|
#include <string>
|
2018-11-04 15:24:24 -08:00
|
|
|
#include <fstream>
|
2018-11-03 12:55:02 -07:00
|
|
|
|
|
|
|
inline std::string wstr_to_utf8(const WCHAR *wstr)
|
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
|
|
|
|
int len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, nullptr, 0, nullptr, nullptr);
|
|
|
|
if(len > 0)
|
|
|
|
{
|
|
|
|
ret.resize(len);
|
|
|
|
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, &ret[0], len, nullptr, nullptr);
|
|
|
|
ret.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-11-04 15:24:24 -08:00
|
|
|
inline std::wstring utf8_to_wstr(const char *str)
|
|
|
|
{
|
|
|
|
std::wstring ret;
|
|
|
|
|
|
|
|
int len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
|
|
|
|
if(len > 0)
|
|
|
|
{
|
|
|
|
ret.resize(len);
|
|
|
|
MultiByteToWideChar(CP_UTF8, 0, str, -1, &ret[0], len);
|
|
|
|
ret.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace al {
|
|
|
|
|
|
|
|
// Windows' std::ifstream fails with non-ANSI paths since the standard only
|
|
|
|
// specifies names using const char* (or std::string). MSVC has a non-standard
|
|
|
|
// extension using const wchar_t* (or std::wstring?) to handle Unicode paths,
|
|
|
|
// but not all Windows compilers support it. So we have to make our own istream
|
|
|
|
// that accepts UTF-8 paths and forwards to Unicode-aware I/O functions.
|
|
|
|
class filebuf final : public std::streambuf {
|
|
|
|
std::array<char_type,4096> mBuffer;
|
|
|
|
HANDLE mFile{INVALID_HANDLE_VALUE};
|
|
|
|
|
2019-06-10 01:51:14 -07:00
|
|
|
int_type underflow() override;
|
|
|
|
pos_type seekoff(off_type offset, std::ios_base::seekdir whence, std::ios_base::openmode mode) override;
|
|
|
|
pos_type seekpos(pos_type pos, std::ios_base::openmode mode) override;
|
2018-11-04 15:24:24 -08:00
|
|
|
|
|
|
|
public:
|
2019-06-10 01:51:14 -07:00
|
|
|
filebuf() = default;
|
|
|
|
~filebuf() override;
|
2018-11-04 15:24:24 -08:00
|
|
|
|
2019-06-10 01:51:14 -07:00
|
|
|
bool open(const wchar_t *filename, std::ios_base::openmode mode);
|
|
|
|
bool open(const char *filename, std::ios_base::openmode mode);
|
2018-11-04 15:24:24 -08:00
|
|
|
|
2019-06-10 01:51:14 -07:00
|
|
|
bool is_open() const noexcept { return mFile != INVALID_HANDLE_VALUE; }
|
2018-11-04 15:24:24 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Inherit from std::istream to use our custom streambuf
|
|
|
|
class ifstream final : public std::istream {
|
|
|
|
filebuf mStreamBuf;
|
|
|
|
|
|
|
|
public:
|
2019-06-10 01:51:14 -07:00
|
|
|
ifstream(const wchar_t *filename, std::ios_base::openmode mode = std::ios_base::in);
|
2018-11-09 21:56:05 -08:00
|
|
|
ifstream(const std::wstring &filename, std::ios_base::openmode mode = std::ios_base::in)
|
|
|
|
: ifstream(filename.c_str(), mode) { }
|
2019-06-10 01:51:14 -07:00
|
|
|
ifstream(const char *filename, std::ios_base::openmode mode = std::ios_base::in);
|
2018-11-09 21:56:05 -08:00
|
|
|
ifstream(const std::string &filename, std::ios_base::openmode mode = std::ios_base::in)
|
|
|
|
: ifstream(filename.c_str(), mode) { }
|
2019-06-18 23:04:24 -07:00
|
|
|
~ifstream() override;
|
2018-11-04 15:24:24 -08:00
|
|
|
|
|
|
|
bool is_open() const noexcept { return mStreamBuf.is_open(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace al
|
|
|
|
|
2013-10-28 11:26:26 -07:00
|
|
|
#define HAVE_DYNLOAD 1
|
2018-11-04 15:24:24 -08:00
|
|
|
|
2018-11-15 03:29:56 -08:00
|
|
|
#else /* _WIN32 */
|
2018-11-04 15:24:24 -08:00
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
namespace al {
|
|
|
|
|
|
|
|
using filebuf = std::filebuf;
|
|
|
|
using ifstream = std::ifstream;
|
|
|
|
|
|
|
|
} // namespace al
|
|
|
|
|
2018-11-15 03:29:56 -08:00
|
|
|
#if defined(HAVE_DLFCN_H)
|
|
|
|
#define HAVE_DYNLOAD 1
|
2013-10-28 11:26:26 -07:00
|
|
|
#endif
|
|
|
|
|
2018-11-15 03:29:56 -08:00
|
|
|
#endif /* _WIN32 */
|
2018-11-11 16:09:24 -08:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
struct PathNamePair { std::string path, fname; };
|
2018-12-25 11:27:22 -08:00
|
|
|
const PathNamePair &GetProcBinary(void);
|
2018-11-15 03:29:56 -08:00
|
|
|
|
|
|
|
#ifdef HAVE_DYNLOAD
|
|
|
|
void *LoadLib(const char *name);
|
|
|
|
void CloseLib(void *handle);
|
|
|
|
void *GetSymbol(void *handle, const char *name);
|
2018-03-09 17:24:03 -08:00
|
|
|
#endif
|
|
|
|
|
2018-11-15 03:29:56 -08:00
|
|
|
#endif /* __cplusplus */
|
|
|
|
|
2013-10-28 11:26:26 -07:00
|
|
|
#endif /* AL_COMPAT_H */
|