CFugue
PathHelper.h
1 #ifndef __PATH_HELPER_H__516BEAA4_9BA9_47f8_A9B1_DF4AC1B98D7A
2 #define __PATH_HELPER_H__516BEAA4_9BA9_47f8_A9B1_DF4AC1B98D7A
3 
4 #include <AtlStr.h>
5 
6 inline bool PathIsDots(const CString& strFilePath)
7 {
8  return strFilePath == _T(".") || strFilePath == _T("..");
9 }
10 
11 inline CString& PathAddBackSlash(CString& strFilePath)
12 {
13  if(strFilePath.Right(1) != _T('\\'))
14  strFilePath += _T("\\");
15  return strFilePath;
16 }
17 
18 inline CString PathGetFileName(const CString& strFilePath)
19 {
20  int nIndex = strFilePath.ReverseFind(_T('\\'));
21  if(nIndex == -1)
22  return strFilePath;
23  return strFilePath.Mid(nIndex+1);
24 }
25 
26 inline CString PathGetFileExtension(const CString& strFilePath)
27 {
28  int nIndex = strFilePath.ReverseFind(_T('.'));
29  if(nIndex == -1)
30  return _T("");
31  return strFilePath.Mid(nIndex+1);
32 }
33 
34 inline CString PathGetFileTitle(const CString& strFilePath)
35 {
36  CString strFileName(PathGetFileName(strFilePath));
37  if(false == strFileName.IsEmpty())
38  {
39  int nIndex = strFileName.ReverseFind(_T('.'));
40  if(nIndex == -1)
41  return strFileName;
42  return strFileName.Mid(0, nIndex);
43  }
44  else
45  return strFileName;
46 }
47 
48 inline void PathGetFileTitleAndExtension(const CString& strFilePath, CString& strFileTitle, CString& strFileExtension)
49 {
50  strFileTitle = _T("");
51  strFileExtension = _T("");
52 
53  CString strFileName(PathGetFileName(strFilePath));
54 
55  if(false == strFileName.IsEmpty())
56  {
57  int nIndex = strFileName.ReverseFind(_T('.'));
58  if(nIndex != -1)
59  {
60  strFileTitle = strFileName.Mid(0, nIndex);
61  strFileExtension = strFileName.Mid(nIndex+1);
62  }
63  else
64  strFileTitle = strFileName;
65  }
66 }
67 
68 inline CString PathGetFolder(const CString& strFilePath) // If strFilePath points to a dir then it should have a \ in the end;
69 {
70  int nIndex = strFilePath.ReverseFind(_T('\\'));
71  if(nIndex == -1)
72  return _T(".\\");
73  return strFilePath.Mid(0, nIndex+1); // We Have to include terminating \\ also
74 }
75 
76 inline void PathGetFolderAndFileName(const CString& strFilePath, CString& strFolder, CString& strFileName)
77 {
78  int nIndex = strFilePath.ReverseFind(_T('\\'));
79  if(nIndex == -1)
80  {
81  strFileName = strFilePath;
82  strFolder = _T(".\\");
83  return;
84  }
85  strFolder = strFilePath.Mid(0, nIndex+1); // We Have to include terminating \\ also
86  strFileName = strFilePath.Mid(nIndex+1);
87 }
88 
89 //
90 // PathGetRootDir: Returns the First Dir on the path;
91 // if strItemPath is a filename or empty, then returns empty string;
92 // e.g. c:\ returns "c:\\" ; c:\text\one\ returns "c:\\" , one.txt returns ""
93 //
94 inline CString PathGetRootDir(const CString& strItemPath)
95 {
96  int nIndex = strItemPath.Find(_T('\\'));
97  if(nIndex == -1) return _T("");
98  return strItemPath.Mid(0, nIndex+1); // We Have to include terminating \\ also
99 }
100 
101 //
102 // Replicates the non-strSourceRoot portion of strTargetItemPath at strDestination Root;
103 // strTargetItemPath could be a File Path rooted at strSourceRoot;
104 //
105 bool CreateDirectoryStructure(CString& strDestinationRoot, const CString& strSourceRoot, const CString& strTargetItemPath)
106 {
107  CString strRelativeTargetDir;
108 
109  if(strTargetItemPath.Find(strSourceRoot) != 0)
110  strRelativeTargetDir = strTargetItemPath;
111  else
112  strRelativeTargetDir = strTargetItemPath.Mid(strSourceRoot.GetLength());
113 
114  CString FirstDir = PathGetRootDir(strRelativeTargetDir);
115  while(FirstDir.IsEmpty() == false)
116  {
117  strDestinationRoot += FirstDir;
118  if(PathFileExists(strDestinationRoot) == false)
119  if(!CreateDirectory(strDestinationRoot, NULL))
120  return false;
121  strRelativeTargetDir = strRelativeTargetDir.Mid(FirstDir.GetLength());
122  FirstDir = PathGetRootDir(strRelativeTargetDir);;
123  }
124 
125  return true;
126 }
127 
128 #endif

CFugue, the C++ Music Programming Library © Copyright 2009 Cenacle Research India Private Limited Gopalakrishna Palem