diff --git a/Source/Engine/Platform/Win32/Win32FileSystem.cpp b/Source/Engine/Platform/Win32/Win32FileSystem.cpp index 486b1c603..ed11e0f90 100644 --- a/Source/Engine/Platform/Win32/Win32FileSystem.cpp +++ b/Source/Engine/Platform/Win32/Win32FileSystem.cpp @@ -8,16 +8,12 @@ #include "Engine/Core/Types/StringView.h" #include "Engine/Core/Math/Math.h" #include "Engine/Core/Collections/Array.h" +#include "Engine/Utilities/StringConverter.h" #include "IncludeWindowsHeaders.h" const DateTime WindowsEpoch(1970, 1, 1); -#define WIN32_INIT_BUFFER(path, buffer) \ - Char buffer[MAX_PATH]; \ - if (path.Length() > MAX_PATH) \ - return true; \ - Platform::MemoryCopy(buffer, path.Get(), path.Length() * sizeof(Char)); \ - buffer[path.Length()] = 0 +#define WIN32_INIT_BUFFER(path, buffer) StringAsTerminated<> buffer(path.Get(), path.Length()) bool Win32FileSystem::CreateDirectory(const StringView& path) { diff --git a/Source/Engine/Utilities/StringConverter.h b/Source/Engine/Utilities/StringConverter.h index 2dbccca52..fd56a40b9 100644 --- a/Source/Engine/Utilities/StringConverter.h +++ b/Source/Engine/Utilities/StringConverter.h @@ -132,3 +132,36 @@ public: this->_static = text; } }; + +template +class StringAsTerminated +{ +protected: + const CharType* _static = nullptr; + CharType* _dynamic = nullptr; + +public: + StringAsTerminated(const CharType* str, int32 length) + { + if (length != 0 && str[length] == 0) // Unsafe to access out of bounds... + { + _static = str; + } + else + { + _dynamic = (CharType*)Allocator::Allocate((length + 1) * sizeof(CharType)); + Platform::MemoryCopy(_dynamic, str, length * sizeof(CharType)); + _dynamic[length] = 0; + } + } + + ~StringAsTerminated() + { + Allocator::Free(_dynamic); + } + + operator const CharType*() const + { + return _static ? _static : _dynamic; + } +};