Click here to Skip to main content
12,699,492 members (32,512 online)
Click here to Skip to main content
Add your own
alternative version

Stats

81.8K views
1K downloads
18 bookmarked
Posted

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

Share

About the Author

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

You may also be interested in...

Pro
Pro

Comments and Discussions

 
Generalidea for future versions Pin
mgama19-Aug-05 6:05
membermgama19-Aug-05 6:05 
GeneralExcuse my ignorance... Pin
.:fl0yd:.22-Aug-03 16:07
member.:fl0yd:.22-Aug-03 16:07 
GeneralRe-inventing the wheel, meeee ? Pin
Kochise23-Aug-03 11:28
memberKochise23-Aug-03 11:28 
GeneralRe: Re-inventing the wheel, meeee ? Pin
.:fl0yd:.23-Aug-03 12:52
member.:fl0yd:.23-Aug-03 12:52 
GeneralI'm glad you expected so much from me :) Pin
Kochise23-Aug-03 23:10
memberKochise23-Aug-03 23:10 
GeneralRe: Excuse my ignorance... Pin
peterchen28-Jun-04 3:07
memberpeterchen28-Jun-04 3:07 
GeneralTell me where it is difficult... Pin
Kochise2-Jul-04 22:53
memberKochise2-Jul-04 22:53 
GeneralRe: Tell me where it is difficult... Pin
peterchen3-Jul-04 3:08
memberpeterchen3-Jul-04 3:08 
QuestionWhy CString*? Pin
Nishant S21-Aug-03 4:35
editorNishant S21-Aug-03 4:35 
AnswerRe: Why CString*? Pin
Kochise21-Aug-03 4:58
memberKochise21-Aug-03 4:58 
GeneralRe: Why CString*? Pin
Florian Heidenreich21-Aug-03 7:08
memberFlorian Heidenreich21-Aug-03 7:08 
GeneralRe: Why CString*? Pin
Kochise21-Aug-03 7:36
memberKochise21-Aug-03 7:36 
GeneralRe: Why CString*? Pin
Florian Heidenreich22-Aug-03 6:10
memberFlorian Heidenreich22-Aug-03 6:10 
GeneralRe: Why CString*? Pin
Eddie Velasquez21-Aug-03 9:43
memberEddie Velasquez21-Aug-03 9:43 
GeneralWill have a sleep first and check this tomorrow... Pin
Kochise21-Aug-03 10:13
memberKochise21-Aug-03 10:13 
GeneralOK, work done ! Pin
Kochise21-Aug-03 13:22
memberKochise21-Aug-03 13:22 
GeneralResults report ! Pin
Kochise21-Aug-03 13:30
memberKochise21-Aug-03 13:30 
GeneralJust added a test app ;) Pin
Kochise22-Aug-03 3:19
memberKochise22-Aug-03 3:19 
GeneralThe source code formatting/indentation :-) Pin
Nishant S21-Aug-03 4:34
editorNishant S21-Aug-03 4:34 
GeneralRe: The source code formatting/indentation :-) Pin
Kochise21-Aug-03 5:00
memberKochise21-Aug-03 5:00 
Generalbad style = errors Pin
dog_spawn21-Aug-03 9:54
memberdog_spawn21-Aug-03 9:54 
GeneralRe: bad style = errors Pin
Kochise21-Aug-03 10:10
memberKochise21-Aug-03 10:10 
GeneralRe: bad style = errors Pin
Claudius Mokler22-Aug-03 4:33
memberClaudius Mokler22-Aug-03 4:33 
GeneralCod is a fish, right, but... Pin
Anonymous22-Aug-03 5:52
sussAnonymous22-Aug-03 5: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.

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