Click here to Skip to main content
Click here to Skip to main content

CPathSplit

, 20 Aug 2003 Zlib
Rate this:
Please Sign up or sign in to vote.
CString based path splitter.

CPathSplitTest.png - 503 * 348 - 11840 bytes

Introduction

I needed a CString compatible path splitter, so here is my very handy version Wink | ;) 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"; // or "\\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 Wink | ;) 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 Wink | ;)

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 Wink | ;) 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 !

License

This article, along with any associated source code and files, is licensed under The zlib/libpng License

Share

About the Author

Kochise
Software Developer
France France
KOCH David, 31 years old
Coder (embedded C/C++, ASM, Erlang)

Comments and Discussions

 
Generalidea for future versions Pinmembermgama19-Aug-05 6:05 
GeneralExcuse my ignorance... Pinmember.:fl0yd:.22-Aug-03 16:07 
GeneralRe-inventing the wheel, meeee ? PinmemberKochise23-Aug-03 11:28 
GeneralRe: Re-inventing the wheel, meeee ? Pinmember.:fl0yd:.23-Aug-03 12:52 
GeneralI'm glad you expected so much from me :) PinmemberKochise23-Aug-03 23:10 
GeneralRe: Excuse my ignorance... Pinmemberpeterchen28-Jun-04 3:07 
GeneralTell me where it is difficult... PinmemberKochise2-Jul-04 22:53 
GeneralRe: Tell me where it is difficult... Pinmemberpeterchen3-Jul-04 3:08 
QuestionWhy CString*? PineditorNishant S21-Aug-03 4:35 
AnswerRe: Why CString*? PinmemberKochise21-Aug-03 4:58 
GeneralRe: Why CString*? PinmemberFlorian Heidenreich21-Aug-03 7:08 
GeneralRe: Why CString*? PinmemberKochise21-Aug-03 7:36 
GeneralRe: Why CString*? PinmemberFlorian Heidenreich22-Aug-03 6:10 
GeneralRe: Why CString*? PinmemberEddie Velasquez21-Aug-03 9:43 
GeneralWill have a sleep first and check this tomorrow... PinmemberKochise21-Aug-03 10:13 
GeneralOK, work done ! PinmemberKochise21-Aug-03 13:22 
GeneralResults report ! PinmemberKochise21-Aug-03 13:30 
GeneralJust added a test app ;) PinmemberKochise22-Aug-03 3:19 
GeneralThe source code formatting/indentation :-) PineditorNishant S21-Aug-03 4:34 
GeneralRe: The source code formatting/indentation :-) PinmemberKochise21-Aug-03 5:00 
Generalbad style = errors Pinmemberdog_spawn21-Aug-03 9:54 
GeneralRe: bad style = errors PinmemberKochise21-Aug-03 10:10 
GeneralRe: bad style = errors PinmemberClaudius Mokler22-Aug-03 4:33 
GeneralCod is a fish, right, but... PinsussAnonymous22-Aug-03 5:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 21 Aug 2003
Article Copyright 2003 by Kochise
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid