Add Dirk Luetjen's ssphys libraries and command-line tool
[vss2svn.git] / ssphys / SSPhysLib / FileName.h
blob52396e7ad63667723086080741701b73b259994e
1 // FileName.h: interface for the CFileName class.
2 //
3 //////////////////////////////////////////////////////////////////////
5 #if !defined(AFX_FILENAME_H__5778DD0A_6746_4301_A8C4_B82F7CC70FC9__INCLUDED_)
6 #define AFX_FILENAME_H__5778DD0A_6746_4301_A8C4_B82F7CC70FC9__INCLUDED_
8 #if _MSC_VER > 1000
9 #pragma once
10 #endif // _MSC_VER > 1000
12 // Siehe auch CFileSpec auf http://www.codeproject.com/cpp/cfilespec.asp
13 // oder CPath auf http://www.thecodeproject.com/file/cpath.asp
15 #include <windows.h>
16 #include <tchar.h>
18 class CFileName
20 public:
21 CFileName (LPCTSTR pFilename = NULL)
23 m_szDrive[0] = m_szDir[0] = m_szFileTitle[0] = m_szExt[0] = _T('\0');
25 if (pFilename)
26 SetFilePath (pFilename);
29 // CFileName (HMODULE hModule)
30 // {
31 // TCHAR szPathName[_MAX_PATH];
32 // if (::GetModuleFileName(hModule, szPathName, _MAX_PATH))
33 // SetFilePath (szPathName);
34 // else
35 // {
36 // DWORD dwError = GetLastError();
37 // TRACE (_T("CFileName(HMODULE) failed: GetLastError returned %d\n"), dwError);
38 // }
39 //
40 // }
42 // set the full path name including filename and extension (e.g.: "c:\WinNT\system32\kernel32.sys")
43 void SetFilePath (LPCTSTR pFilepath)
45 assert(pFilepath != NULL);
46 // assert(AfxIsValidString(pFilepath));
48 _tsplitpath (pFilepath, m_szDrive, m_szDir, m_szFileTitle, m_szExt);
50 // return full path name including filename and extension (e.g.: "c:\WinNT\system32\kernel32.sys")
51 std::string GetFilePath () const
53 TCHAR str[_MAX_PATH];
54 _tmakepath (str, m_szDrive, m_szDir, m_szFileTitle, m_szExt);
55 return str;
58 // set the drive name (e.g.: "c:")
59 void SetDrive (LPCTSTR pStr)
61 assert(pStr != NULL);
62 // assert(AfxIsValidString(pStr));
64 _tcsncpy (m_szDrive, pStr, _MAX_DRIVE);
66 // // return the drive name (e.g.: "c:")
67 // std::string GetDrive () const
68 // {
69 // CString str;
70 // str.Format(_T("%.*s"), _MAX_DRIVE, m_szDrive);
71 // return str;
72 // }
74 // set the directory, _including_ the trailing slash (e.g.: "WinNT\system32\")
75 void SetDir (LPCTSTR pStr)
77 assert (IsTrailingSlash (pStr) || _tcslen (pStr) == 0);
78 _tcsncpy (m_szDir, pStr, _MAX_DIR);
80 // // return the directory, including a trailing slash (e.g.: "WinNT\system32\")
81 // CString GetDir () const
82 // {
83 // CString str;
84 // str.Format(_T("%.*s"), _MAX_DIR, m_szDir);
85 // return str;
86 // }
88 // set the file title (e.g.: "kernel32")
89 void SetFileTitle (LPCTSTR pStr)
91 assert(pStr != NULL);
92 // assert(AfxIsValidString(pStr));
94 _tcsncpy (m_szFileTitle, pStr, _MAX_FNAME);
96 // // return the file title (e.g.: "kernel32")
97 // CString GetFileTitle () const
98 // {
99 // CString str;
100 // str.Format(_T("%.*s"), _MAX_FNAME, m_szFileTitle);
101 // return str;
102 // }
104 // set the extension, with or without the leading dot (e.g.: "sys" or ".sys")
105 void SetExt (LPCTSTR str)
107 if (IsLeadingDot (str))
108 _tcsncpy (m_szExt, str, _MAX_EXT);
109 else
111 TCHAR* lpsz = m_szExt;
112 _tcsncpy (lpsz, _T("."), 1);
113 lpsz = _tcsinc (lpsz);
114 _tcsncpy (lpsz, str, _MAX_EXT-1);
117 // // return the extension without the leading dot (e.g.: "sys")
118 // CString GetExt () const
119 // {
120 // CString str; str.Format(_T("%.*s"), _MAX_EXT, m_szExt); return StripLeadingDot (str);
121 // }
122 // // return the extension including the leading dot (e.g.: ".sys")
123 // CString GetDotExt () const
124 // {
125 // CString str; str.Format(_T("%.*s"), _MAX_EXT, m_szExt); return str;
126 // }
128 // set the full path to the file, _including_ a trailing slash (e.g.: "c:\WinNT\system32\")
129 void SetPath (LPCTSTR pStr)
131 assert (IsTrailingSlash (pStr) || _tcslen (pStr) == 0);
132 _tsplitpath (pStr, m_szDrive, m_szDir, 0, 0);
134 // // return the full path to the file, including a trailing slash (e.g.: "c:\WinNT\system32\")
135 // CString GetPath () const
136 // {
137 // CString str;
138 // LPTSTR p = str.GetBuffer(_MAX_PATH);
139 // _tmakepath (p, m_szDrive, m_szDir, NULL, NULL);
140 // str.ReleaseBuffer();
141 // return str;
142 // }
143 // // return the relative Path to another location
144 // CString GetRelativePathFrom (LPCTSTR pszFrom) const;
145 // CString GetRelativePathTo (LPCTSTR pszTo) const;
147 // // return this as an absolute path
148 // CString GetAbsolutePath () const;
150 // set the file title including the extension (e.g.: "kernel32.sys")
151 void SetFileName (LPCTSTR pStr)
153 assert(pStr != NULL);
154 // assert(AfxIsValidString(pStr));
156 _tsplitpath (pStr, 0, 0, m_szFileTitle, m_szExt);
158 // // return the file title including the extension (e.g.: "kernel32.sys")
159 std::string GetFileName () const
161 TCHAR str[_MAX_PATH];
162 _tmakepath (str, NULL, NULL, m_szFileTitle, m_szExt);
163 return str;
166 // Accept '/' or '\\' as a path separator on all platforms.
167 static BOOL IsPathSeparator(TCHAR ch)
169 return (ch == _T('/') || ch == _T('\\'));
172 // returns wether the path is a UNC path (e.g.: \\computer\c$\windows\kernel32.sys)
173 BOOL IsUNCPath() const
175 return IsUNCPath(m_szDir);
178 // returns wether the path is a UNC path (e.g.: \\computer\c$\windows\kernel32.sys)
179 static BOOL IsUNCPath(LPCTSTR szPath)
181 assert(szPath != NULL);
182 // assert(AfxIsValidString(szPath));
184 if (_tcslen(szPath) < 2)
185 return FALSE;
187 return (szPath[0] == '\\' && szPath[1] == '\\');
190 BOOL IsFullPath ()
192 std::string path = GetFilePath();
194 return(IsPathSeparator(path[0])
195 #if !PLATFORM_UNIX
196 || (_istalpha(path[0]) && path[1] == _T(':'))
197 #endif // !PLATFORM_UNIX
202 // remove all unnecessary elements from path like path/to/./subpath, or path/to//subpath
203 void Normalize ();
205 protected:
206 bool IsTrailingSlash (LPCTSTR lpstr) const
208 assert(lpstr != NULL);
209 // assert(AfxIsValidString(lpstr));
211 int len = _tcslen (lpstr);
212 if (len == 0)
213 return false;
215 LPCTSTR lpsz = _tcsninc(lpstr, len-1);
216 return (_tcsncmp (lpsz, _T("\\"), 1) == 0 || _tcsncmp (lpsz, _T("/"), 1) == 0);
219 bool IsLeadingDot (LPCTSTR pStr) const
221 assert(pStr != NULL);
222 // assert(AfxIsValidString(pStr));
224 return (_tcsncmp (pStr, _T("."), 1) == 0);
227 // CString StripLeadingDot (CString str) const
228 // {
229 // while (IsLeadingDot (str))
230 // str = str.Right (str.GetLength() - 1);
231 // return str;
232 // }
234 void SplitComputer ()
236 // If UNC name
237 if ( !_tcsncmp(m_szDir, _T("\\\\"), 2))
239 // Get the computername
240 _tcsncpy(m_szComputer, _tcsstr(m_szDir, _T("\\\\"))+2, sizeof(m_szComputer)-1 );
241 _tcsnset(_tcsstr(m_szComputer, _T("\\")), 0, 1);
243 // Strip the computername from the directory
244 _tcsncpy(m_szDir, _tcsstr(m_szDir, _T("\\\\"))+2, sizeof(m_szDir)-1 );
245 _tcsncpy(m_szDir, _tcsstr(m_szDir, _T("\\")), sizeof(m_szDir)-1 );
250 TCHAR m_szDrive[_MAX_DRIVE];
251 TCHAR m_szDir[_MAX_DIR];
252 TCHAR m_szFileTitle[_MAX_FNAME];
253 TCHAR m_szExt[_MAX_EXT];
254 TCHAR m_szComputer[_MAX_FNAME];
257 #endif // !defined(AFX_FILENAME_H__5778DD0A_6746_4301_A8C4_B82F7CC70FC9__INCLUDED_)