Files
OpenRCT2-Web/src/core/Path.cpp
T

76 lines
2.0 KiB
C++
Raw Normal View History

extern "C"
{
#include "../platform/platform.h"
#include "../localisation/localisation.h"
#include "../util/util.h"
}
2016-01-29 00:06:02 +00:00
#include "Math.hpp"
#include "Memory.hpp"
#include "Path.hpp"
#include "String.hpp"
#include "Util.hpp"
namespace Path
{
utf8 * Append(utf8 * buffer, size_t bufferSize, const utf8 * src)
{
return safe_strcat_path(buffer, src, bufferSize);
}
2016-01-29 00:06:02 +00:00
utf8 * GetFileNameWithoutExtension(utf8 * buffer, size_t bufferSize, const utf8 * path)
{
const utf8 * lastDot = nullptr;
const utf8 * ch = path;
for (; ch != '\0'; ch++)
{
if (*ch == '.')
{
lastDot = ch;
}
}
if (lastDot == nullptr)
{
return String::Set(buffer, bufferSize, path);
}
size_t truncatedLength = Math::Min<size_t>(bufferSize - 1, lastDot - path);
Memory::Copy(buffer, path, truncatedLength);
buffer[truncatedLength] = '\0';
return buffer;
}
utf8 * GetAbsolute(utf8 *buffer, size_t bufferSize, const utf8 * relativePath)
{
#if __WINDOWS__
wchar_t * relativePathW = utf8_to_widechar(relativePath);
wchar_t absolutePathW[MAX_PATH];
DWORD length = GetFullPathNameW(relativePathW, Util::CountOf(absolutePathW), absolutePathW, NULL);
Memory::Free(relativePathW);
if (length == 0)
{
return String::Set(buffer, bufferSize, relativePath);
}
else
{
utf8 * absolutePath = widechar_to_utf8(absolutePathW);
String::Set(buffer, bufferSize, absolutePath);
Memory::Free(absolutePath);
return buffer;
}
#else
utf8 * absolutePath = realpath(relativePath, NULL);
if (absolutePath == nullptr)
{
return String::Set(buffer, bufferSize, relativePath);
}
else
{
String::Set(buffer, bufferSize, absolutePath);
Memory::Free(absolutePath);
return buffer;
}
#endif
}
}