From 6772c651543825db21eb5cb3b74db0062be47bf6 Mon Sep 17 00:00:00 2001 From: Ilya Orlov Date: Thu, 24 May 2018 15:33:05 +0300 Subject: [PATCH] xrCore: now build on Linux --- src/xrCore/FS.cpp | 4 +- src/xrCore/FileSystem_borland.cpp | 3 +- src/xrCore/PostProcess/PPInfo.cpp | 2 +- src/xrCore/PostProcess/PostProcess.cpp | 2 +- src/xrCore/Threading/Event.cpp | 14 ++ src/xrCore/_types.h | 3 +- src/xrCore/stream_reader.cpp | 2 +- src/xrCore/stream_reader_inline.h | 5 +- src/xrCore/xrMemory.h | 2 + src/xrCore/xr_ini.cpp | 201 +++++++++++++++++++++++++ 10 files changed, 229 insertions(+), 9 deletions(-) diff --git a/src/xrCore/FS.cpp b/src/xrCore/FS.cpp index fee0ac73a49..1903af1caca 100644 --- a/src/xrCore/FS.cpp +++ b/src/xrCore/FS.cpp @@ -520,7 +520,7 @@ CVirtualFileRW::CVirtualFileRW(const char* cFileName) ::fstat(hSrcFile, &file_info); Size = (int)file_info.st_size; R_ASSERT3(Size, cFileName, xrDebug::ErrorToString(GetLastError())); - data = (char*)::mmap(NULL, 0, PROT_READ | PROT_WRITE, MAP_SHARED, hSrcFile, 0); + data = (char*)::mmap(NULL, Size, PROT_READ | PROT_WRITE, MAP_SHARED, hSrcFile, 0); #endif #ifdef FS_DEBUG register_file_mapping(data, Size, cFileName); @@ -563,7 +563,7 @@ CVirtualFileReader::CVirtualFileReader(const char* cFileName) ::fstat(hSrcFile, &file_info); Size = (int)file_info.st_size; R_ASSERT3(Size, cFileName, xrDebug::ErrorToString(GetLastError())); - data = (char*)::mmap(NULL, 0, PROT_READ, MAP_SHARED, hSrcFile, 0); + data = (char*)::mmap(NULL, Size, PROT_READ, MAP_SHARED, hSrcFile, 0); #endif R_ASSERT3(data, cFileName, xrDebug::ErrorToString(GetLastError())); diff --git a/src/xrCore/FileSystem_borland.cpp b/src/xrCore/FileSystem_borland.cpp index e84e5599e71..44aac455a21 100644 --- a/src/xrCore/FileSystem_borland.cpp +++ b/src/xrCore/FileSystem_borland.cpp @@ -1,12 +1,11 @@ //---------------------------------------------------- // file: FileSystem.cpp //---------------------------------------------------- -#if defined(WINDOWS) #include "stdafx.h" #pragma hdrstop #include "FileSystem.h" - +#ifdef WINDOWS #include #include #include diff --git a/src/xrCore/PostProcess/PPInfo.cpp b/src/xrCore/PostProcess/PPInfo.cpp index 67c521f228c..872cbd7d40d 100644 --- a/src/xrCore/PostProcess/PPInfo.cpp +++ b/src/xrCore/PostProcess/PPInfo.cpp @@ -1,4 +1,4 @@ -#include "StdAfx.h" +#include "stdafx.h" #include "PPInfo.hpp" SPPInfo& SPPInfo::add(const SPPInfo& ppi) diff --git a/src/xrCore/PostProcess/PostProcess.cpp b/src/xrCore/PostProcess/PostProcess.cpp index 7e9b810a4ba..191e06d19e7 100644 --- a/src/xrCore/PostProcess/PostProcess.cpp +++ b/src/xrCore/PostProcess/PostProcess.cpp @@ -1,4 +1,4 @@ -#include "StdAfx.h" +#include "stdafx.h" #include "PostProcess.hpp" // postprocess value LOAD method implementation diff --git a/src/xrCore/Threading/Event.cpp b/src/xrCore/Threading/Event.cpp index 6b2f2056c50..4cfa4ad22c4 100644 --- a/src/xrCore/Threading/Event.cpp +++ b/src/xrCore/Threading/Event.cpp @@ -1,5 +1,7 @@ #include "stdafx.h" #include "Event.hpp" +#if defined(WINDOWS) +#include Event::Event() noexcept { handle = (void*)CreateEvent(NULL, FALSE, FALSE, NULL); } Event::~Event() noexcept { CloseHandle(handle); } @@ -10,3 +12,15 @@ bool Event::Wait(u32 millisecondsTimeout) const noexcept { return WaitForSingleObject(handle, millisecondsTimeout) != WAIT_TIMEOUT; } +#elif defined(LINUX) +Event::Event() noexcept { handle = (void*)malloc(1); } +Event::~Event() noexcept { free(handle); } +void Event::Reset() noexcept { memset(handle, 1, 1); } +void Event::Set() noexcept { memset(handle, 0, 1); } +void Event::Wait() const noexcept { Sleep(0); } +bool Event::Wait(u32 millisecondsTimeout) const noexcept +{ + Sleep(millisecondsTimeout); + return true; +} +#endif diff --git a/src/xrCore/_types.h b/src/xrCore/_types.h index afbd03233bd..28df2e6243e 100644 --- a/src/xrCore/_types.h +++ b/src/xrCore/_types.h @@ -70,6 +70,7 @@ using string4096 = char[4096]; using string_path = char[2 * max_path]; // XXX: Replace __interface with either struct or class. MS defines it as struct for COM, but this project is C++. +#if defined(WINDOWS) #define xr_pure_interface __interface - +#endif #endif diff --git a/src/xrCore/stream_reader.cpp b/src/xrCore/stream_reader.cpp index e9b4391a6ec..df8e2834bcc 100644 --- a/src/xrCore/stream_reader.cpp +++ b/src/xrCore/stream_reader.cpp @@ -44,7 +44,7 @@ void CStreamReader::map(const u32& new_offset) (u8*)MapViewOfFile(m_file_mapping_handle, FILE_MAP_READ, 0, start_offset, m_current_window_size); #elif defined(LINUX) m_current_map_view_of_file = - (u8*)mmap(NULL, m_current_window_size, PROT_READ, MAP_SHARED, m_file_mapping_handle, start_offset); // TODO проверить не могу до полной сборки под Linux + (u8*)::mmap(NULL, m_current_window_size, PROT_READ, MAP_SHARED, m_file_mapping_handle, start_offset); // TODO проверить не могу до полной сборки под Linux #endif m_current_pointer = m_current_map_view_of_file; diff --git a/src/xrCore/stream_reader_inline.h b/src/xrCore/stream_reader_inline.h index 5e6e733a66b..6bc23bad2de 100644 --- a/src/xrCore/stream_reader_inline.h +++ b/src/xrCore/stream_reader_inline.h @@ -1,5 +1,8 @@ #ifndef STREAM_READER_INLINE_H #define STREAM_READER_INLINE_H +#ifdef LINUX +#include +#endif IC CStreamReader::CStreamReader() {} IC CStreamReader::CStreamReader(const CStreamReader& object) @@ -19,7 +22,7 @@ IC const HANDLE& CStreamReader::file_mapping_handle() const { return (m_file_map #if defined(WINDOWS) IC void CStreamReader::unmap() { UnmapViewOfFile(m_current_map_view_of_file); } #else -IC void CStreamReader::unmap() { ; } +IC void CStreamReader::unmap() { ::munmap(const_cast(m_current_map_view_of_file), m_current_window_size); } #endif IC void CStreamReader::remap(const u32& new_offset) { diff --git a/src/xrCore/xrMemory.h b/src/xrCore/xrMemory.h index 432caba34f5..2923a1716b9 100644 --- a/src/xrCore/xrMemory.h +++ b/src/xrCore/xrMemory.h @@ -11,7 +11,9 @@ Поэтому всё-же стоит переопределять для большинства случаев операторы new и delete. А для остального мы будем полагать (и надеяться), что прокси справится без проблем. */ +#if defined(WINDOWS) // Не знаю, как это на виндах работает, но на линуксе ломается линковка из-за множественного объявления __TBB_malloc_proxy_helper_object #include "tbb/tbbmalloc_proxy.h" +#endif class XRCORE_API xrMemory { diff --git a/src/xrCore/xr_ini.cpp b/src/xrCore/xr_ini.cpp index 891c269c875..df51a4bfca8 100644 --- a/src/xrCore/xr_ini.cpp +++ b/src/xrCore/xr_ini.cpp @@ -6,6 +6,207 @@ XRCORE_API CInifile const* pSettings = nullptr; XRCORE_API CInifile const* pSettingsAuth = nullptr; +#if defined(LINUX) +#include +#define MSVCRT_EINVAL 22 +#define MSVCRT_ERANGE 34 + +#define MSVCRT_UI64_MAX (((uint64_t)0xffffffff << 32) | 0xffffffff) + +/** + * from wine@dlls/msvcrt/string.c + * + * @param fileName + * @param readOnly + * @return + */ +int _cdecl _i64toa_s(int64_t value, char *str, size_t size, int radix){ + uint64_t val; + unsigned int digit; + int is_negative; + char buffer[65], *pos; + size_t len; + + if (!(str != NULL)) + return MSVCRT_EINVAL; + if (!(size > 0)) + return MSVCRT_EINVAL; + if (!(radix >= 2 && radix <= 36)) { + str[0] = '\0'; + return MSVCRT_EINVAL; + } + + if (value < 0 && radix == 10) { + is_negative = 1; + val = -value; + } else { + is_negative = 0; + val = value; + } + + pos = buffer + 64; + *pos = '\0'; + + do { + digit = val % radix; + val /= radix; + + if (digit < 10) + *--pos = '0' + digit; + else + *--pos = 'a' + digit - 10; + } while (val != 0); + + if (is_negative) + *--pos = '-'; + + len = buffer + 65 - pos; + if (len > size) { + size_t i; + char *p = str; + + /* Copy the temporary buffer backwards up to the available number of + * characters. Don't copy the negative sign if present. */ + + if (is_negative) { + p++; + size--; + } + + for (pos = buffer + 63, i = 0; i < size; i++) + *p++ = *pos--; + + str[0] = '\0'; + return MSVCRT_ERANGE; + } + + memcpy(str, pos, len); + return 0; +} + +int _cdecl _ui64toa_s(uint64_t value, char *str, size_t size, int radix) { + char buffer[65], *pos; + int digit; + + if (!(str != NULL)) + return MSVCRT_EINVAL; + if (!(size > 0)) + return MSVCRT_EINVAL; + if (!(radix >= 2 && radix <= 36)) { + str[0] = '\0'; + return MSVCRT_EINVAL; + } + + pos = buffer + 64; + *pos = '\0'; + + do { + digit = value % radix; + value /= radix; + + if (digit < 10) + *--pos = '0' + digit; + else + *--pos = 'a' + digit - 10; + } while (value != 0); + + if (buffer - pos + 65 > size) { + return MSVCRT_EINVAL; + } + + memcpy(str, pos, buffer - pos + 65); + return 0; +} + +LARGE_INTEGER _cdecl _atoi64( const char *str ) +{ + ULARGE_INTEGER RunningTotal = 0; + char bMinus = 0; + + while (*str == ' ' || (*str >= '\011' && *str <= '\015')) { + str++; + } /* while */ + + if (*str == '+') { + str++; + } else if (*str == '-') { + bMinus = 1; + str++; + } /* if */ + + while (*str >= '0' && *str <= '9') { + RunningTotal = RunningTotal * 10 + *str - '0'; + str++; + } /* while */ + + return bMinus ? -RunningTotal : RunningTotal; +} + +uint64_t _cdecl _strtoui64_l(const char *nptr, char **endptr, int base, locale_t locale) +{ + BOOL negative = FALSE; + uint64_t ret = 0; + + + if (!(nptr != NULL)) return 0; + if (!(base == 0 || base >= 2)) return 0; + if (!(base <= 36)) return 0; + + while(isspace(*nptr)) nptr++; + + if(*nptr == '-') { + negative = TRUE; + nptr++; + } else if(*nptr == '+') + nptr++; + + if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') { + base = 16; + nptr += 2; + } + + if(base == 0) { + if(*nptr=='0') + base = 8; + else + base = 10; + } + + while(*nptr) { + char cur = tolower(*nptr); + int v; + + if(isdigit(cur)) { + if(cur >= '0'+base) + break; + v = *nptr-'0'; + } else { + if(cur<'a' || cur>='a'+base-10) + break; + v = cur-'a'+10; + } + + nptr++; + + if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) { + ret = MSVCRT_UI64_MAX; + } else + ret = ret*base + v; + } + + if(endptr) + *endptr = (char*)nptr; + + return negative ? -ret : ret; +} + +uint64_t _cdecl _strtoui64(const char *nptr, char **endptr, int base) +{ + return _strtoui64_l(nptr, endptr, base, NULL); +} +#endif + + CInifile* CInifile::Create(pcstr fileName, bool readOnly) { return new CInifile(fileName, readOnly);