Pārlūkot izejas kodu

{src,tools}: flip preprocessor logic to always have a default

Daniele Bartolini 3 gadi atpakaļ
vecāks
revīzija
1840b1987f

+ 57 - 57
src/core/filesystem/filesystem_disk.cpp

@@ -11,35 +11,35 @@
 #include "core/os.h"
 #include "core/strings/dynamic_string.inl"
 
-#if CROWN_PLATFORM_POSIX
-	#include <stdio.h>
-	#include <errno.h>
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 	#include <tchar.h>
 	#ifndef WIN32_LEAN_AND_MEAN
 		#define WIN32_LEAN_AND_MEAN
 	#endif
 	#include <windows.h>
+#else
+	#include <stdio.h>
+	#include <errno.h>
 #endif
 
 namespace crown
 {
 struct FileDisk : public File
 {
-#if CROWN_PLATFORM_POSIX
-	FILE *_file;
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 	HANDLE _file;
 	bool _eof;
+#else
+	FILE *_file;
 #endif
 
 	/// Opens the file located at @a path with the given @a mode.
 	FileDisk()
-#if CROWN_PLATFORM_POSIX
-		: _file(NULL)
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 		: _file(INVALID_HANDLE_VALUE)
 		, _eof(false)
+#else
+		: _file(NULL)
 #endif
 	{
 	}
@@ -51,9 +51,7 @@ struct FileDisk : public File
 
 	void open(const char *path, FileOpenMode::Enum mode)
 	{
-#if CROWN_PLATFORM_POSIX
-		_file = fopen(path, (mode == FileOpenMode::READ) ? "rb" : "wb");
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 		_file = CreateFile(path
 			, (mode == FileOpenMode::READ) ? GENERIC_READ : GENERIC_WRITE
 			, 0
@@ -62,82 +60,84 @@ struct FileDisk : public File
 			, FILE_ATTRIBUTE_NORMAL
 			, NULL
 			);
+#else
+		_file = fopen(path, (mode == FileOpenMode::READ) ? "rb" : "wb");
 #endif
 	}
 
 	void close()
 	{
 		if (is_open()) {
-#if CROWN_PLATFORM_POSIX
-			fclose(_file);
-			_file = NULL;
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 			CloseHandle(_file);
 			_file = INVALID_HANDLE_VALUE;
+#else
+			fclose(_file);
+			_file = NULL;
 #endif
 		}
 	}
 
 	bool is_open()
 	{
-#if CROWN_PLATFORM_POSIX
-		return _file != NULL;
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 		return _file != INVALID_HANDLE_VALUE;
+#else
+		return _file != NULL;
 #endif
 	}
 
 	u32 size()
 	{
 		CE_ASSERT(is_open(), "File is not open");
-#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_WINDOWS
+		return GetFileSize(_file, NULL);
+#else
 		Stat stat;
 		os::stat(stat, fileno(_file));
 		return (u32)stat.size;
-#elif CROWN_PLATFORM_WINDOWS
-		return GetFileSize(_file, NULL);
 #endif
 	}
 
 	u32 position()
 	{
 		CE_ASSERT(is_open(), "File is not open");
-#if CROWN_PLATFORM_POSIX
-		long pos = ftell(_file);
-		CE_ASSERT(pos != -1, "ftell: errno = %d", errno);
-		return (u32)pos;
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 		DWORD pos = SetFilePointer(_file, 0, NULL, FILE_CURRENT);
 		CE_ASSERT(pos != INVALID_SET_FILE_POINTER
 			, "SetFilePointer: GetLastError = %d"
 			, GetLastError()
 			);
 		return (u32)pos;
+#else
+		long pos = ftell(_file);
+		CE_ASSERT(pos != -1, "ftell: errno = %d", errno);
+		return (u32)pos;
 #endif
 	}
 
 	bool end_of_file()
 	{
 		CE_ASSERT(is_open(), "File is not open");
-#if CROWN_PLATFORM_POSIX
-		return feof(_file) != 0;
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 		return _eof;
+#else
+		return feof(_file) != 0;
 #endif
 	}
 
 	void seek(u32 position)
 	{
 		CE_ASSERT(is_open(), "File is not open");
-#if CROWN_PLATFORM_POSIX
-		int err = fseek(_file, (long)position, SEEK_SET);
-		CE_ASSERT(err == 0, "fseek: errno = %d", errno);
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 		DWORD err = SetFilePointer(_file, position, NULL, FILE_BEGIN);
 		CE_ASSERT(err != INVALID_SET_FILE_POINTER
 			, "SetFilePointer: GetLastError = %d"
 			, GetLastError()
 			);
+#else
+		int err = fseek(_file, (long)position, SEEK_SET);
+		CE_ASSERT(err == 0, "fseek: errno = %d", errno);
 #endif
 		CE_UNUSED(err);
 	}
@@ -145,15 +145,15 @@ struct FileDisk : public File
 	void seek_to_end()
 	{
 		CE_ASSERT(is_open(), "File is not open");
-#if CROWN_PLATFORM_POSIX
-		int err = fseek(_file, 0, SEEK_END);
-		CE_ASSERT(err == 0, "fseek: errno = %d", errno);
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 		DWORD err = SetFilePointer(_file, 0, NULL, FILE_END);
 		CE_ASSERT(err != INVALID_SET_FILE_POINTER
 			, "SetFilePointer: GetLastError = %d"
 			, GetLastError()
 			);
+#else
+		int err = fseek(_file, 0, SEEK_END);
+		CE_ASSERT(err == 0, "fseek: errno = %d", errno);
 #endif
 		CE_UNUSED(err);
 	}
@@ -161,15 +161,15 @@ struct FileDisk : public File
 	void skip(u32 bytes)
 	{
 		CE_ASSERT(is_open(), "File is not open");
-#if CROWN_PLATFORM_POSIX
-		int err = fseek(_file, bytes, SEEK_CUR);
-		CE_ASSERT(err == 0, "fseek: errno = %d", errno);
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 		DWORD err = SetFilePointer(_file, bytes, NULL, FILE_CURRENT);
 		CE_ASSERT(err != INVALID_SET_FILE_POINTER
 			, "SetFilePointer: GetLastError = %d"
 			, GetLastError()
 			);
+#else
+		int err = fseek(_file, bytes, SEEK_CUR);
+		CE_ASSERT(err == 0, "fseek: errno = %d", errno);
 #endif
 		CE_UNUSED(err);
 	}
@@ -178,16 +178,16 @@ struct FileDisk : public File
 	{
 		CE_ASSERT(is_open(), "File is not open");
 		CE_ASSERT(data != NULL, "Data must be != NULL");
-#if CROWN_PLATFORM_POSIX
-		size_t bytes_read = fread(data, 1, size, _file);
-		CE_ASSERT(ferror(_file) == 0, "fread error");
-		return (u32)bytes_read;
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 		DWORD bytes_read;
 		BOOL err = ReadFile(_file, data, size, &bytes_read, NULL);
 		CE_ASSERT(err == TRUE, "ReadFile: GetLastError = %d", GetLastError());
 		_eof = err && bytes_read == 0;
 		return bytes_read;
+#else
+		size_t bytes_read = fread(data, 1, size, _file);
+		CE_ASSERT(ferror(_file) == 0, "fread error");
+		return (u32)bytes_read;
 #endif
 	}
 
@@ -195,11 +195,7 @@ struct FileDisk : public File
 	{
 		CE_ASSERT(is_open(), "File is not open");
 		CE_ASSERT(data != NULL, "Data must be != NULL");
-#if CROWN_PLATFORM_POSIX
-		size_t bytes_written = fwrite(data, 1, size, _file);
-		CE_ASSERT(ferror(_file) == 0, "fwrite error");
-		return (u32)bytes_written;
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 		DWORD bytes_written;
 		WriteFile(_file, data, size, &bytes_written, NULL);
 		CE_ASSERT(size == bytes_written
@@ -207,21 +203,25 @@ struct FileDisk : public File
 			, GetLastError()
 			);
 		return bytes_written;
+#else
+		size_t bytes_written = fwrite(data, 1, size, _file);
+		CE_ASSERT(ferror(_file) == 0, "fwrite error");
+		return (u32)bytes_written;
 #endif
 	}
 
 	void flush()
 	{
 		CE_ASSERT(is_open(), "File is not open");
-#if CROWN_PLATFORM_POSIX
-		int err = fflush(_file);
-		CE_ASSERT(err == 0, "fflush: errno = %d", errno);
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 		BOOL err = FlushFileBuffers(_file);
 		CE_ASSERT(err != 0
 			, "FlushFileBuffers: GetLastError = %d"
 			, GetLastError()
 			);
+#else
+		int err = fflush(_file);
+		CE_ASSERT(err == 0, "fflush: errno = %d", errno);
 #endif
 		CE_UNUSED(err);
 	}

+ 11 - 11
src/core/filesystem/path.cpp

@@ -12,10 +12,10 @@
 
 namespace crown
 {
-#if CROWN_PLATFORM_POSIX
-const char PATH_SEPARATOR = '/';
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 const char PATH_SEPARATOR = '\\';
+#else
+const char PATH_SEPARATOR = '/';
 #endif // CROWN_PLATFORM_POSIX
 
 namespace path
@@ -23,16 +23,16 @@ namespace path
 	bool is_absolute(const char *path)
 	{
 		CE_ENSURE(NULL != path);
-#if CROWN_PLATFORM_POSIX
-		return strlen32(path) > 0
-			&& path[0] == PATH_SEPARATOR
-			;
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 		return strlen32(path) > 2
 			&& isalpha(path[0])
 			&& path[1] == ':'
 			&& path[2] == PATH_SEPARATOR
 			;
+#else
+		return strlen32(path) > 0
+			&& path[0] == PATH_SEPARATOR
+			;
 #endif
 	}
 
@@ -45,10 +45,10 @@ namespace path
 	bool is_root(const char *path)
 	{
 		CE_ENSURE(NULL != path);
-#if CROWN_PLATFORM_POSIX
-		return is_absolute(path) && strlen32(path) == 1;
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 		return is_absolute(path) && strlen32(path) == 3;
+#else
+		return is_absolute(path) && strlen32(path) == 1;
 #endif
 	}
 

+ 8 - 8
src/core/guid.cpp

@@ -9,12 +9,12 @@
 #include <stdio.h> // sscanf
 #include <stb_sprintf.h>
 
-#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_WINDOWS
+	#include <objbase.h>
+#else
 	#include <fcntl.h>
 	#include <unistd.h>
 	#include <errno.h>
-#elif CROWN_PLATFORM_WINDOWS
-	#include <objbase.h>
 #endif // CROWN_PLATFORM_POSIX
 
 namespace crown
@@ -48,17 +48,17 @@ namespace guid
 	Guid new_guid()
 	{
 		Guid guid;
-#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_WINDOWS
+		HRESULT hr = CoCreateGuid((GUID *)&guid);
+		CE_ASSERT(hr == S_OK, "CoCreateGuid: error");
+		CE_UNUSED(hr);
+#else
 		CE_ASSERT(guid_globals::_fd != -1, "new_guid: library uninitialized");
 		ssize_t rb = read(guid_globals::_fd, &guid, sizeof(guid));
 		CE_ENSURE(rb == sizeof(guid));
 		CE_UNUSED(rb);
 		guid.data1 = (guid.data1 & 0xffffffffffff4fffu) | 0x4000u;
 		guid.data2 = (guid.data2 & 0x3fffffffffffffffu) | 0x8000000000000000u;
-#elif CROWN_PLATFORM_WINDOWS
-		HRESULT hr = CoCreateGuid((GUID *)&guid);
-		CE_ASSERT(hr == S_OK, "CoCreateGuid: error");
-		CE_UNUSED(hr);
 #endif // CROWN_PLATFORM_POSIX
 		return guid;
 	}

+ 21 - 21
src/core/network/socket.cpp

@@ -10,7 +10,10 @@
 #include <new>
 #include <string.h> // memcpy
 
-#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_WINDOWS
+	#include <winsock2.h>
+	#define MSG_NOSIGNAL 0
+#else
 	#include <errno.h>
 	#include <fcntl.h>      // fcntl
 	#include <netinet/in.h> // htons, htonl, ...
@@ -24,9 +27,6 @@
 	#define WSAECONNREFUSED ECONNREFUSED
 	#define WSAETIMEDOUT ETIMEDOUT
 	#define WSAEWOULDBLOCK EWOULDBLOCK
-#elif CROWN_PLATFORM_WINDOWS
-	#include <winsock2.h>
-	#define MSG_NOSIGNAL 0
 #endif // CROWN_PLATFORM_POSIX
 
 namespace crown
@@ -35,10 +35,10 @@ namespace
 {
 	inline int last_error()
 	{
-#if CROWN_PLATFORM_POSIX
-		return errno;
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 		return WSAGetLastError();
+#else
+		return errno;
 #endif
 	}
 
@@ -150,14 +150,14 @@ namespace socket_internal
 
 	void set_blocking(SOCKET socket, bool blocking)
 	{
-#if CROWN_PLATFORM_POSIX
-		int flags = fcntl(socket, F_GETFL, 0);
-		fcntl(socket, F_SETFL, blocking ? (flags & ~O_NONBLOCK) : O_NONBLOCK);
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 		u_long non_blocking = blocking ? 0 : 1;
 		int err = ioctlsocket(socket, FIONBIO, &non_blocking);
 		CE_ASSERT(err == 0, "ioctlsocket: last_error() = %d", last_error());
 		CE_UNUSED(err);
+#else
+		int flags = fcntl(socket, F_GETFL, 0);
+		fcntl(socket, F_SETFL, blocking ? (flags & ~O_NONBLOCK) : O_NONBLOCK);
 #endif
 	}
 
@@ -367,22 +367,22 @@ bool SocketSet::isset(TCPSocket *socket)
 
 u32 SocketSet::num()
 {
-#if CROWN_PLATFORM_POSIX
-	return _priv->maxfd + 1;
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 	return _priv->fdset.fd_count;
+#else
+	return _priv->maxfd + 1;
 #endif
 }
 
 TCPSocket SocketSet::get(u32 index)
 {
 	TCPSocket socket;
-#if CROWN_PLATFORM_POSIX
-	CE_ENSURE((int)index < FD_SETSIZE);
-	socket._priv->socket = (int)index;
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 	CE_ENSURE(index < _priv->fdset.fd_count);
 	socket._priv->socket = _priv->fdset.fd_array[index];
+#else
+	CE_ENSURE((int)index < FD_SETSIZE);
+	socket._priv->socket = (int)index;
 #endif
 	return socket;
 }
@@ -396,10 +396,10 @@ SelectResult SocketSet::select(u32 timeout_ms)
 	SelectResult sr;
 	sr.num_ready = 0;
 	int ret = ::select(
-#if CROWN_PLATFORM_POSIX
-		_priv->maxfd + 1
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 		0 // Ignored: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-select
+#else
+		_priv->maxfd + 1
 #endif
 		, &_priv->fdset
 		, NULL

+ 86 - 86
src/core/os.cpp

@@ -13,7 +13,14 @@
 #include <string.h>   // strcmp
 #include <sys/stat.h> // stat, mkdir
 
-#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_WINDOWS
+	#include <io.h>       // _access
+	#include <stdio.h>
+	#ifndef WIN32_LEAN_AND_MEAN
+		#define WIN32_LEAN_AND_MEAN
+	#endif
+	#include <windows.h>
+#else
 	#include <dirent.h>   // opendir, readdir
 	#include <dlfcn.h>    // dlopen, dlclose, dlsym
 	#include <errno.h>
@@ -23,13 +30,6 @@
 	#include <sys/wait.h> // wait
 	#include <time.h>     // clock_gettime
 	#include <unistd.h>   // unlink, rmdir, getcwd, access
-#elif CROWN_PLATFORM_WINDOWS
-	#include <io.h>       // _access
-	#include <stdio.h>
-	#ifndef WIN32_LEAN_AND_MEAN
-		#define WIN32_LEAN_AND_MEAN
-	#endif
-	#include <windows.h>
 #endif
 #if CROWN_PLATFORM_ANDROID
 	#include <android/log.h>
@@ -41,37 +41,37 @@ namespace os
 {
 	void sleep(u32 ms)
 	{
-#if CROWN_PLATFORM_POSIX
-		usleep(ms * 1000);
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 		Sleep(ms);
+#else
+		usleep(ms * 1000);
 #endif
 	}
 
 	void *library_open(const char *path)
 	{
-#if CROWN_PLATFORM_POSIX
-		return ::dlopen(path, RTLD_LAZY);
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 		return (void *)LoadLibraryA(path);
+#else
+		return ::dlopen(path, RTLD_LAZY);
 #endif
 	}
 
 	void library_close(void *library)
 	{
-#if CROWN_PLATFORM_POSIX
-		dlclose(library);
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 		FreeLibrary((HMODULE)library);
+#else
+		dlclose(library);
 #endif
 	}
 
 	void *library_symbol(void *library, const char *name)
 	{
-#if CROWN_PLATFORM_POSIX
-		return ::dlsym(library, name);
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 		return (void *)GetProcAddress((HMODULE)library, name);
+#else
+		return ::dlsym(library, name);
 #endif
 	}
 
@@ -117,21 +117,7 @@ namespace os
 		info.size  = 0;
 		info.mtime = 0;
 
-#if CROWN_PLATFORM_POSIX
-		struct stat buf;
-		memset(&buf, 0, sizeof(buf));
-		int err = ::stat(path, &buf);
-		if (err != 0)
-			return;
-
-		if (S_ISREG(buf.st_mode) == 1)
-			info.file_type = Stat::REGULAR;
-		else if (S_ISDIR(buf.st_mode) == 1)
-			info.file_type = Stat::DIRECTORY;
-
-		info.size  = buf.st_size;
-		info.mtime = buf.st_mtim.tv_sec * s64(1000000000) + buf.st_mtim.tv_nsec;
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 		WIN32_FIND_DATAA wfd;
 		HANDLE fh = FindFirstFileA(path, &wfd);
 		if (fh == INVALID_HANDLE_VALUE)
@@ -152,20 +138,27 @@ namespace os
 		large_int.LowPart  = wfd.ftLastWriteTime.dwLowDateTime;
 		large_int.HighPart = wfd.ftLastWriteTime.dwHighDateTime;
 		info.mtime = large_int.QuadPart * u64(100); // See https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime
+#else
+		struct stat buf;
+		memset(&buf, 0, sizeof(buf));
+		int err = ::stat(path, &buf);
+		if (err != 0)
+			return;
+
+		if (S_ISREG(buf.st_mode) == 1)
+			info.file_type = Stat::REGULAR;
+		else if (S_ISDIR(buf.st_mode) == 1)
+			info.file_type = Stat::DIRECTORY;
+
+		info.size  = buf.st_size;
+		info.mtime = buf.st_mtim.tv_sec * s64(1000000000) + buf.st_mtim.tv_nsec;
 #endif
 	}
 
 	DeleteResult delete_file(const char *path)
 	{
 		DeleteResult dr;
-#if CROWN_PLATFORM_POSIX
-		if (::unlink(path) == 0)
-			dr.error = DeleteResult::SUCCESS;
-		else if (errno == ENOENT)
-			dr.error = DeleteResult::NO_ENTRY;
-		else
-			dr.error = DeleteResult::UNKNOWN;
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 		if (DeleteFile(path) != 0)
 			dr.error = DeleteResult::SUCCESS;
 		else if (GetLastError() == ERROR_FILE_NOT_FOUND)
@@ -174,6 +167,13 @@ namespace os
 		// 	dr.error = DeleteResult::NOT_FILE;
 		else
 			dr.error = DeleteResult::UNKNOWN;
+#else
+		if (::unlink(path) == 0)
+			dr.error = DeleteResult::SUCCESS;
+		else if (errno == ENOENT)
+			dr.error = DeleteResult::NO_ENTRY;
+		else
+			dr.error = DeleteResult::UNKNOWN;
 #endif
 		return dr;
 	}
@@ -181,17 +181,17 @@ namespace os
 	CreateResult create_directory(const char *path)
 	{
 		CreateResult cr;
-#if CROWN_PLATFORM_POSIX
-		if (::mkdir(path, 0755) == 0)
+#if CROWN_PLATFORM_WINDOWS
+		if (CreateDirectory(path, NULL) != 0)
 			cr.error = CreateResult::SUCCESS;
-		else if (errno == EEXIST)
+		else if (GetLastError() == ERROR_ALREADY_EXISTS)
 			cr.error = CreateResult::ALREADY_EXISTS;
 		else
 			cr.error = CreateResult::UNKNOWN;
-#elif CROWN_PLATFORM_WINDOWS
-		if (CreateDirectory(path, NULL) != 0)
+#else
+		if (::mkdir(path, 0755) == 0)
 			cr.error = CreateResult::SUCCESS;
-		else if (GetLastError() == ERROR_ALREADY_EXISTS)
+		else if (errno == EEXIST)
 			cr.error = CreateResult::ALREADY_EXISTS;
 		else
 			cr.error = CreateResult::UNKNOWN;
@@ -202,14 +202,7 @@ namespace os
 	DeleteResult delete_directory(const char *path)
 	{
 		DeleteResult dr;
-#if CROWN_PLATFORM_POSIX
-		if (::rmdir(path) == 0)
-			dr.error = DeleteResult::SUCCESS;
-		else if (errno == ENOENT)
-			dr.error = DeleteResult::NO_ENTRY;
-		else
-			dr.error = DeleteResult::UNKNOWN;
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 		if (RemoveDirectory(path) != 0)
 			dr.error = DeleteResult::SUCCESS;
 		else if (GetLastError() == ERROR_FILE_NOT_FOUND)
@@ -218,52 +211,40 @@ namespace os
 		// 	dr.error = DeleteResult::NOT_DIRECTORY;
 		else
 			dr.error = DeleteResult::UNKNOWN;
+#else
+		if (::rmdir(path) == 0)
+			dr.error = DeleteResult::SUCCESS;
+		else if (errno == ENOENT)
+			dr.error = DeleteResult::NO_ENTRY;
+		else
+			dr.error = DeleteResult::UNKNOWN;
 #endif
 		return dr;
 	}
 
 	const char *getcwd(char *buf, u32 size)
 	{
-#if CROWN_PLATFORM_POSIX
-		return ::getcwd(buf, size);
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 		GetCurrentDirectory(size, buf);
 		return buf;
+#else
+		return ::getcwd(buf, size);
 #endif
 	}
 
 	const char *getenv(const char *name)
 	{
-#if CROWN_PLATFORM_POSIX
-		return ::getenv(name);
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 		// GetEnvironmentVariable(name, buf, size);
 		return NULL;
+#else
+		return ::getenv(name);
 #endif
 	}
 
 	void list_files(const char *path, Vector<DynamicString> &files)
 	{
-#if CROWN_PLATFORM_POSIX
-		struct dirent *entry;
-
-		DIR *dir = opendir(path);
-		if (dir != NULL) {
-			while ((entry = readdir(dir))) {
-				const char *dname = entry->d_name;
-
-				if (!strcmp(dname, ".") || !strcmp(dname, ".."))
-					continue;
-
-				TempAllocator256 ta;
-				DynamicString fname(ta);
-				fname.set(dname, strlen32(dname));
-				vector::push_back(files, fname);
-			}
-
-			closedir(dir);
-		}
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 		TempAllocator256 ta_path;
 		DynamicString cur_path(ta_path);
 		cur_path += path;
@@ -286,16 +267,35 @@ namespace os
 
 			FindClose(file);
 		}
+#else
+		struct dirent *entry;
+
+		DIR *dir = opendir(path);
+		if (dir != NULL) {
+			while ((entry = readdir(dir))) {
+				const char *dname = entry->d_name;
+
+				if (!strcmp(dname, ".") || !strcmp(dname, ".."))
+					continue;
+
+				TempAllocator256 ta;
+				DynamicString fname(ta);
+				fname.set(dname, strlen32(dname));
+				vector::push_back(files, fname);
+			}
+
+			closedir(dir);
+		}
 #endif
 	}
 
 	///
 	s32 access(const char *path, u32 flags)
 	{
-#if CROWN_PLATFORM_POSIX
-		return ::access(path, flags);
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 		return ::_access(path, flags == AccessFlags::EXECUTE ? AccessFlags::EXISTS : flags);
+#else
+		return ::access(path, flags);
 #endif
 	}
 

+ 102 - 102
src/core/process.cpp

@@ -8,28 +8,28 @@
 #include "core/strings/string_stream.inl"
 #include <stdio.h> // fdopen etc.
 
-#if CROWN_PLATFORM_POSIX
-	#include <unistd.h>   // fork, execvp
-	#include <sys/wait.h> // waitpid
-	#include <errno.h>
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 	#ifndef WIN32_LEAN_AND_MEAN
 		#define WIN32_LEAN_AND_MEAN
 	#endif
 	#include <windows.h>
+#else
+	#include <unistd.h>   // fork, execvp
+	#include <sys/wait.h> // waitpid
+	#include <errno.h>
 #endif
 
 namespace crown
 {
 struct Private
 {
-#if CROWN_PLATFORM_POSIX
-	FILE *file;
-	pid_t pid;
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 	PROCESS_INFORMATION process;
 	HANDLE stdout_rd;
 	HANDLE stdout_wr;
+#else
+	FILE *file;
+	pid_t pid;
 #endif
 };
 
@@ -37,10 +37,10 @@ namespace process_internal
 {
 	bool is_open(Private *priv)
 	{
-#if CROWN_PLATFORM_POSIX
-		return priv->pid != -1;
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 		return priv->process.hProcess != 0;
+#else
+		return priv->pid != -1;
 #endif
 	}
 
@@ -51,10 +51,10 @@ Process::Process()
 	CE_STATIC_ASSERT(sizeof(_data) >= sizeof(*_priv));
 	_priv = new (_data) Private();
 
-#if CROWN_PLATFORM_POSIX
-	_priv->pid = -1;
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 	memset(&_priv->process, 0, sizeof(_priv->process));
+#else
+	_priv->pid = -1;
 #endif
 }
 
@@ -68,61 +68,7 @@ s32 Process::spawn(const char * const *argv, u32 flags)
 {
 	CE_ENSURE(process_internal::is_open(_priv) == false);
 
-#if CROWN_PLATFORM_POSIX
-	// https://opensource.apple.com/source/Libc/Libc-167/gen.subproj/popen.c.auto.html
-	int fildes[2];
-	pid_t pid;
-
-	if (flags & CROWN_PROCESS_STDIN_PIPE || flags & CROWN_PROCESS_STDOUT_PIPE) {
-		if (pipe(fildes) < 0)
-			return -1;
-	}
-
-	pid = fork();
-	if (pid == -1) { // Error, cleanup and return
-		close(fildes[0]);
-		close(fildes[1]);
-		return -1;
-	} else if (pid == 0) { // Child
-		if (flags & CROWN_PROCESS_STDOUT_PIPE) {
-			if (fildes[1] != STDOUT_FILENO) {
-				dup2(fildes[1], STDOUT_FILENO);
-				close(fildes[1]);
-				fildes[1] = STDOUT_FILENO;
-			}
-			close(fildes[0]);
-
-			if (flags & CROWN_PROCESS_STDERR_MERGE) {
-				dup2(fildes[1], 2);
-			}
-		} else if (flags & CROWN_PROCESS_STDIN_PIPE) {
-			if (fildes[0] != STDIN_FILENO) {
-				dup2(fildes[0], STDIN_FILENO);
-				close(fildes[0]);
-				fildes[0] = STDIN_FILENO;
-			}
-			close(fildes[1]);
-		}
-
-		execvp(argv[0], (char * const *)argv);
-		// exec returned error
-		return -1;
-	}
-
-	// Parent
-	if (flags & CROWN_PROCESS_STDOUT_PIPE) {
-		_priv->file = fdopen(fildes[0], "r");
-		close(fildes[1]);
-	} else if (flags & CROWN_PROCESS_STDIN_PIPE) {
-		_priv->file = fdopen(fildes[1], "w");
-		close(fildes[0]);
-	} else {
-		_priv->file = NULL;
-	}
-
-	_priv->pid = pid;
-	return 0;
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 	TempAllocator512 ta;
 	StringStream path(ta);
 
@@ -187,24 +133,78 @@ s32 Process::spawn(const char * const *argv, u32 flags)
 	if (flags & CROWN_PROCESS_STDOUT_PIPE)
 		CloseHandle(_priv->stdout_wr);
 	return 0;
+#else
+	// https://opensource.apple.com/source/Libc/Libc-167/gen.subproj/popen.c.auto.html
+	int fildes[2];
+	pid_t pid;
+
+	if (flags & CROWN_PROCESS_STDIN_PIPE || flags & CROWN_PROCESS_STDOUT_PIPE) {
+		if (pipe(fildes) < 0)
+			return -1;
+	}
+
+	pid = fork();
+	if (pid == -1) { // Error, cleanup and return
+		close(fildes[0]);
+		close(fildes[1]);
+		return -1;
+	} else if (pid == 0) { // Child
+		if (flags & CROWN_PROCESS_STDOUT_PIPE) {
+			if (fildes[1] != STDOUT_FILENO) {
+				dup2(fildes[1], STDOUT_FILENO);
+				close(fildes[1]);
+				fildes[1] = STDOUT_FILENO;
+			}
+			close(fildes[0]);
+
+			if (flags & CROWN_PROCESS_STDERR_MERGE) {
+				dup2(fildes[1], 2);
+			}
+		} else if (flags & CROWN_PROCESS_STDIN_PIPE) {
+			if (fildes[0] != STDIN_FILENO) {
+				dup2(fildes[0], STDIN_FILENO);
+				close(fildes[0]);
+				fildes[0] = STDIN_FILENO;
+			}
+			close(fildes[1]);
+		}
+
+		execvp(argv[0], (char * const *)argv);
+		// exec returned error
+		return -1;
+	}
+
+	// Parent
+	if (flags & CROWN_PROCESS_STDOUT_PIPE) {
+		_priv->file = fdopen(fildes[0], "r");
+		close(fildes[1]);
+	} else if (flags & CROWN_PROCESS_STDIN_PIPE) {
+		_priv->file = fdopen(fildes[1], "w");
+		close(fildes[0]);
+	} else {
+		_priv->file = NULL;
+	}
+
+	_priv->pid = pid;
+	return 0;
 #endif
 }
 
 bool Process::spawned()
 {
-#if CROWN_PLATFORM_POSIX
-	return _priv->pid != -1;
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 	return _priv->process.hProcess != 0;
+#else
+	return _priv->pid != -1;
 #endif
 }
 
 void Process::force_exit()
 {
 	CE_ENSURE(process_internal::is_open(_priv) == true);
-#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_WINDOWS
+#else
 	kill(_priv->pid, SIGKILL);
-#elif CROWN_PLATFORM_WINDOWS
 #endif
 }
 
@@ -212,7 +212,15 @@ s32 Process::wait()
 {
 	CE_ENSURE(process_internal::is_open(_priv) == true);
 
-#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_WINDOWS
+	DWORD exitcode = 1;
+	::WaitForSingleObject(_priv->process.hProcess, INFINITE);
+	GetExitCodeProcess(_priv->process.hProcess, &exitcode);
+	CloseHandle(_priv->process.hProcess);
+	CloseHandle(_priv->process.hThread);
+	memset(&_priv->process, 0, sizeof(_priv->process));
+	return (s32)exitcode;
+#else
 	pid_t pid;
 	int wstatus;
 
@@ -227,38 +235,13 @@ s32 Process::wait()
 
 	_priv->pid = -1;
 	return WIFEXITED(wstatus) ? (s32)WEXITSTATUS(wstatus) : -1;
-#elif CROWN_PLATFORM_WINDOWS
-	DWORD exitcode = 1;
-	::WaitForSingleObject(_priv->process.hProcess, INFINITE);
-	GetExitCodeProcess(_priv->process.hProcess, &exitcode);
-	CloseHandle(_priv->process.hProcess);
-	CloseHandle(_priv->process.hThread);
-	memset(&_priv->process, 0, sizeof(_priv->process));
-	return (s32)exitcode;
 #endif
 }
 
 char *Process::read(u32 *num_bytes_read, char *data, u32 len)
 {
 	CE_ENSURE(process_internal::is_open(_priv) == true);
-#if CROWN_PLATFORM_POSIX
-	CE_ENSURE(_priv->file != NULL);
-	size_t read = fread(data, 1, len, _priv->file);
-	if (read != len) {
-		if (feof(_priv->file) != 0) {
-			if (read == 0) {
-				*num_bytes_read = 0;
-				return NULL;
-			}
-		} else if (ferror(_priv->file) != 0) {
-			*num_bytes_read = UINT32_MAX;
-			return NULL;
-		}
-	}
-
-	*num_bytes_read = read;
-	return data;
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 	DWORD read;
 	BOOL success = FALSE;
 
@@ -276,6 +259,23 @@ char *Process::read(u32 *num_bytes_read, char *data, u32 len)
 		*num_bytes_read = read;
 		return data;
 	}
+#else
+	CE_ENSURE(_priv->file != NULL);
+	size_t read = fread(data, 1, len, _priv->file);
+	if (read != len) {
+		if (feof(_priv->file) != 0) {
+			if (read == 0) {
+				*num_bytes_read = 0;
+				return NULL;
+			}
+		} else if (ferror(_priv->file) != 0) {
+			*num_bytes_read = UINT32_MAX;
+			return NULL;
+		}
+	}
+
+	*num_bytes_read = read;
+	return data;
 #endif
 }
 

+ 3 - 5
src/core/process.h

@@ -7,14 +7,12 @@
 
 #include "core/strings/types.h"
 
-#if CROWN_PLATFORM_POSIX
-	#define EXE_PREFIX "./"
-	#define EXE_SUFFIX ""
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 	#define EXE_PREFIX ""
 	#define EXE_SUFFIX ".exe"
 #else
-	#error "Unknown platform"
+	#define EXE_PREFIX "./"
+	#define EXE_SUFFIX ""
 #endif // CROWN_PLATFORM_POSIX
 
 #define EXE_PATH(exe) EXE_PREFIX exe EXE_SUFFIX

+ 18 - 18
src/core/thread/condition_variable.cpp

@@ -9,23 +9,23 @@
 #include "core/thread/mutex.h"
 #include <new>
 
-#if CROWN_PLATFORM_POSIX
-	#include <pthread.h>
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 	#ifndef WIN32_LEAN_AND_MEAN
 		#define WIN32_LEAN_AND_MEAN
 	#endif
 	#include <windows.h>
+#else
+	#include <pthread.h>
 #endif
 
 namespace crown
 {
 struct Private
 {
-#if CROWN_PLATFORM_POSIX
-	pthread_cond_t cond;
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 	CONDITION_VARIABLE cv;
+#else
+	pthread_cond_t cond;
 #endif
 };
 
@@ -34,46 +34,46 @@ ConditionVariable::ConditionVariable()
 	CE_STATIC_ASSERT(sizeof(_data) >= sizeof(*_priv));
 	_priv = new (_data) Private();
 
-#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_WINDOWS
+	InitializeConditionVariable(&_priv->cv);
+#else
 	int err = pthread_cond_init(&_priv->cond, NULL);
 	CE_ASSERT(err == 0, "pthread_cond_init: errno = %d", err);
 	CE_UNUSED(err);
-#elif CROWN_PLATFORM_WINDOWS
-	InitializeConditionVariable(&_priv->cv);
 #endif
 }
 
 ConditionVariable::~ConditionVariable()
 {
-#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_WINDOWS
+	// Do nothing
+#else
 	int err = pthread_cond_destroy(&_priv->cond);
 	CE_ASSERT(err == 0, "pthread_cond_destroy: errno = %d", err);
 	CE_UNUSED(err);
-#elif CROWN_PLATFORM_WINDOWS
-	// Do nothing
 #endif
 	_priv->~Private();
 }
 
 void ConditionVariable::wait(Mutex &mutex)
 {
-#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_WINDOWS
+	SleepConditionVariableCS(&_priv->cv, (CRITICAL_SECTION *)mutex.native_handle(), INFINITE);
+#else
 	int err = pthread_cond_wait(&_priv->cond, (pthread_mutex_t *)mutex.native_handle());
 	CE_ASSERT(err == 0, "pthread_cond_wait: errno = %d", err);
 	CE_UNUSED(err);
-#elif CROWN_PLATFORM_WINDOWS
-	SleepConditionVariableCS(&_priv->cv, (CRITICAL_SECTION *)mutex.native_handle(), INFINITE);
 #endif
 }
 
 void ConditionVariable::signal()
 {
-#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_WINDOWS
+	WakeConditionVariable(&_priv->cv);
+#else
 	int err = pthread_cond_signal(&_priv->cond);
 	CE_ASSERT(err == 0, "pthread_cond_signal: errno = %d", err);
 	CE_UNUSED(err);
-#elif CROWN_PLATFORM_WINDOWS
-	WakeConditionVariable(&_priv->cv);
 #endif
 }
 

+ 21 - 21
src/core/thread/mutex.cpp

@@ -8,23 +8,23 @@
 #include "core/thread/mutex.h"
 #include <new>
 
-#if CROWN_PLATFORM_POSIX
-	#include <pthread.h>
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 	#ifndef WIN32_LEAN_AND_MEAN
 		#define WIN32_LEAN_AND_MEAN
 	#endif
 	#include <windows.h>
+#else
+	#include <pthread.h>
 #endif
 
 namespace crown
 {
 struct Private
 {
-#if CROWN_PLATFORM_POSIX
-	pthread_mutex_t mutex;
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 	CRITICAL_SECTION cs;
+#else
+	pthread_mutex_t mutex;
 #endif
 };
 
@@ -33,7 +33,9 @@ Mutex::Mutex()
 	CE_STATIC_ASSERT(sizeof(_data) >= sizeof(*_priv));
 	_priv = new (_data) Private();
 
-#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_WINDOWS
+	InitializeCriticalSection(&_priv->cs);
+#else
 	pthread_mutexattr_t attr;
 	int err = pthread_mutexattr_init(&attr);
 	CE_ASSERT(err == 0, "pthread_mutexattr_init: errno = %d", err);
@@ -44,51 +46,49 @@ Mutex::Mutex()
 	err = pthread_mutexattr_destroy(&attr);
 	CE_ASSERT(err == 0, "pthread_mutexattr_destroy: errno = %d", err);
 	CE_UNUSED(err);
-#elif CROWN_PLATFORM_WINDOWS
-	InitializeCriticalSection(&_priv->cs);
 #endif
 }
 
 Mutex::~Mutex()
 {
-#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_WINDOWS
+	DeleteCriticalSection(&_priv->cs);
+#else
 	int err = pthread_mutex_destroy(&_priv->mutex);
 	CE_ASSERT(err == 0, "pthread_mutex_destroy: errno = %d", err);
 	CE_UNUSED(err);
-#elif CROWN_PLATFORM_WINDOWS
-	DeleteCriticalSection(&_priv->cs);
 #endif
 	_priv->~Private();
 }
 
 void Mutex::lock()
 {
-#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_WINDOWS
+	EnterCriticalSection(&_priv->cs);
+#else
 	int err = pthread_mutex_lock(&_priv->mutex);
 	CE_ASSERT(err == 0, "pthread_mutex_lock: errno = %d", err);
 	CE_UNUSED(err);
-#elif CROWN_PLATFORM_WINDOWS
-	EnterCriticalSection(&_priv->cs);
 #endif
 }
 
 void Mutex::unlock()
 {
-#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_WINDOWS
+	LeaveCriticalSection(&_priv->cs);
+#else
 	int err = pthread_mutex_unlock(&_priv->mutex);
 	CE_ASSERT(err == 0, "pthread_mutex_unlock: errno = %d", err);
 	CE_UNUSED(err);
-#elif CROWN_PLATFORM_WINDOWS
-	LeaveCriticalSection(&_priv->cs);
 #endif
 }
 
 void *Mutex::native_handle()
 {
-#if CROWN_PLATFORM_POSIX
-	return &_priv->mutex;
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 	return &_priv->cs;
+#else
+	return &_priv->mutex;
 #endif
 }
 

+ 35 - 35
src/core/thread/semaphore.cpp

@@ -8,26 +8,26 @@
 #include "core/thread/semaphore.h"
 #include <new>
 
-#if CROWN_PLATFORM_POSIX
-	#include <pthread.h>
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 	#ifndef WIN32_LEAN_AND_MEAN
 		#define WIN32_LEAN_AND_MEAN
 	#endif
 	#include <windows.h>
 	#include <limits.h> // LONG_MAX
+#else
+	#include <pthread.h>
 #endif
 
 namespace crown
 {
 struct Private
 {
-#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_WINDOWS
+	HANDLE handle;
+#else
 	pthread_mutex_t mutex;
 	pthread_cond_t cond;
 	s32 count;
-#elif CROWN_PLATFORM_WINDOWS
-	HANDLE handle;
 #endif
 };
 
@@ -36,7 +36,11 @@ Semaphore::Semaphore()
 	CE_STATIC_ASSERT(sizeof(_data) >= sizeof(*_priv));
 	_priv = new (_data) Private();
 
-#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_WINDOWS
+	_priv->handle = CreateSemaphore(NULL, 0, LONG_MAX, NULL);
+	CE_ASSERT(_priv->handle != NULL, "CreateSemaphore: GetLastError = %d", GetLastError());
+	CE_UNUSED(_priv->handle);
+#else
 	int err = 0;
 	err = pthread_mutex_init(&_priv->mutex, NULL);
 	CE_ASSERT(err == 0, "pthread_mutex_init: errno = %d", err);
@@ -44,23 +48,19 @@ Semaphore::Semaphore()
 	CE_ASSERT(err == 0, "pthread_cond_init: errno = %d", err);
 	CE_UNUSED(err);
 	_priv->count = 0;
-#elif CROWN_PLATFORM_WINDOWS
-	_priv->handle = CreateSemaphore(NULL, 0, LONG_MAX, NULL);
-	CE_ASSERT(_priv->handle != NULL, "CreateSemaphore: GetLastError = %d", GetLastError());
-	CE_UNUSED(_priv->handle);
 #endif
 }
 
 Semaphore::~Semaphore()
 {
-#if CROWN_PLATFORM_POSIX
-	int err = pthread_cond_destroy(&_priv->cond);
-	CE_ASSERT(err == 0, "pthread_cond_destroy: errno = %d", err);
-	CE_UNUSED(err);
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 	BOOL err = CloseHandle(_priv->handle);
 	CE_ASSERT(err != 0, "CloseHandle: GetLastError = %d", GetLastError());
 	CE_UNUSED(err);
+#else
+	int err = pthread_cond_destroy(&_priv->cond);
+	CE_ASSERT(err == 0, "pthread_cond_destroy: errno = %d", err);
+	CE_UNUSED(err);
 #endif
 
 	_priv->~Private();
@@ -68,7 +68,11 @@ Semaphore::~Semaphore()
 
 void Semaphore::post(u32 count)
 {
-#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_WINDOWS
+	BOOL err = ReleaseSemaphore(_priv->handle, count, NULL);
+	CE_ASSERT(err != 0, "ReleaseSemaphore: GetLastError = %d", GetLastError());
+	CE_UNUSED(err);
+#else
 	pthread_mutex_lock(&_priv->mutex);
 	for (u32 i = 0; i < count; ++i) {
 		int err = pthread_cond_signal(&_priv->cond);
@@ -78,16 +82,16 @@ void Semaphore::post(u32 count)
 
 	_priv->count += count;
 	pthread_mutex_unlock(&_priv->mutex);
-#elif CROWN_PLATFORM_WINDOWS
-	BOOL err = ReleaseSemaphore(_priv->handle, count, NULL);
-	CE_ASSERT(err != 0, "ReleaseSemaphore: GetLastError = %d", GetLastError());
-	CE_UNUSED(err);
 #endif
 }
 
 void Semaphore::wait()
 {
-#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_WINDOWS
+	DWORD err = WaitForSingleObject(_priv->handle, INFINITE);
+	CE_ASSERT(err == WAIT_OBJECT_0, "WaitForSingleObject: GetLastError = %d", GetLastError());
+	CE_UNUSED(err);
+#else
 	pthread_mutex_lock(&_priv->mutex);
 	while (_priv->count <= 0) {
 		int err = pthread_cond_wait(&_priv->cond, &_priv->mutex);
@@ -96,16 +100,20 @@ void Semaphore::wait()
 	}
 	_priv->count--;
 	pthread_mutex_unlock(&_priv->mutex);
-#elif CROWN_PLATFORM_WINDOWS
-	DWORD err = WaitForSingleObject(_priv->handle, INFINITE);
-	CE_ASSERT(err == WAIT_OBJECT_0, "WaitForSingleObject: GetLastError = %d", GetLastError());
-	CE_UNUSED(err);
 #endif
 }
 
 bool Semaphore::try_wait()
 {
-#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_WINDOWS
+	DWORD err = WaitForSingleObject(_priv->handle, 0u);
+	if (err == WAIT_OBJECT_0)
+		return true;
+	else if (err == WAIT_TIMEOUT)
+		return false;
+	CE_FATAL("WaitForSingleObject: GetLastError = %d", GetLastError());
+	return false;
+#else
 	pthread_mutex_lock(&_priv->mutex);
 	if (_priv->count == 0) {
 		pthread_mutex_unlock(&_priv->mutex);
@@ -120,14 +128,6 @@ bool Semaphore::try_wait()
 	_priv->count--;
 	pthread_mutex_unlock(&_priv->mutex);
 	return true;
-#elif CROWN_PLATFORM_WINDOWS
-	DWORD err = WaitForSingleObject(_priv->handle, 0u);
-	if (err == WAIT_OBJECT_0)
-		return true;
-	else if (err == WAIT_TIMEOUT)
-		return false;
-	CE_FATAL("WaitForSingleObject: GetLastError = %d", GetLastError());
-	return false;
 #endif
 }
 

+ 25 - 25
src/core/thread/thread.cpp

@@ -9,14 +9,14 @@
 #include "core/thread/thread.h"
 #include <new>
 
-#if CROWN_PLATFORM_POSIX
-	#include <pthread.h>
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 	#ifndef WIN32_LEAN_AND_MEAN
 		#define WIN32_LEAN_AND_MEAN
 	#endif
 	#include <windows.h>
 	#include <process.h>
+#else
+	#include <pthread.h>
 #endif
 
 namespace crown
@@ -28,26 +28,26 @@ struct Private
 	Semaphore _sem;
 	bool _is_running;
 	s32 _exit_code;
-#if CROWN_PLATFORM_POSIX
-	pthread_t handle;
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 	HANDLE handle;
+#else
+	pthread_t handle;
 #endif
 };
 
-#if CROWN_PLATFORM_POSIX
-static void *thread_proc(void *arg)
+#if CROWN_PLATFORM_WINDOWS
+static DWORD WINAPI thread_proc(void *arg)
 {
 	Thread *thread = (Thread *)arg;
 	thread->_priv->_sem.post();
-	return (void *)(uintptr_t)thread->_priv->_function(thread->_priv->_user_data);
+	return thread->_priv->_function(thread->_priv->_user_data);
 }
-#elif CROWN_PLATFORM_WINDOWS
-static DWORD WINAPI thread_proc(void *arg)
+#else
+static void *thread_proc(void *arg)
 {
 	Thread *thread = (Thread *)arg;
 	thread->_priv->_sem.post();
-	return thread->_priv->_function(thread->_priv->_user_data);
+	return (void *)(uintptr_t)thread->_priv->_function(thread->_priv->_user_data);
 }
 #endif
 
@@ -60,10 +60,10 @@ Thread::Thread()
 	_priv->_is_running = false;
 	_priv->_exit_code = 0;
 
-#if CROWN_PLATFORM_POSIX
-	_priv->handle = 0;
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 	_priv->handle = INVALID_HANDLE_VALUE;
+#else
+	_priv->handle = 0;
 #endif
 }
 
@@ -82,7 +82,10 @@ void Thread::start(ThreadFunction func, void *user_data, u32 stack_size)
 	_priv->_function = func;
 	_priv->_user_data = user_data;
 
-#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_WINDOWS
+	_priv->handle = CreateThread(NULL, stack_size, thread_proc, this, 0, NULL);
+	CE_ASSERT(_priv->handle != NULL, "CreateThread: GetLastError = %d", GetLastError());
+#else
 	pthread_attr_t attr;
 	int err = pthread_attr_init(&attr);
 	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
@@ -99,9 +102,6 @@ void Thread::start(ThreadFunction func, void *user_data, u32 stack_size)
 	err = pthread_attr_destroy(&attr);
 	CE_ASSERT(err == 0, "pthread_attr_destroy: errno = %d", err);
 	CE_UNUSED(err);
-#elif CROWN_PLATFORM_WINDOWS
-	_priv->handle = CreateThread(NULL, stack_size, thread_proc, this, 0, NULL);
-	CE_ASSERT(_priv->handle != NULL, "CreateThread: GetLastError = %d", GetLastError());
 #endif
 
 	_priv->_is_running = true;
@@ -112,18 +112,18 @@ void Thread::stop()
 {
 	CE_ASSERT(_priv->_is_running, "Thread is not running");
 
-#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_WINDOWS
+	WaitForSingleObject(_priv->handle, INFINITE);
+	GetExitCodeThread(_priv->handle, (DWORD *)&_priv->_exit_code);
+	CloseHandle(_priv->handle);
+	_priv->handle = INVALID_HANDLE_VALUE;
+#else
 	void *retval;
 	int err = pthread_join(_priv->handle, &retval);
 	CE_ASSERT(err == 0, "pthread_join: errno = %d", err);
 	CE_UNUSED(err);
 	_priv->_exit_code = (s32)(uintptr_t)retval;
 	_priv->handle = 0;
-#elif CROWN_PLATFORM_WINDOWS
-	WaitForSingleObject(_priv->handle, INFINITE);
-	GetExitCodeThread(_priv->handle, (DWORD *)&_priv->_exit_code);
-	CloseHandle(_priv->handle);
-	_priv->handle = INVALID_HANDLE_VALUE;
 #endif
 
 	_priv->_is_running = false;

+ 18 - 16
src/core/time.cpp

@@ -5,13 +5,15 @@
 
 #include "core/time.h"
 
-#if CROWN_PLATFORM_POSIX
-	#include <time.h> // clock_gettime
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 	#ifndef WIN32_LEAN_AND_MEAN
 		#define WIN32_LEAN_AND_MEAN
 	#endif
 	#include <windows.h>
+#elif CROWN_PLATFORM_OSX
+	#include <sys/time.h> // gettimeofday
+#else
+	#include <time.h> // clock_gettime
 #endif
 
 namespace crown
@@ -20,31 +22,31 @@ namespace time
 {
 	s64 now()
 	{
-#if CROWN_PLATFORM_LINUX || CROWN_PLATFORM_ANDROID
-		timespec now;
-		clock_gettime(CLOCK_MONOTONIC, &now);
-		return now.tv_sec * s64(1000000000) + now.tv_nsec;
+#if CROWN_PLATFORM_WINDOWS
+		LARGE_INTEGER ttime;
+		QueryPerformanceCounter(&ttime);
+		return (s64)ttime.QuadPart;
 #elif CROWN_PLATFORM_OSX
 		struct timeval now;
 		gettimeofday(&now, NULL);
 		return now.tv_sec * s64(1000000) + now.tv_usec;
-#elif CROWN_PLATFORM_WINDOWS
-		LARGE_INTEGER ttime;
-		QueryPerformanceCounter(&ttime);
-		return (s64)ttime.QuadPart;
+#else
+		timespec now;
+		clock_gettime(CLOCK_MONOTONIC, &now);
+		return now.tv_sec * s64(1000000000) + now.tv_nsec;
 #endif
 	}
 
 	inline s64 frequency()
 	{
-#if CROWN_PLATFORM_LINUX || CROWN_PLATFORM_ANDROID
-		return s64(1000000000);
-#elif CROWN_PLATFORM_OSX
-		return s64(1000000);
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 		LARGE_INTEGER freq;
 		QueryPerformanceFrequency(&freq);
 		return (s64)freq.QuadPart;
+#elif CROWN_PLATFORM_OSX
+		return s64(1000000);
+#else
+		return s64(1000000000);
 #endif
 	}
 

+ 30 - 30
src/core/unit_tests.cpp

@@ -1296,75 +1296,75 @@ static void test_sjson()
 
 static void test_path()
 {
-#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_WINDOWS
 	{
-		const bool a = path::is_absolute("/home/foo");
+		const bool a = path::is_absolute("C:\\Users\\foo");
 		ENSURE(a == true);
-		const bool b = path::is_absolute("home/foo");
+		const bool b = path::is_absolute("Users\\foo");
 		ENSURE(b == false);
 	}
 	{
-		const bool a = path::is_relative("/home/foo");
+		const bool a = path::is_relative("D:\\Users\\foo");
 		ENSURE(a == false);
-		const bool b = path::is_relative("home/foo");
+		const bool b = path::is_relative("Users\\foo");
 		ENSURE(b == true);
 	}
 	{
-		const bool a = path::is_root("/");
+		const bool a = path::is_root("E:\\");
 		ENSURE(a == true);
-		const bool b = path::is_root("/home");
+		const bool b = path::is_root("E:\\Users");
 		ENSURE(b == false);
 	}
 	{
-		ENSURE(path::has_trailing_separator("/home/foo/"));
-		ENSURE(!path::has_trailing_separator("/home/foo"));
+		ENSURE(path::has_trailing_separator("C:\\Users\\foo\\"));
+		ENSURE(!path::has_trailing_separator("C:\\Users\\foo"));
 	}
 	{
 		TempAllocator128 ta;
 		DynamicString clean(ta);
-		path::reduce(clean, "/home//foo/");
-		ENSURE(clean == "/home/foo");
+		path::reduce(clean, "C:\\Users\\\\foo\\");
+		ENSURE(clean == "C:\\Users\\foo");
 	}
 	{
 		TempAllocator128 ta;
 		DynamicString clean(ta);
-		path::reduce(clean, "\\home\\\\foo\\");
-		ENSURE(clean == "/home/foo");
+		path::reduce(clean, "C:/Users//foo/");
+		ENSURE(clean == "C:\\Users\\foo");
 	}
 #else
 	{
-		const bool a = path::is_absolute("C:\\Users\\foo");
+		const bool a = path::is_absolute("/home/foo");
 		ENSURE(a == true);
-		const bool b = path::is_absolute("Users\\foo");
+		const bool b = path::is_absolute("home/foo");
 		ENSURE(b == false);
 	}
 	{
-		const bool a = path::is_relative("D:\\Users\\foo");
+		const bool a = path::is_relative("/home/foo");
 		ENSURE(a == false);
-		const bool b = path::is_relative("Users\\foo");
+		const bool b = path::is_relative("home/foo");
 		ENSURE(b == true);
 	}
 	{
-		const bool a = path::is_root("E:\\");
+		const bool a = path::is_root("/");
 		ENSURE(a == true);
-		const bool b = path::is_root("E:\\Users");
+		const bool b = path::is_root("/home");
 		ENSURE(b == false);
 	}
 	{
-		ENSURE(path::has_trailing_separator("C:\\Users\\foo\\"));
-		ENSURE(!path::has_trailing_separator("C:\\Users\\foo"));
+		ENSURE(path::has_trailing_separator("/home/foo/"));
+		ENSURE(!path::has_trailing_separator("/home/foo"));
 	}
 	{
 		TempAllocator128 ta;
 		DynamicString clean(ta);
-		path::reduce(clean, "C:\\Users\\\\foo\\");
-		ENSURE(clean == "C:\\Users\\foo");
+		path::reduce(clean, "/home//foo/");
+		ENSURE(clean == "/home/foo");
 	}
 	{
 		TempAllocator128 ta;
 		DynamicString clean(ta);
-		path::reduce(clean, "C:/Users//foo/");
-		ENSURE(clean == "C:\\Users\\foo");
+		path::reduce(clean, "\\home\\\\foo\\");
+		ENSURE(clean == "/home/foo");
 	}
 #endif // CROWN_PLATFORM_POSIX
 	{
@@ -1425,19 +1425,19 @@ static void test_path()
 		path::join(path, "", "bar");
 		ENSURE(path == "bar");
 	}
-#if CROWN_PLATFORM_POSIX
+#if CROWN_PLATFORM_WINDOWS
 	{
 		TempAllocator128 ta;
 		DynamicString path(ta);
-		path::join(path, "foo", "bar");
-		ENSURE(path == "foo/bar");
+		path::join(path, "C:\\foo", "bar");
+		ENSURE(path == "C:\\foo\\bar");
 	}
 #else
 	{
 		TempAllocator128 ta;
 		DynamicString path(ta);
-		path::join(path, "C:\\foo", "bar");
-		ENSURE(path == "C:\\foo\\bar");
+		path::join(path, "foo", "bar");
+		ENSURE(path == "foo/bar");
 	}
 #endif // CROWN_PLATFORM_POSIX
 }

+ 12 - 12
src/resource/data_compiler.cpp

@@ -47,13 +47,13 @@
 #include "resource/unit_resource.h"
 #include <algorithm>
 #include <inttypes.h>
-#if CROWN_PLATFORM_POSIX
-	#include <signal.h>
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 	#ifndef WIN32_LEAN_AND_MEAN
 		#define WIN32_LEAN_AND_MEAN
 	#endif
 	#include <windows.h>
+#else
+	#include <signal.h>
 #endif // CROWN_PLATFORM_LINUX
 
 LOG_SYSTEM(DATA_COMPILER, "data_compiler")
@@ -1224,15 +1224,7 @@ void DataCompiler::file_monitor_callback(void *thiz, FileMonitorEvent::Enum fme,
 
 int main_data_compiler(const DeviceOptions &opts)
 {
-#if CROWN_PLATFORM_POSIX
-	struct sigaction old_SIGINT;
-	struct sigaction act;
-	act.sa_handler = [](int /*signum*/) { _quit = true; };
-	sigemptyset(&act.sa_mask);
-	act.sa_flags = 0;
-	sigaction(SIGINT, NULL, &old_SIGINT);
-	sigaction(SIGINT, &act, NULL);
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 	// code-format off
 	PHANDLER_ROUTINE signal_handler = [](DWORD dwCtrlType) {
 		switch (dwCtrlType) {
@@ -1246,6 +1238,14 @@ int main_data_compiler(const DeviceOptions &opts)
 	};
 	// code-format on
 	SetConsoleCtrlHandler(signal_handler, TRUE);
+#else
+	struct sigaction old_SIGINT;
+	struct sigaction act;
+	act.sa_handler = [](int /*signum*/) { _quit = true; };
+	sigemptyset(&act.sa_mask);
+	act.sa_flags = 0;
+	sigaction(SIGINT, NULL, &old_SIGINT);
+	sigaction(SIGINT, &act, NULL);
 #endif // CROWN_PLATFORM_POSIX
 
 	console_server_globals::init();

+ 4 - 4
tools/level_editor/preferences_dialog.vala

@@ -203,7 +203,10 @@ public class PreferencesDialog : Gtk.Dialog
 		if (preferences.has_key("theme"))
 			_theme_combo.value = (string)preferences["theme"];
 
-#if CROWN_PLATFORM_LINUX
+#if CROWN_PLATFORM_WINDOWS
+		_lua_external_tool_button.set_app(AppChooserButton.APP_DEFAULT, null);
+		_image_external_tool_button.set_app(AppChooserButton.APP_DEFAULT, null);
+#else
 		// External tools.
 		Hashtable external_tools = preferences.has_key("external_tools")
 			? (Hashtable)preferences["external_tools"]
@@ -243,9 +246,6 @@ public class PreferencesDialog : Gtk.Dialog
 		else
 			app_id = null;
 		_image_external_tool_button.set_app(app, app_id);
-#else
-		_lua_external_tool_button.set_app(AppChooserButton.APP_DEFAULT, null);
-		_image_external_tool_button.set_app(AppChooserButton.APP_DEFAULT, null);
 #endif // CROWN_PLATFORM_LINUX
 	}
 

+ 3 - 3
tools/level_editor/project.vala

@@ -54,10 +54,10 @@ public class Project
 
 	public Project(Database db, DataCompiler dc)
 	{
-#if CROWN_PLATFORM_LINUX
-		_platform = "linux";
-#elif CROWN_PLATFORM_WINDOWS
+#if CROWN_PLATFORM_WINDOWS
 		_platform = "windows";
+#else
+		_platform = "linux";
 #endif // CROWN_PLATFORM_LINUX
 		_database = db;
 		_files = new Database();