1 #include "stdafx.h"
2 #include "FilePathHelper.h"
3 #include "Common.h"
4
5
CFilePathHelper(const wstring & file_path)6 CFilePathHelper::CFilePathHelper(const wstring & file_path)
7 : m_file_path{ file_path }
8 {
9 }
10
~CFilePathHelper()11 CFilePathHelper::~CFilePathHelper()
12 {
13 }
14
GetFileExtension(bool upper,bool width_dot) const15 wstring CFilePathHelper::GetFileExtension(bool upper, bool width_dot) const
16 {
17 wstring file_name = GetFileName();
18 size_t index;
19 index = file_name.rfind('.');
20 if (index == wstring::npos || index == file_name.size() - 1)
21 return wstring();
22 wstring file_extension{ file_name.substr(width_dot ? index : index + 1) };
23 CCommon::StringTransform(file_extension, upper);
24 return file_extension;
25 }
26
GetFileName() const27 wstring CFilePathHelper::GetFileName() const
28 {
29 size_t index;
30 index = m_file_path.rfind('\\');
31 if (index == wstring::npos)
32 index = m_file_path.rfind('/');
33 return m_file_path.substr(index + 1);
34 }
35
GetFileNameWithoutExtension() const36 wstring CFilePathHelper::GetFileNameWithoutExtension() const
37 {
38 size_t index, index1;
39 index = m_file_path.rfind('.');
40 index1 = m_file_path.rfind('\\');
41 if (index1 == wstring::npos)
42 index1 = m_file_path.rfind('/');
43 return m_file_path.substr(index1 + 1, (index - index1 - 1));
44 }
45
GetFolderName() const46 wstring CFilePathHelper::GetFolderName() const
47 {
48 int index, index1;
49 index = m_file_path.find_last_of(L"\\/");
50 if (index == wstring::npos || index == 0)
51 return wstring();
52
53 index1 = m_file_path.find_last_of(L"\\/", index - 1);
54 if (index1 == wstring::npos || index1 == 0)
55 return wstring();
56
57 return m_file_path.substr(index1 + 1, (index - index1 - 1));
58 }
59
GetDir() const60 wstring CFilePathHelper::GetDir() const
61 {
62 if (!m_file_path.empty() && (m_file_path.back() == L'\\' || m_file_path.back() == L'/'))
63 return m_file_path;
64 size_t index;
65 index = m_file_path.rfind('\\');
66 if (index == wstring::npos)
67 index = m_file_path.rfind('/');
68 return m_file_path.substr(0, index + 1);
69 }
70
GetParentDir() const71 wstring CFilePathHelper::GetParentDir() const
72 {
73 wstring dir{ GetDir() };
74 size_t index;
75 if (!dir.empty() && (dir.back() == L'\\' || dir.back() == L'/'))
76 dir.pop_back();
77 index = dir.rfind('\\');
78 if (index == wstring::npos)
79 index = dir.rfind('/');
80 return m_file_path.substr(0, index + 1);
81 }
82
ReplaceFileExtension(const wchar_t * new_extension)83 const wstring& CFilePathHelper::ReplaceFileExtension(const wchar_t * new_extension)
84 {
85 size_t index, index1;
86 index = m_file_path.rfind('.');
87 index1 = m_file_path.rfind('\\');
88 if (index == wstring::npos || (index1 != wstring::npos && index < index1)) //如果没有找到“.”,或者“.”在反斜杠的左边,则在末尾添加一个“.”
89 {
90 m_file_path.push_back(L'.');
91 }
92 else if (index != m_file_path.size() - 1) //如果“.”不在最后的位置,则删除“.”后面的字符串
93 {
94 m_file_path.erase(index + 1);
95 }
96 if (new_extension == nullptr || *new_extension == L'\0')
97 {
98 if (!m_file_path.empty() && m_file_path.back() == L'.')
99 m_file_path.pop_back();
100 }
101 else
102 {
103 m_file_path.append(new_extension); //在末尾添加扩展名
104 }
105 return m_file_path;
106 }
107
GetFilePathWithoutExtension() const108 wstring CFilePathHelper::GetFilePathWithoutExtension() const
109 {
110 size_t index;
111 index = m_file_path.rfind('.');
112 return m_file_path.substr(0, index);
113 }
114