Add PathUtils to Utils component

This commit is contained in:
Jan 2019-09-24 23:32:04 +02:00
parent 42af6df5d8
commit eeff08d772
3 changed files with 97 additions and 0 deletions

View File

@ -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>

View 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;
}
}

View 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);
};
}