Click here to Skip to main content
Licence Zlib
First Posted 20 Aug 2003
Views 69,615
Bookmarked 18 times

CPathSplit

By | 20 Aug 2003 | Article
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 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 ;) 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

About the Author

Kochise

Software Developer

France France

Member

KOCH David, 31 years old
Coder (embedded C/C++, ASM, Erlang)

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalidea for future versions Pinmembermgama5:05 19 Aug '05  
GeneralExcuse my ignorance... Pinmember.:fl0yd:.15:07 22 Aug '03  
GeneralRe-inventing the wheel, meeee ? PinmemberKochise10:28 23 Aug '03  
GeneralRe: Re-inventing the wheel, meeee ? Pinmember.:fl0yd:.11:52 23 Aug '03  
GeneralI'm glad you expected so much from me :) PinmemberKochise22:10 23 Aug '03  
GeneralRe: Excuse my ignorance... Pinmemberpeterchen2:07 28 Jun '04  
GeneralTell me where it is difficult... PinmemberKochise21:53 2 Jul '04  
GeneralRe: Tell me where it is difficult... Pinmemberpeterchen2:08 3 Jul '04  
QuestionWhy CString*? PineditorNishant S3:35 21 Aug '03  
AnswerRe: Why CString*? PinmemberKochise3:58 21 Aug '03  
GeneralRe: Why CString*? PinmemberFlorian Heidenreich6:08 21 Aug '03  
GeneralRe: Why CString*? PinmemberKochise6:36 21 Aug '03  
GeneralRe: Why CString*? PinmemberFlorian Heidenreich5:10 22 Aug '03  
GeneralRe: Why CString*? PinmemberEddie Velasquez8:43 21 Aug '03  
GeneralWill have a sleep first and check this tomorrow... PinmemberKochise9:13 21 Aug '03  
GeneralOK, work done ! PinmemberKochise12:22 21 Aug '03  
GeneralResults report ! PinmemberKochise12:30 21 Aug '03  
GeneralJust added a test app ;) PinmemberKochise2:19 22 Aug '03  
GeneralThe source code formatting/indentation :-) PineditorNishant S3:34 21 Aug '03  
GeneralRe: The source code formatting/indentation :-) PinmemberKochise4:00 21 Aug '03  
Generalbad style = errors Pinmemberdog_spawn8:54 21 Aug '03  
GeneralRe: bad style = errors PinmemberKochise9:10 21 Aug '03  
GeneralRe: bad style = errors PinmemberClaudius Mokler3:33 22 Aug '03  
GeneralCod is a fish, right, but... PinsussAnonymous4:52 22 Aug '03  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 21 Aug 2003
Article Copyright 2003 by Kochise
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid