<commit_before>﻿//======================================================================
//-----------------------------------------------------------------------
/**
 * @file        iutest_filepath.ipp
 * @brief       iris unit test ファイルパスクラス ファイル
 *
 * @author      t.shirayanagi
 * @par         copyright
 * Copyright (C) 2012-2018, <NAME>\n
 * This software is released under the new BSD License,
 * see LICENSE
*/
//-----------------------------------------------------------------------
//======================================================================
#ifndef INCG_IRIS_IUTEST_FILEPATH_IPP_D69E7545_BF8A_4EDC_9493_9105C69F9378_
#define INCG_IRIS_IUTEST_FILEPATH_IPP_D69E7545_BF8A_4EDC_9493_9105C69F9378_

//======================================================================
// include
#include "../internal/iutest_filepath.hpp"

#if defined(IUTEST_OS_WINDOWS) && !defined(IUTEST_OS_WINDOWS_MOBILE)
#  include <direct.h>
#endif

namespace iutest {
namespace detail
{

IUTEST_IPP_INLINE bool iuFilePath::IsDirectory() const
{
#if IUTEST_USE_CXX_FILESYSTEM
    return ::std::filesystem::is_directory(m_path);
#else
    if( m_path.empty() )
    {
        return false;
    }
    const char last = *m_path.rbegin();
    return (IsPathSeparator(last) || last == '.');
#endif
}

IUTEST_IPP_INLINE bool iuFilePath::IsRootDirectory() const
{
#ifdef IUTEST_OS_WINDOWS
    if( length() != 3 )
    {
        return false;
    }
#else
    if( length() != 3 )
    {
        return false;
    }
#endif
    return IsAbsolutePath();
}

IUTEST_IPP_INLINE bool iuFilePath::IsAbsolutePath() const
{
#if IUTEST_USE_CXX_FILESYSTEM
    return m_path.is_absolute();
#else
#ifdef IUTEST_OS_WINDOWS
    if( length() < 3 )
    {
        return false;
    }
    const char* name = m_path.c_str();

    if( IsDBCSLeadByte(name[0]) )
    {
        ++name;
    }
    return (name[1] == ':' && IsPathSeparator(name[0]));
#else
    return IsPathSeparator(*m_path.begin());
#endif
#endif
}

IUTEST_IPP_INLINE iuFilePath iuFilePath::RemoveTrailingPathSeparator() const
{
    ::std::string::const_iterator it = m_path.end()-1;
    while(it != m_path.begin() && IsPathSeparator(*it))
    {
        --it;
    }
    return iuFilePath(::std::string(m_path.begin(), it+1));
}

IUTEST_IPP_INLINE ::std::string iuFilePath::GetExtension() const
{
#if IUTEST_USE_CXX_FILESYSTEM
    return m_path.extension().generic_string();
#else
    const char* const path = m_path.c_str();
    const char* const ext = strrchr(path, '.');
    if(ext == NULL)
    {
        return "";
    }
    return ext;
#endif
}

IUTEST_IPP_INLINE iuFilePath iuFilePath::RemoveExtension(const char* extension) const
{
#if IUTEST_USE_CXX_FILESYSTEM
    const ::std::string path_string = m_path.generic_string();
#else
    const ::std::string& path_string = m_path;
#endif
    const char* const path = path_string.c_str();
    const char* const ext = strrchr(path, '.');
    if( ext == NULL )
    {
        return *this;
    }
    if( extension != NULL && !IsStringCaseEqual(ext + 1, extension) )
    {
        return *this;
    }
    const size_t length = ext - path;
    return iuFilePath(::std::string(path, length));
}

IUTEST_IPP_INLINE iuFilePath iuFilePath::RemoveDirectoryName() const
{
#if IUTEST_USE_CXX_FILESYSTEM
    return iuFilePath(m_path.parent_path());
#else
    const char* const sep = detail::FindLastPathSeparator(m_path.c_str(), m_path.length());
    return sep != NULL ? iuFilePath(sep+1) : *this;
#endif
}

IUTEST_IPP_INLINE iuFilePath iuFilePath::RemoveFileName() const
{
#if IUTEST_USE_CXX_FILESYSTEM
    if(m_path.has_filename()) {
        return iuFilePath(m_path.parent_path());
    }
    return iuFilePath(m_path);
#else
    const char* path = m_path.c_str();
    size_t pos = detail::FindLastPathSeparatorPosition(path, m_path.length());
    if( pos == ::std::string::npos )
    {
        return GetRelativeCurrentDir();
    }
    const size_t length = pos + 1;
    return iuFilePath(::std::string(path, length));
#endif
}

IUTEST_IPP_INLINE bool iuFilePath::CreateFolder() const
{
#if IUTEST_USE_CXX_FILESYSTEM
    return ::std::filesystem::create_directory(m_path);
#else
#if IUTEST_HAS_FILE_STAT

#if   defined(IUTEST_OS_WINDOWS_MOBILE)
#elif defined(IUTEST_OS_WINDOWS) && !defined(IUTEST_OS_WINDOWS_WINE)
    if( _mkdir(m_path.c_str()) == 0 )
    {
        return true;
    }
#else
    if( mkdir(m_path.c_str(), 0777) == 0 )
    {
        return true;
    }
#endif

#endif
    return DirectoryExists();
#endif
}

IUTEST_IPP_INLINE bool iuFilePath::CreateDirectoriesRecursively() const
{
#if IUTEST_USE_CXX_FILESYSTEM
    return ::std::filesystem::create_directories(m_path);
#else
    if( !IsDirectory() )
    {
        return false;
    }

    if( length() == 0 || DirectoryExists() )
    {
        return true;
    }

    const iuFilePath parent = RemoveTrailingPathSeparator().RemoveFileName();
    if( !parent.CreateDirectoriesRecursively() )
    {
        return false;
    }
    return CreateFolder();
#endif
}

IUTEST_IPP_INLINE bool iuFilePath::FileOrDirectoryExists() const
{
#if IUTEST_USE_CXX_FILESYSTEM
    return ::std::filesystem::exists(m_path);
#else
#if IUTEST_HAS_FILE_STAT
    posix::StatStruct file_stat;
    return posix::Stat(m_path.c_str(), &file_stat) == 0;
#else
    return false;
#endif
#endif
}

IUTEST_IPP_INLINE bool iuFilePath::DirectoryExists() const
{
#if IUTEST_USE_CXX_FILESYSTEM
    return ::std::filesystem::is_directory(::std::filesystem::status(m_path));
#else
#if IUTEST_HAS_FILE_STAT

#ifdef IUTEST_OS_WINDOWS
    const iuFilePath& path = IsRootDirectory() ? *this : RemoveTrailingPathSeparator();
#else
    const iuFilePath& path = *this;
#endif
    posix::StatStruct file_stat;
    if( posix::Stat(path.m_path.c_str(), &file_stat) == 0 )
    {
        return posix::IsDir(file_stat);
    }

#endif
    return false;
#endif
}

IUTEST_IPP_INLINE iuFilePath iuFilePath::GetCurrentDir()
{
    return iuFilePath(internal::posix::GetCWD());
}

IUTEST_IPP_INLINE iuFilePath iuFilePath::GetRelativeCurrentDir()
{
    ::std::string dir(".");
    dir += GetPathSeparator();
    return iuFilePath(dir);
}

IUTEST_IPP_INLINE iuFilePath iuFilePath::GetExecFilePath()
{
#if   defined(IUTEST_OS_WINDOWS)
    char path[IUTEST_MAX_PATH];
    ::GetModuleFileNameA(NULL, path, sizeof(path));
    return iuFilePath(path);
#elif defined(IUTEST_OS_SOLARIS)
    return iuFilePath(getexecname());
#elif defined(__FreeBSD__)
    int exe_path_mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, getpid() };
    char buf[1024];
    size_t length = 0;
    if( sysctl(exe_path_mib, 4, buf, &length, NULL, 0) != 0 )
    {
        return iuFilePath();
    }
    return iuFilePath(buf);
#elif defined(IUTEST_OS_LINUX) || defined(IUTEST_OS_CYGWIN)
#if (defined(__BSD_VISIBLE) && __BSD_VISIBLE) \
    || (defined(__POSIX_VISIBLE) && __POSIX_VISIBLE >= 200112) \
    || (defined(__XSI_VISIBLE) && __XSI_VISIBLE >= 4)
    char buf[1024];
    const ssize_t len = ::readlink("/proc/self/exe", buf, sizeof(buf)-1);
    if( len == -1 )
    {
        return iuFilePath();
    }
    buf[len] = '\0';
    return iuFilePath(buf);
#else
    return iuFilePath();
#endif
#else
    return iuFilePath();
#endif
}

IUTEST_IPP_INLINE iuFilePath iuFilePath::ConcatPaths(const iuFilePath& directory, const iuFilePath& relative_path)
{
#if IUTEST_USE_CXX_FILESYSTEM
    return iuFilePath(directory.m_path / relative_path.m_path);
#else
    ::std::string path = directory.RemoveTrailingPathSeparator().m_path;
    path += GetPathSeparator();
    path += relative_path.m_path;
    return iuFilePath(path);
#endif
}

IUTEST_IPP_INLINE void iuFilePath::Normalize()
{
#if IUTEST_USE_CXX_FILESYSTEM
#else
    const char* src = m_path.c_str();
    char* const dst_top = new char [length()+1];
    char* dst = dst_top;

    while(*src != '\0')
    {
        *dst = *src;
        if( !IsPathSeparator(*src) )
        {
            ++src;
        }
        else
        {
            if( IsAltPathSeparator(*src) )
            {
                *dst = GetPathSeparator();
            }
            ++src;
            while( IsPathSeparator(*src) )
            {
                ++src;
            }
        }
        ++dst;
    }
    *dst = '\0';
    m_path = dst_top;
    delete [] dst_top;
#endif
}

}   // end of namespace detail
}   // end of namespace iutest

#endif // INCG_IRIS_IUTEST_FILEPATH_IPP_D69E7545_BF8A_4EDC_9493_9105C69F9378_
<commit_msg>workaround<commit_after>﻿//======================================================================
//-----------------------------------------------------------------------
/**
 * @file        iutest_filepath.ipp
 * @brief       iris unit test ファイルパスクラス ファイル
 *
 * @author      t.shirayanagi
 * @par         copyright
 * Copyright (C) 2012-2018, <NAME>\n
 * This software is released under the new BSD License,
 * see LICENSE
*/
//-----------------------------------------------------------------------
//======================================================================
#ifndef INCG_IRIS_IUTEST_FILEPATH_IPP_D69E7545_BF8A_4EDC_9493_9105C69F9378_
#define INCG_IRIS_IUTEST_FILEPATH_IPP_D69E7545_BF8A_4EDC_9493_9105C69F9378_

//======================================================================
// include
#include "../internal/iutest_filepath.hpp"

#if defined(IUTEST_OS_WINDOWS) && !defined(IUTEST_OS_WINDOWS_MOBILE)
#  include <direct.h>
#endif

namespace iutest {
namespace detail
{

IUTEST_IPP_INLINE bool iuFilePath::IsDirectory() const
{
#if IUTEST_USE_CXX_FILESYSTEM
    return ::std::filesystem::is_directory(m_path);
#else
    if( m_path.empty() )
    {
        return false;
    }
    const char last = *m_path.rbegin();
    return (IsPathSeparator(last) || last == '.');
#endif
}

IUTEST_IPP_INLINE bool iuFilePath::IsRootDirectory() const
{
#ifdef IUTEST_OS_WINDOWS
    if( length() != 3 )
    {
        return false;
    }
#else
    if( length() != 3 )
    {
        return false;
    }
#endif
    return IsAbsolutePath();
}

IUTEST_IPP_INLINE bool iuFilePath::IsAbsolutePath() const
{
#if IUTEST_USE_CXX_FILESYSTEM
    return m_path.is_absolute();
#else
#ifdef IUTEST_OS_WINDOWS
    if( length() < 3 )
    {
        return false;
    }
    const char* name = m_path.c_str();

    if( IsDBCSLeadByte(name[0]) )
    {
        ++name;
    }
    return (name[1] == ':' && IsPathSeparator(name[0]));
#else
    return IsPathSeparator(*m_path.begin());
#endif
#endif
}

IUTEST_IPP_INLINE iuFilePath iuFilePath::RemoveTrailingPathSeparator() const
{
#if IUTEST_USE_CXX_FILESYSTEM
    const ::std::string path = m_path.generic_string();
#else
    const ::std::string& path = m_path;
#endif
    ::std::string::const_iterator it = path.end()-1;
    while(it != path.begin() && IsPathSeparator(*it))
    {
        --it;
    }
    return iuFilePath(::std::string(path.begin(), it+1));
}

IUTEST_IPP_INLINE ::std::string iuFilePath::GetExtension() const
{
#if IUTEST_USE_CXX_FILESYSTEM
    return m_path.extension().generic_string();
#else
    const char* const path = m_path.c_str();
    const char* const ext = strrchr(path, '.');
    if(ext == NULL)
    {
        return "";
    }
    return ext;
#endif
}

IUTEST_IPP_INLINE iuFilePath iuFilePath::RemoveExtension(const char* extension) const
{
#if IUTEST_USE_CXX_FILESYSTEM
    const ::std::string path_string = m_path.generic_string();
#else
    const ::std::string& path_string = m_path;
#endif
    const char* const path = path_string.c_str();
    const char* const ext = strrchr(path, '.');
    if( ext == NULL )
    {
        return *this;
    }
    if( extension != NULL && !IsStringCaseEqual(ext + 1, extension) )
    {
        return *this;
    }
    const size_t length = ext - path;
    return iuFilePath(::std::string(path, length));
}

IUTEST_IPP_INLINE iuFilePath iuFilePath::RemoveDirectoryName() const
{
#if IUTEST_USE_CXX_FILESYSTEM
    return iuFilePath(m_path.parent_path());
#else
    const char* const sep = detail::FindLastPathSeparator(m_path.c_str(), m_path.length());
    return sep != NULL ? iuFilePath(sep+1) : *this;
#endif
}

IUTEST_IPP_INLINE iuFilePath iuFilePath::RemoveFileName() const
{
#if IUTEST_USE_CXX_FILESYSTEM
    if(m_path.has_filename()) {
        return iuFilePath(m_path.parent_path());
    }
    return iuFilePath(m_path);
#else
    const char* path = m_path.c_str();
    size_t pos = detail::FindLastPathSeparatorPosition(path, m_path.length());
    if( pos == ::std::string::npos )
    {
        return GetRelativeCurrentDir();
    }
    const size_t length = pos + 1;
    return iuFilePath(::std::string(path, length));
#endif
}

IUTEST_IPP_INLINE bool iuFilePath::CreateFolder() const
{
#if IUTEST_USE_CXX_FILESYSTEM
    return ::std::filesystem::create_directory(m_path);
#else
#if IUTEST_HAS_FILE_STAT

#if   defined(IUTEST_OS_WINDOWS_MOBILE)
#elif defined(IUTEST_OS_WINDOWS) && !defined(IUTEST_OS_WINDOWS_WINE)
    if( _mkdir(m_path.c_str()) == 0 )
    {
        return true;
    }
#else
    if( mkdir(m_path.c_str(), 0777) == 0 )
    {
        return true;
    }
#endif

#endif
    return DirectoryExists();
#endif
}

IUTEST_IPP_INLINE bool iuFilePath::CreateDirectoriesRecursively() const
{
#if IUTEST_USE_CXX_FILESYSTEM
    return ::std::filesystem::create_directories(m_path);
#else
    if( !IsDirectory() )
    {
        return false;
    }

    if( length() == 0 || DirectoryExists() )
    {
        return true;
    }

    const iuFilePath parent = RemoveTrailingPathSeparator().RemoveFileName();
    if( !parent.CreateDirectoriesRecursively() )
    {
        return false;
    }
    return CreateFolder();
#endif
}

IUTEST_IPP_INLINE bool iuFilePath::FileOrDirectoryExists() const
{
#if IUTEST_USE_CXX_FILESYSTEM
    return ::std::filesystem::exists(m_path);
#else
#if IUTEST_HAS_FILE_STAT
    posix::StatStruct file_stat;
    return posix::Stat(m_path.c_str(), &file_stat) == 0;
#else
    return false;
#endif
#endif
}

IUTEST_IPP_INLINE bool iuFilePath::DirectoryExists() const
{
#if IUTEST_USE_CXX_FILESYSTEM
    return ::std::filesystem::is_directory(::std::filesystem::status(m_path));
#else
#if IUTEST_HAS_FILE_STAT

#ifdef IUTEST_OS_WINDOWS
    const iuFilePath& path = IsRootDirectory() ? *this : RemoveTrailingPathSeparator();
#else
    const iuFilePath& path = *this;
#endif
    posix::StatStruct file_stat;
    if( posix::Stat(path.m_path.c_str(), &file_stat) == 0 )
    {
        return posix::IsDir(file_stat);
    }

#endif
    return false;
#endif
}

IUTEST_IPP_INLINE iuFilePath iuFilePath::GetCurrentDir()
{
    return iuFilePath(internal::posix::GetCWD());
}

IUTEST_IPP_INLINE iuFilePath iuFilePath::GetRelativeCurrentDir()
{
    ::std::string dir(".");
    dir += GetPathSeparator();
    return iuFilePath(dir);
}

IUTEST_IPP_INLINE iuFilePath iuFilePath::GetExecFilePath()
{
#if   defined(IUTEST_OS_WINDOWS)
    char path[IUTEST_MAX_PATH];
    ::GetModuleFileNameA(NULL, path, sizeof(path));
    return iuFilePath(path);
#elif defined(IUTEST_OS_SOLARIS)
    return iuFilePath(getexecname());
#elif defined(__FreeBSD__)
    int exe_path_mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, getpid() };
    char buf[1024];
    size_t length = 0;
    if( sysctl(exe_path_mib, 4, buf, &length, NULL, 0) != 0 )
    {
        return iuFilePath();
    }
    return iuFilePath(buf);
#elif defined(IUTEST_OS_LINUX) || defined(IUTEST_OS_CYGWIN)
#if (defined(__BSD_VISIBLE) && __BSD_VISIBLE) \
    || (defined(__POSIX_VISIBLE) && __POSIX_VISIBLE >= 200112) \
    || (defined(__XSI_VISIBLE) && __XSI_VISIBLE >= 4)
    char buf[1024];
    const ssize_t len = ::readlink("/proc/self/exe", buf, sizeof(buf)-1);
    if( len == -1 )
    {
        return iuFilePath();
    }
    buf[len] = '\0';
    return iuFilePath(buf);
#else
    return iuFilePath();
#endif
#else
    return iuFilePath();
#endif
}

IUTEST_IPP_INLINE iuFilePath iuFilePath::ConcatPaths(const iuFilePath& directory, const iuFilePath& relative_path)
{
#if IUTEST_USE_CXX_FILESYSTEM
    return iuFilePath(directory.m_path / relative_path.m_path);
#else
    ::std::string path = directory.RemoveTrailingPathSeparator().m_path;
    path += GetPathSeparator();
    path += relative_path.m_path;
    return iuFilePath(path);
#endif
}

IUTEST_IPP_INLINE void iuFilePath::Normalize()
{
#if IUTEST_USE_CXX_FILESYSTEM
#else
    const char* src = m_path.c_str();
    char* const dst_top = new char [length()+1];
    char* dst = dst_top;

    while(*src != '\0')
    {
        *dst = *src;
        if( !IsPathSeparator(*src) )
        {
            ++src;
        }
        else
        {
            if( IsAltPathSeparator(*src) )
            {
                *dst = GetPathSeparator();
            }
            ++src;
            while( IsPathSeparator(*src) )
            {
                ++src;
            }
        }
        ++dst;
    }
    *dst = '\0';
    m_path = dst_top;
    delete [] dst_top;
#endif
}

}   // end of namespace detail
}   // end of namespace iutest

#endif // INCG_IRIS_IUTEST_FILEPATH_IPP_D69E7545_BF8A_4EDC_9493_9105C69F9378_

{'fraction_non_alphanumeric': 0.16607669616519175, 'fraction_numerical': 0.01415929203539823, 'mean_word_length': 3.3688144329896907, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6865672', 'n_tokens_mistral': 6171, 'n_tokens_neox': 5970, 'n_words': 1114}