Click here to Skip to main content
15,884,739 members
Articles / Mobile Apps
Article

CPathSplit

Rate me:
Please Sign up or sign in to vote.
3.28/5 (12 votes)
20 Aug 2003Zlib3 min read 103.2K   1.1K   19   24
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 ;) 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 elementDisk driveNetwork drivePocket 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 :


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

License

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


Written By
Software Developer
France France
KOCH David, 41 years old
Coder (embedded, C/C++, ASM, Erlang)

Comments and Discussions

 
Generalidea for future versions Pin
mgama19-Aug-05 5:05
mgama19-Aug-05 5:05 
GeneralExcuse my ignorance... Pin
.:floyd:.22-Aug-03 15:07
.:floyd:.22-Aug-03 15:07 
GeneralRe-inventing the wheel, meeee ? Pin
Kochise23-Aug-03 10:28
Kochise23-Aug-03 10:28 
GeneralRe: Re-inventing the wheel, meeee ? Pin
.:floyd:.23-Aug-03 11:52
.:floyd:.23-Aug-03 11:52 
GeneralI'm glad you expected so much from me :) Pin
Kochise23-Aug-03 22:10
Kochise23-Aug-03 22:10 
GeneralRe: Excuse my ignorance... Pin
peterchen28-Jun-04 2:07
peterchen28-Jun-04 2:07 
GeneralTell me where it is difficult... Pin
Kochise2-Jul-04 21:53
Kochise2-Jul-04 21:53 
GeneralRe: Tell me where it is difficult... Pin
peterchen3-Jul-04 2:08
peterchen3-Jul-04 2:08 
QuestionWhy CString*? Pin
Nish Nishant21-Aug-03 3:35
sitebuilderNish Nishant21-Aug-03 3:35 
AnswerRe: Why CString*? Pin
Kochise21-Aug-03 3:58
Kochise21-Aug-03 3:58 
GeneralRe: Why CString*? Pin
Florian Heidenreich21-Aug-03 6:08
Florian Heidenreich21-Aug-03 6:08 
GeneralRe: Why CString*? Pin
Kochise21-Aug-03 6:36
Kochise21-Aug-03 6:36 
GeneralRe: Why CString*? Pin
Florian Heidenreich22-Aug-03 5:10
Florian Heidenreich22-Aug-03 5:10 
GeneralRe: Why CString*? Pin
Eddie Velasquez21-Aug-03 8:43
Eddie Velasquez21-Aug-03 8:43 
GeneralWill have a sleep first and check this tomorrow... Pin
Kochise21-Aug-03 9:13
Kochise21-Aug-03 9:13 
GeneralOK, work done ! Pin
Kochise21-Aug-03 12:22
Kochise21-Aug-03 12:22 
GeneralResults report ! Pin
Kochise21-Aug-03 12:30
Kochise21-Aug-03 12:30 
GeneralJust added a test app ;) Pin
Kochise22-Aug-03 2:19
Kochise22-Aug-03 2:19 
GeneralThe source code formatting/indentation :-) Pin
Nish Nishant21-Aug-03 3:34
sitebuilderNish Nishant21-Aug-03 3:34 
GeneralRe: The source code formatting/indentation :-) Pin
Kochise21-Aug-03 4:00
Kochise21-Aug-03 4:00 
I *ALWAYS* do Wink | ;) Have a look at http://www.codeproject.com/miscctrl/CSkinProgress.asp[^] !

It's easier to analyse the code structure, instead to have ten operations per line, and even easier to disable an operation just by commenting the spoted line Wink | ;)

Coding is an art that cannot only be learnt at school Wink | ;) I *LOVE* efficiency and reliability !

Kochise

In Cod we trust !
Generalbad style = errors Pin
dog_spawn21-Aug-03 8:54
dog_spawn21-Aug-03 8:54 
GeneralRe: bad style = errors Pin
Kochise21-Aug-03 9:10
Kochise21-Aug-03 9:10 
GeneralRe: bad style = errors Pin
Claudius Mokler22-Aug-03 3:33
Claudius Mokler22-Aug-03 3:33 
GeneralCod is a fish, right, but... Pin
Anonymous22-Aug-03 4:52
Anonymous22-Aug-03 4:52 

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

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