Introduction
I needed a CString
compatible path splitter, so here is my very
handy version ;) It's fully network path compliant and Windows CE compatible
also...
Splitting path
Two ways to split a path :
During constructor
While creating the splitter object :
CString l_oStrPath;
l_oStrPath = "C:\\Windows\\test.txt";
CPathSplit l_oPathSplit(l_oStrPath);
Once created
Cut another path into pieces:
CString l_oStrPath;
CPathSplit l_oPathSplit;
l_oStrPath = "C:\\Windows\\test.txt";
l_oPathSplit.Split(l_oStrPath);
CPathSplit::Split()
returns true
if the path
is valid and was correctly split, else false
!
Getting path elements
Structure of a path
typedef enum ePathElement
{ ePATHEL_DRIVE = 0
, ePATHEL_DIRECTORY
, ePATHEL_FILENAME
, ePATHEL_EXTENSION
, ePATHEL__END
};
An enum
list is now provided to allow selection of the path
elements.
In the case of a disk drive :
l_oStrFullPath
=
"C:\Windows\test.txt"In the case of a network drive :
l_oStrFullPath
=
"\\Server_remote\Charles\test.txt"In
the case of a Pocket PC drive :
l_oStrFullPath
=
"\Storage
Card\test.txt"
The following table shows which enum element match which path element :
Enum element |
Disk drive |
Network drive |
Pocket PC drive |
ePATHEL_DRIVE |
"C:" |
"" (empty) |
"" (empty) |
ePATHEL_DIRECTORY |
"\Windows\" |
"\Server_remote\Charles\" |
"\Storage Card\" |
ePATHEL_FILENAME |
"test" |
"test" |
"test" |
ePATHEL_EXTENSION |
".txt" |
".txt" |
".txt" |
Getting elements of a path
The following table shows which function returns which path element :
Function |
Enum element |
GetDrive() |
ePATHEL_DRIVE |
GetDirectory() |
ePATHEL_DIRECTORY |
GetFileName() |
ePATHEL_FILENAME |
GetExtension() |
ePATHEL_EXTENSION |
Once a path is split, you can retrieve each element.
For instance, the path "C:\Windows\test.txt" was split:
Return the drive
l_oStrDrive = l_oPathSplit.GetDrive();
l_oStrDrive
= "C:"
Return the directory
l_oStrDirectory = l_oPathSplit.GetDirectory();
l_oStrDirectory
= "\Windows\"
Return the file name without extension
l_oStrFileName = l_oPathSplit.GetFileName();
l_oStrFileName
= "test"
Return the file extension (includes the DOT)
l_oStrFileExtension = l_oPathSplit.GetExtension();
l_oStrFileExtension
= ".txt"
Getting a path suite :
You can naw also retrieve complete parts of the full qualified path.
Return the full path
l_oStrFullPath = l_oPathSplit.GetPath();
l_oStrFullPath
= "C:\Windows\test.txt"
Return the beginning of the path including an ending path element
l_oStrPathFile = l_oPathSplit.GetPath(ePATHEL_FILENAME);
l_oStrPathFile
= "C:\Windows\test"
Return the beginning of the path and adding a queue string
l_oStrFileExt = ".html";
l_oStrPathCustom = l_oPathSplit.GetPath(ePATHEL_FILENAME, &l_oStrFileExt);
l_oStrPathCustom
= "C:\Windows\test.html"
Return a part of the path from an element to another one
l_oStrPathPart = l_oPathSplit.GetPath(ePATHEL_FILENAME, NULL,
ePATHEL_DIRECTORY);
l_oStrPathPart
= "\Windows\test"
Conclusion
In the case of a network drive or a Pocket PC drive, there is no drive
letter:
l_oStrPath = "\\\\Windows\\test.txt";
l_oPathSplit.Split(l_oStrPath);
l_oStrDrive = l_oPathSplit.GetDrive();
l_oStrDrive
= ""
Extension ALWAYS includes the dot !
History
- 2004/03/13 - 1.03 : Modified core for better usability and part retrieving
- 2003/08/22 - 1.02 : Added empty default constructor for embedded path
splitter
- 2003/08/21 - 1.01 : Modifications based on Eddie Velasquez and Marga.FP's
wishes
- 2003/08/21 - 1.00 : First release of
CPathSplit
Note
Sorry if it seems to be a bit odd, I know there are many-many path splitters.
But this one also works quite fine on Windows CE due to its CString
usage ;) It doesn't intend to replace other splitters featuring regular
expression or so! It's just a little and trivial path splitter for you to
enjoy, so don't leave bad comments because it's nothing more fabulous than just
a tens of MFC lines ;)
About the indent some people around there may find strange : I know it seems
to be really ugly, but I gave a try to the Concurrent Clean language and the way
it's indented pleases me alot. It structures almost everything, even test
statements, with one instruction per line. Hence, the clarity of the code is
very good, and it reminds me assembler where you cannot put several instructions
per line. Far easier also when something crash, when you debug you automatically
find the faulty instruction by going to the right line. You don't have to
scratch your head to look for which instruction on the line caused the crash ;)
And it's very... clean when you are a compile defined freak like me
(embedded coder past). Easy also to comment each line and/or argument !