minor fixes from work; also add sys_WideFromArgv

This was SVN commit r8856.
This commit is contained in:
janwas 2011-01-04 19:53:38 +00:00
parent a0d2d89863
commit 002e3f5606
12 changed files with 84 additions and 29 deletions

View File

@ -35,7 +35,7 @@ struct DummyDeleter
};
template<class T>
shared_ptr<T> DummySharedPtr(T* ptr)
inline shared_ptr<T> DummySharedPtr(T* ptr)
{
return shared_ptr<T>(ptr, DummyDeleter());
}
@ -71,7 +71,7 @@ struct AlignedDeleter
};
template<class T>
shared_ptr<T> AllocateAligned(size_t size)
inline shared_ptr<T> AllocateAligned(size_t size)
{
return shared_ptr<T>((T*)rtl_AllocateAligned(size, x86_x64_L2CacheLineSize()), AlignedDeleter());
}

View File

@ -37,7 +37,7 @@
* - n must be in [0, CHAR_BIT*sizeof(T)), else the result is undefined!
**/
template<typename T>
T Bit(size_t n)
inline T Bit(size_t n)
{
const T one = T(1);
return (T)(one << n);
@ -51,7 +51,7 @@ T Bit(size_t n)
#define BIT(n) (1u << (n))
template<typename T>
bool IsBitSet(T value, size_t index)
inline bool IsBitSet(T value, size_t index)
{
const T bit = Bit<T>(index);
return (value & bit) != 0;
@ -69,7 +69,7 @@ bool IsBitSet(T value, size_t index)
* @param numBits Number of bits in mask.
**/
template<typename T>
T bit_mask(size_t numBits)
inline T bit_mask(size_t numBits)
{
const T bitsInT = sizeof(T)*CHAR_BIT;
const T allBits = (T)~T(0);
@ -125,7 +125,7 @@ inline T SetBitsTo(T num, size_t lo_idx, size_t hi_idx, size_t value)
* @return number of 1-bits in mask
**/
template<typename T>
size_t PopulationCount(T mask)
inline size_t PopulationCount(T mask)
{
// note: a more complex but probably faster method is given at
// http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
@ -173,7 +173,7 @@ inline T ClearLeastSignificantBit(T x)
* zero if the input is zero.
**/
template<typename T>
size_t ceil_log2(T x)
inline size_t ceil_log2(T x)
{
T bit = 1;
size_t log = 0;
@ -200,7 +200,7 @@ extern int floor_log2(const float x);
* round up to next larger power of two.
**/
template<typename T>
T round_up_to_pow2(T x)
inline T round_up_to_pow2(T x)
{
return T(1) << ceil_log2(x);
}
@ -212,7 +212,7 @@ T round_up_to_pow2(T x)
* @param multiple Must be a power of two.
**/
template<typename T>
T round_up(T n, T multiple)
inline T round_up(T n, T multiple)
{
debug_assert(is_pow2(multiple));
const T result = (n + multiple-1) & ~(multiple-1);
@ -221,7 +221,7 @@ T round_up(T n, T multiple)
}
template<typename T>
T round_down(T n, T multiple)
inline T round_down(T n, T multiple)
{
debug_assert(is_pow2(multiple));
const T result = n & ~(multiple-1);
@ -231,14 +231,14 @@ T round_down(T n, T multiple)
template<typename T>
bool IsAligned(T t, uintptr_t multiple)
inline bool IsAligned(T t, uintptr_t multiple)
{
return ((uintptr_t)t % multiple) == 0;
}
template<typename T>
T MaxPowerOfTwoDivisor(T value)
inline T MaxPowerOfTwoDivisor(T value)
{
debug_assert(value != T(0));
@ -252,5 +252,4 @@ T MaxPowerOfTwoDivisor(T value)
return 0;
}
#endif // #ifndef INCLUDED_BITS

View File

@ -71,4 +71,9 @@
# define CONFIG_DEHYDRA 0
#endif
// include Boost filesystem and shared_ptr in PCH?
#ifndef CONFIG_ENABLE_BOOST
# define CONFIG_ENABLE_BOOST 1
#endif
#endif // #ifndef INCLUDED_CONFIG

View File

@ -48,6 +48,9 @@
// disable some common and annoying warnings
// must come after compiler.h, but as soon as possible so that
// headers below are covered
#if MSC_VERSION
# pragma warning(push)
#endif
#include "lib/pch/pch_warnings.h"
#if ICC_VERSION
@ -72,7 +75,15 @@ long double __cdecl abs(long double x); // required for Eigen
#include "lib/lib.h"
#include "lib/secure_crt.h"
#include "lib/pch/pch_boost.h"
#if CONFIG_ENABLE_BOOST
# include "lib/pch/pch_boost.h"
#elif HAVE_CPP0X
#include <memory>
using std::shared_ptr;
#else
#include <memory>
using std::tr1::shared_ptr;
#endif
// (must come after boost and common lib headers, but before re-enabling
// warnings to avoid boost spew)
@ -99,9 +110,6 @@ long double __cdecl abs(long double x); // required for Eigen
#endif // #if CONFIG_ENABLE_PCH && HAVE_PCH
// restore temporarily-disabled warnings
#if ICC_VERSION
#if MSC_VERSION
# pragma warning(pop)
#endif
#if MSC_VERSION
# pragma warning(default:4702)
#endif

View File

@ -118,6 +118,16 @@
#endif
// (at least rudimentary) support for C++0x
#ifndef HAVE_CPP0X
# if defined(__GXX_EXPERIMENTAL_CPP0X__) || MSC_VERSION >= 1600 || ICC_VERSION >= 1200
# define HAVE_CPP0X 1
# else
# define HAVE_CPP0X 0
# endif
#endif
// C99-like restrict (non-standard in C++, but widely supported in various forms).
//
// May be used on pointers. May also be used on member functions to indicate

View File

@ -77,7 +77,7 @@ LIB_API bool cpu_CAS64(volatile i64* location, i64 expected, i64 newValue);
* casting in user code.
**/
template<typename T>
bool cpu_CAS(volatile T* location, T expected, T new_value)
inline bool cpu_CAS(volatile T* location, T expected, T new_value)
{
return cpu_CAS((volatile intptr_t*)location, (intptr_t)expected, (intptr_t)new_value);
}

View File

@ -45,6 +45,13 @@
#define URL_OPEN_COMMAND "xdg-open"
#endif
std::wstring sys_WideFromArgv(const char* argv_i)
{
// argv is usually UTF-8 on Linux, unsure about OS X..
return wstring_from_utf8(argv_i);
}
// these are basic POSIX-compatible backends for the sysdep.h functions.
// Win32 has better versions which override these.

View File

@ -111,7 +111,7 @@ extern int aio_suspend(const struct aiocb* const[], int, const struct timespec*)
extern int aio_write(struct aiocb*);
extern int lio_listio(int, struct aiocb* const[], int, struct sigevent*);
// for use by wposix_wchar's wopen/wclose:
// for use by wfilesystem's wopen/wclose:
// (re)open file in asynchronous mode and associate handle with fd.
// (this works because the files default to DENY_NONE sharing)

View File

@ -81,7 +81,7 @@
// defined by winsock2 and also Linux (with different values)
// (values derived from winsock2 WSA* constants minus WSABASEERR)
// update: disabled on newer Boost versions because filesystem drags in boost/cerrno.hpp
#if !defined(BOOST_VERSION) || BOOST_VERSION <= 103401
#if (!defined(BOOST_VERSION) || BOOST_VERSION <= 103401) && (!MSC_VERSION || MSC_VERSION < 1600)
#define EWOULDBLOCK 35
#define EINPROGRESS 36
#define EALREADY 37

View File

@ -361,18 +361,19 @@ EXTERN_C int main(int argc, char* argv[]);
// required because main's argv is in a non-UTF8 encoding
int wmain(int argc, wchar_t* argv[])
{
char** utf8_argv = new char*[argc];
if(argc == 0)
return EXIT_FAILURE; // ensure &utf8_argv[0] is safe
std::vector<char*> utf8_argv(argc);
for(int i = 0; i < argc; i++)
{
std::string utf8 = utf8_from_wstring(argv[i]);
utf8_argv[i] = strdup(utf8.c_str());
}
const int ret = main(argc, utf8_argv);
const int ret = main(argc, &utf8_argv[0]);
for(int i = 0; i < argc; i++)
free(utf8_argv[i]);
delete[] utf8_argv;
return ret;
}

View File

@ -44,6 +44,23 @@
#endif
std::wstring sys_WideFromArgv(const char* argv_i)
{
// NB: despite http://cbloomrants.blogspot.com/2008/06/06-14-08-1.html,
// WinXP x64 EN cmd.exe (chcp reports 437) encodes argv u-umlaut
// (entered manually or via auto-complete) via cp1252. the same applies
// to WinXP SP2 DE (where chcp reports 850).
const UINT cp = CP_ACP;
const DWORD flags = MB_PRECOMPOSED|MB_ERR_INVALID_CHARS;
const int inputSize = -1; // null-terminated
wchar_t buf[MAX_PATH] = {'\0'};
// NB: avoid mbstowcs because it may specify another locale
const int ret = MultiByteToWideChar(cp, flags, argv_i, inputSize, buf, ARRAY_SIZE(buf));
debug_assert(ret != 0);
return std::wstring(buf);
}
void sys_display_msg(const wchar_t* caption, const wchar_t* msg)
{
MessageBoxW(0, msg, caption, MB_ICONEXCLAMATION|MB_TASKMODAL|MB_SETFOREGROUND);
@ -61,8 +78,8 @@ static POINTS dlg_prevClientSize;
static void dlg_OnMove(HWND UNUSED(hDlg), int x, int y)
{
dlg_clientOrigin.x = x;
dlg_clientOrigin.y = y;
dlg_clientOrigin.x = (short)x;
dlg_clientOrigin.y = (short)y;
}
@ -120,8 +137,8 @@ static void dlg_OnSize(HWND hDlg, UINT state, int clientSizeX, int clientSizeY)
const int dx = clientSizeX - dlg_prevClientSize.x;
const int dy = clientSizeY - dlg_prevClientSize.y;
dlg_prevClientSize.x = clientSizeX;
dlg_prevClientSize.y = clientSizeY;
dlg_prevClientSize.x = (short)clientSizeX;
dlg_prevClientSize.y = (short)clientSizeY;
if(!isOriginValid) // must not call dlg_ResizeControl
return;

View File

@ -64,6 +64,14 @@ extern ErrorReactionInternal sys_display_error(const wchar_t* text, size_t flags
// misc
//
/**
* @return a wide string conversion of the platform's encoding of main's argv.
*
* (NB: wseh.cpp defines a wmain that converts argv to UTF-8 and calls main(),
* but only if LIB_STATIC_LINK)
**/
LIB_API std::wstring sys_WideFromArgv(const char* argv_i);
/**
* sys_vswprintf: doesn't quite follow the standard for vswprintf, but works
* better across compilers:
@ -158,7 +166,7 @@ extern size_t sys_max_sector_size();
* this should only be used with small numbers of bytes, to avoid
* hogging the system's entropy.
**/
extern LibError sys_generate_random_bytes(u8* buf, size_t count);
LIB_API LibError sys_generate_random_bytes(u8* buf, size_t count);
/**
* directory separation character