mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-04-19 15:52:53 +00:00
Add PathUtils to Utils component
This commit is contained in:
parent
42af6df5d8
commit
eeff08d772
@ -21,9 +21,11 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="Utils\ClassUtils.h" />
|
<ClInclude Include="Utils\ClassUtils.h" />
|
||||||
<ClInclude Include="Utils\FileAPI.h" />
|
<ClInclude Include="Utils\FileAPI.h" />
|
||||||
|
<ClInclude Include="Utils\PathUtils.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="Utils\FileAPI.cpp" />
|
<ClCompile Include="Utils\FileAPI.cpp" />
|
||||||
|
<ClCompile Include="Utils\PathUtils.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<PropertyGroup Label="Globals">
|
<PropertyGroup Label="Globals">
|
||||||
<VCProjectVersion>15.0</VCProjectVersion>
|
<VCProjectVersion>15.0</VCProjectVersion>
|
||||||
|
79
src/Utils/Utils/PathUtils.cpp
Normal file
79
src/Utils/Utils/PathUtils.cpp
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
#include "PathUtils.h"
|
||||||
|
|
||||||
|
namespace utils
|
||||||
|
{
|
||||||
|
std::string Path::GetFilename(std::string path)
|
||||||
|
{
|
||||||
|
const size_t lastSlashIndex = path.find_last_of("\\/");
|
||||||
|
if (std::string::npos != lastSlashIndex)
|
||||||
|
{
|
||||||
|
path.erase(0, lastSlashIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Path::GetFilenameWithoutExtension(std::string path)
|
||||||
|
{
|
||||||
|
const size_t lastSlashIndex = path.find_last_of("\\/");
|
||||||
|
if (std::string::npos != lastSlashIndex)
|
||||||
|
{
|
||||||
|
path.erase(0, lastSlashIndex + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove extension if present.
|
||||||
|
const size_t dotIndex = path.rfind('.');
|
||||||
|
if (std::string::npos != dotIndex)
|
||||||
|
{
|
||||||
|
path.erase(dotIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Path::GetExtension(std::string path)
|
||||||
|
{
|
||||||
|
const size_t lastSlashIndex = path.find_last_of("\\/");
|
||||||
|
const size_t lastDotIndex = path.find_last_of('.');
|
||||||
|
if (std::string::npos != lastDotIndex
|
||||||
|
&& (lastSlashIndex == std::string::npos || lastDotIndex > lastSlashIndex))
|
||||||
|
{
|
||||||
|
path.erase(0, lastDotIndex);
|
||||||
|
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Path::GetDirectory(std::string path)
|
||||||
|
{
|
||||||
|
const size_t lastSlashIndex = path.find_last_of("\\/");
|
||||||
|
if (std::string::npos != lastSlashIndex)
|
||||||
|
{
|
||||||
|
path.erase(lastSlashIndex);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return "./";
|
||||||
|
}
|
||||||
|
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Path::Combine(std::string p1, std::string p2)
|
||||||
|
{
|
||||||
|
char c;
|
||||||
|
|
||||||
|
while (!p1.empty() && (c = p1[p1.size() - 1], c == '\\' || c == '/'))
|
||||||
|
p1.erase(p1.size() - 1);
|
||||||
|
|
||||||
|
while (!p2.empty() && (c = p2[0], c == '\\' || c == '/'))
|
||||||
|
p2.erase(0);
|
||||||
|
|
||||||
|
if (!p1.empty())
|
||||||
|
p1 += '/';
|
||||||
|
|
||||||
|
return p1 + p2;
|
||||||
|
}
|
||||||
|
}
|
16
src/Utils/Utils/PathUtils.h
Normal file
16
src/Utils/Utils/PathUtils.h
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace utils
|
||||||
|
{
|
||||||
|
class Path
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static std::string GetFilename(std::string path);
|
||||||
|
static std::string GetFilenameWithoutExtension(std::string path);
|
||||||
|
static std::string GetExtension(std::string path);
|
||||||
|
static std::string GetDirectory(std::string path);
|
||||||
|
static std::string Combine(std::string p1, std::string p2);
|
||||||
|
};
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user