Back to Amir Israeli Homepage

[CPath : Extended functions for general string path management]

This article was contributed by Amir Israeli Amir Israeli.
Amir Israeli Web Site

Environment: Win 95/98 VC6 SP4

The class was first written by me at the beginning of 2000, when I first got to know Windows , The need for more complex functions in addition to those that already exist in C++ libraries , windows libraries and Shell32.dll light functions.
I need several more for specific reasons.

Usage of paths is so widespread that we can say that any kind of data (even tabular one: pathtoDB\\tables\\table\\[i][j] = "hello") may be represented as a path. It may be File System path,Registry paths,Version info (of any file in any VStudio : Look at resource few at Version\\VS_VERSION_INFO), Shell path (Fully qualified IDList: which is a binary path) a link (IShellLink) or url-link. ,also a path within a database - to some data or field . Any hirerchical data representation is as a matter of fact : a path. Any data that may be represented in a tree-ctrl is a path.
This class was simply created in order to supply path functionality for my File-sytem+Shell objects then I added some functionality to support registry operations.

This class is already documented with short remarks within the source code . Moreover function names are simple (and sometimes long), so I will demonstrate only those functions that require an additinal word.

Basic functions include concatenating strings and addition/removal of separators from path: separators that are invalid : such as "\\c:\\hello\\" : Trim() will CString::Trim.. separators at start and end of that string into "c:\\hello".
The second function appends another string and (of course) Trims.

	// Trims a character off a string (both sides)
	void Trim(CString &strPath,TCHAR ch=_T('\\'));
	// appends a string in the end of path (also trims)
	void AppendPath(CString &strTarget,LPCTSTR lpszSrc);
A very big group of functions will handle the levels that create a multiple-level-path : . The first string of any multiple-level-path has an index of 0 (zero). Level manipulators include functions to get a specific level, count levels , get level after/before other levels, remove some level(last) and check whether a level exist in path.
	// Level functions:
	// counts number of level in a given path
	int CountLevels(LPCTSTR lpszPath);
	/* get level of some index (indexes start at 0) from a given string.
	if level doesn't exist then returns FALSE */
	BOOL GetLevel(int nLevel,LPCTSTR lpszPath,CString &strOut);
	BOOL GetLastLevel(LPCTSTR lpszPath,CString &strOut);

	/* searches for a sub string in some path and in certain place :
	-1 indicates in any place */
	BOOL LevelInPath(LPCTSTR lpszPath,LPCTSTR lpszRequired,int nLevelRequired=-1);

	/* get some level before/After some other level that appear in condition : 
	lpszBeforeLevel/lpszAfterLevel */
	BOOL GetLevelBeforeLevel(LPCTSTR lpszPath,LPCTSTR lpszBeforeLevel,CString &strOut,BOOL bGiveRoot=FALSE);
	BOOL GetLevelAfterLevel(LPCTSTR lpszPath,LPCTSTR lpszAfterLevel,CString &strOut);

	// removes the last item in path : useful if want to go up (in file-system structure/shell structure)
	BOOL RemoveLastItem(CString &strPath);
There are also several functions available for registry paths manipulations
	BOOL FormulatePath(CString &strRegPath,HKEY &hKey);
	BOOL RootKey2String(CString &strRegSrc,HKEY &hKey);
	BOOL StringRoot2key(const CString &strRegPath,HKEY &hKey);
Several more functions are available : msg function (does amessage-box, and IsEqual() checks if 2 strings are identical.

This update (March 2001) I optimized several functions , (especially level functions) . I removed all shortcuts for registry keys : for example removed HKLM for HKEY_LOCAL_MACHINE. (less is better approach)
Still this version is compatible to previous version of this class.

Downloads

Projects that uses path include :
All Registry objects
Shell Namespace projects and file systems.

Download source - 3 Kb

History

Date Posted: December 20, 2000

Home  |  Links  |  About