FilePath.h#

Parent directory (stream)

Provides a class representing a Filepath and common manipulating functions.

Includes#

Included By#

Namespaces#

Classes#

Source Code#

#pragma once

#include <cstdint>
#include <string>
#include <limits>

namespace pvr {
class FilePath : public std::string
{
public:
    FilePath() {}

    FilePath(const std::string& str) : std::string(str) {}

    std::string getFileExtension() const
    {
        size_t index = find_last_of(c_extensionSeparator, length());

        if (index != std::string::npos) { return substr((index + 1), length() - (index + 1)); }

        return std::string();
    }

    std::string getDirectory() const
    {
        const std::string::size_type c_objectNotFound = std::numeric_limits<std::string::size_type>::max();

        std::string::size_type index = static_cast<std::string::size_type>(find_last_of(c_unixDirectorySeparator, length()));

#if defined(_WIN32)
        if (index == std::string::npos) { index = static_cast<std::string::size_type>(find_last_of(c_windowsDirectorySeparator, length())); }
#endif

        if (index != c_objectNotFound) { return substr(0, index); }

        return std::string();
    }

    std::string getFilename() const
    {
        std::string::size_type index = static_cast<std::string::size_type>(find_last_of(c_unixDirectorySeparator, length()));

#if defined(_WIN32)
        if (index == std::string::npos) { index = static_cast<std::string::size_type>(find_last_of(c_windowsDirectorySeparator, length())); }
#endif

        if (index != std::string::npos) { return substr((index + 1), length() - (index + 1)); }

        return *this;
    }

    std::string getFilenameNoExtension() const
    {
        std::string::size_type extensionIndex = static_cast<std::string::size_type>(getFilename().find_last_of(c_extensionSeparator, length()));

        if (extensionIndex != std::string::npos) { return getFilename().substr(0, extensionIndex); }

        return getFilename();
    }

    static char getDirectorySeparator()
    {
#if defined(WIN32) || defined(_WIN32)
        return c_windowsDirectorySeparator;
#else
        return c_unixDirectorySeparator;
#endif
    }

private:
    static const char c_unixDirectorySeparator = '/';
    static const char c_windowsDirectorySeparator = '\\';
    static const char c_extensionSeparator = '.';
};
} // namespace pvr