📄 path.hpp
字号:
WINSTL_ASSERT('\\' == m_buffer[0]);
WINSTL_ASSERT('\\' == m_buffer[1]);
WINSTL_ASSERT('\\' != m_buffer[2]);
char_type const* slash0 = next_slash_or_end(&m_buffer[3]);
char_type const* slash1 = next_slash_or_end(slash0);
for(ws_size_t i = 0, n = static_cast<ws_size_t>(slash1 - &m_buffer[0]); i < n; ++i)
{
*dest++ = *p1++;
}
}
else if(this->is_absolute())
{
// Copy over the drive letter, colon and slash
*dest++ = *p1++;
*dest++ = *p1++;
*dest++ = *p1++;
}
else if(this->is_rooted())
{
*dest++ = traits_type::path_name_separator();
++p1;
}
// 1. Parse the path into an uncanonicalised sequence of directory parts
{
size_type i = 0;
for(; '\0' != *p1; ++i)
{
p2 = next_slash_or_end(p1);
parts[i].len = static_cast<size_type>(p2 - p1);
parts[i].p = p1;
parts[i].type = part_type::normal;
switch(parts[i].len)
{
case 1:
if('.' == p1[0])
{
parts[i].type = part_type::dot;
}
break;
case 2:
if('.' == p1[0])
{
if('.' == p1[1])
{
parts[i].type = part_type::dotdot;
}
else if(traits_type::path_name_separator() == p1[1])
{
parts[i].type = part_type::dot;
}
else if(path_name_separator_alt() == p1[1])
{
parts[i].type = part_type::dot;
}
}
break;
case 3:
if( '.' == p1[0] &&
'.' == p1[1])
{
if(traits_type::path_name_separator() == p1[2])
{
parts[i].type = part_type::dotdot;
}
else if(path_name_separator_alt() == p1[2])
{
parts[i].type = part_type::dotdot;
}
}
break;
default:
break;
}
p1 = p2;
}
parts.resize(i);
}
// 2.a Remove all '.' parts
{ for(size_type i = 0; i < parts.size(); ++i)
{
WINSTL_ASSERT(0 != parts[i].len);
part_type& part = parts[i];
if(part_type::dot == part.type)
{
part.len = 0;
}
}}
coallesce_parts_(parts);
// 2.b Process the '..' parts
{ for(size_type i = 1; i < parts.size(); ++i)
{
WINSTL_ASSERT(0 != parts[i].len);
part_type& part = parts[i];
if(part_type::dotdot == part.type)
{
{ for(size_type prior = i; ; )
{
if(0 == prior)
{
break;
}
else
{
--prior;
if(0 != parts[prior].len)
{
if(part_type::normal == parts[prior].type)
{
part.len = 0;
parts[prior].len = 0;
break;
}
}
}
}}
}
}}
coallesce_parts_(parts);
// 2.c "insert" a '.' if we've removed everything.
if(parts.empty())
{
static const char_type s_dot[] = { '.', '/' };
parts.resize(1);
parts[0].type = part_type::dot;
parts[0].len = 1;
parts[0].p = s_dot;
}
// 3. Write out all the parts back into the new path instance
{
#ifdef _DEBUG
::memset(dest, '~', newPath.m_buffer.size() - (dest - &newPath.m_buffer[0]));
#endif /* _DEBUG */
for(size_type i = 0; i < parts.size(); ++i)
{
traits_type::str_n_copy(dest, parts[i].p, parts[i].len);
dest += parts[i].len;
}
*dest = '\0';
newPath.m_len = static_cast<size_type>(dest - newPath.c_str());
}
// Now we determine whether to leave a trailing separator or
// not and, if so, what type it should be.
WINSTL_ASSERT(m_len > 0);
char_type last = m_buffer[m_len - 1];
if( !bRemoveTrailingPathNameSeparator &&
traits_type::is_path_name_separator(last))
{
newPath.push_sep_(last);
}
else
{
newPath.pop_sep();
}
swap(newPath);
return *this;
}
template< ss_typename_param_k C
, ss_typename_param_k T
, ss_typename_param_k A
>
inline ss_typename_type_k basic_path<C, T, A>::char_type const* basic_path<C, T, A>::get_file() const
{
char_type const* slash = traits_type::str_rchr(c_str_ptr(m_buffer), traits_type::path_name_separator());
char_type const* slash_a = traits_type::str_rchr(c_str_ptr(m_buffer), path_name_separator_alt());
if(slash_a > slash)
{
slash = slash_a;
}
if(NULL == slash)
{
slash = stlsoft_ns_qual(c_str_ptr)(m_buffer);
}
else
{
++slash;
}
return slash;
}
template< ss_typename_param_k C
, ss_typename_param_k T
, ss_typename_param_k A
>
inline ss_typename_type_k basic_path<C, T, A>::char_type const* basic_path<C, T, A>::get_ext() const
{
char_type const *dot = traits_type::str_rchr(this->c_str(), '.');
char_type const *file = get_file();
static const char_type s_empty[1] = { '\0' };
if(NULL == dot)
{
return s_empty;
}
else if(dot < file)
{
return s_empty;
}
else
{
return dot + 1;
}
}
template< ss_typename_param_k C
, ss_typename_param_k T
, ss_typename_param_k A
>
inline ss_typename_type_k basic_path<C, T, A>::size_type basic_path<C, T, A>::length() const
{
return m_len;
}
template< ss_typename_param_k C
, ss_typename_param_k T
, ss_typename_param_k A
>
inline ss_typename_type_k basic_path<C, T, A>::size_type basic_path<C, T, A>::size() const
{
return length();
}
template< ss_typename_param_k C
, ss_typename_param_k T
, ss_typename_param_k A
>
inline ss_typename_type_k basic_path<C, T, A>::bool_type basic_path<C, T, A>::empty() const
{
return 0 == size();
}
template< ss_typename_param_k C
, ss_typename_param_k T
, ss_typename_param_k A
>
inline ss_typename_type_k basic_path<C, T, A>::char_type const* basic_path<C, T, A>::c_str() const
{
return stlsoft_ns_qual(c_str_ptr)(m_buffer);
}
template< ss_typename_param_k C
, ss_typename_param_k T
, ss_typename_param_k A
>
inline ss_typename_type_k basic_path<C, T, A>::char_type const& basic_path<C, T, A>::operator [](ss_typename_type_k basic_path<C, T, A>::size_type index) const
{
WINSTL_MESSAGE_ASSERT("Index out of range", !(size() < index));
return c_str()[index];
}
template< ss_typename_param_k C
, ss_typename_param_k T
, ss_typename_param_k A
>
inline ws_bool_t basic_path<C, T, A>::exists() const
{
return traits_type::file_exists(this->c_str());
}
template< ss_typename_param_k C
, ss_typename_param_k T
, ss_typename_param_k A
>
inline ws_bool_t basic_path<C, T, A>::is_rooted() const
{
return traits_type::is_path_rooted(this->c_str());
}
template< ss_typename_param_k C
, ss_typename_param_k T
, ss_typename_param_k A
>
inline ws_bool_t basic_path<C, T, A>::is_absolute() const
{
return traits_type::is_path_absolute(this->c_str());
}
template< ss_typename_param_k C
, ss_typename_param_k T
, ss_typename_param_k A
>
inline ws_bool_t basic_path<C, T, A>::has_sep() const
{
return this->empty() ? false : traits_type::has_dir_end(this->c_str() + (this->size() - 1));
}
template< ss_typename_param_k C
, ss_typename_param_k T
, ss_typename_param_k A
>
inline ss_typename_type_k basic_path<C, T, A>::size_type basic_path<C, T, A>::copy(ss_typename_type_k basic_path<C, T, A>::char_type *buffer, ss_typename_type_k basic_path<C, T, A>::size_type cchBuffer) const
{
return stlsoft_ns_qual(copy_contents)(buffer, cchBuffer, m_buffer.data(), m_len);
}
template< ss_typename_param_k C
, ss_typename_param_k T
, ss_typename_param_k A
>
inline ws_bool_t basic_path<C, T, A>::equivalent(basic_path<C, T, A> const& rhs) const
{
return equivalent(rhs.c_str());
}
template< ss_typename_param_k C
, ss_typename_param_k T
, ss_typename_param_k A
>
inline ws_bool_t basic_path<C, T, A>::equivalent(ss_typename_type_k basic_path<C, T, A>::char_type const* rhs) const
{
class_type lhs_(*this);
class_type rhs_(rhs);
return lhs_.make_absolute(false).canonicalise(true) == rhs_.make_absolute(false).canonicalise(true);
}
template< ss_typename_param_k C
, ss_typename_param_k T
, ss_typename_param_k A
>
inline ws_bool_t basic_path<C, T, A>::equal(basic_path<C, T, A> const& rhs) const
{
return equal(rhs.c_str());
}
template< ss_typename_param_k C
, ss_typename_param_k T
, ss_typename_param_k A
>
inline ws_bool_t basic_path<C, T, A>::equal(ss_typename_type_k basic_path<C, T, A>::char_type const* rhs) const
{
return 0 == traits_type::str_compare_no_case(stlsoft_ns_qual(c_str_ptr)(m_buffer), stlsoft_ns_qual(c_str_ptr)(rhs));
}
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* ////////////////////////////////////////////////////////////////////// */
#ifndef _WINSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
} // namespace winstl
# else
} // namespace winstl_project
} // namespace stlsoft
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_WINSTL_NO_NAMESPACE */
/* In the special case of Intel behaving as VC++ 7.0 or earlier on Win32, we
* illegally insert into the std namespace.
*/
#if defined(STLSOFT_CF_std_NAMESPACE)
# if ( ( defined(STLSOFT_COMPILER_IS_INTEL) && \
defined(_MSC_VER))) && \
_MSC_VER < 1310
namespace std
{
template< ss_typename_param_k C
, ss_typename_param_k T
, ss_typename_param_k A
>
inline void swap(winstl_ns_qual(basic_path)<C, T, A>& lhs, winstl_ns_qual(basic_path)<C, T, A>& rhs)
{
lhs.swap(rhs);
}
} // namespace std
# endif /* INTEL && _MSC_VER < 1310 */
#endif /* STLSOFT_CF_std_NAMESPACE */
/* /////////////////////////////////////////////////////////////////////////
* Namespace
*
* The string access shims exist either in the stlsoft namespace, or in the
* global namespace. This is required by the lookup rules.
*
*/
#ifndef _WINSTL_NO_NAMESPACE
# if !defined(_STLSOFT_NO_NAMESPACE) && \
!defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
namespace stlsoft
{
# else /* ? _STLSOFT_NO_NAMESPACE */
/* There is no stlsoft namespace, so must define in the global namespace */
# endif /* !_STLSOFT_NO_NAMESPACE */
using ::winstl::c_str_data;
using ::winstl::c_str_data_a;
using ::winstl::c_str_data_w;
using ::winstl::c_str_len;
using ::winstl::c_str_len_a;
using ::winstl::c_str_len_w;
using ::winstl::c_str_ptr;
using ::winstl::c_str_ptr_a;
using ::winstl::c_str_ptr_w;
using ::winstl::c_str_ptr_null;
using ::winstl::c_str_ptr_null_a;
using ::winstl::c_str_ptr_null_w;
# if !defined(_STLSOFT_NO_NAMESPACE) && \
!defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
} // namespace stlsoft
# else /* ? _STLSOFT_NO_NAMESPACE */
/* There is no stlsoft namespace, so must define in the global namespace */
# endif /* !_STLSOFT_NO_NAMESPACE */
#endif /* !_WINSTL_NO_NAMESPACE */
/* ////////////////////////////////////////////////////////////////////// */
#endif /* WINSTL_INCL_WINSTL_FILESYSTEM_HPP_PATH */
/* ////////////////////////////////////////////////////////////////////// */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -