Until very recently, I've been mostly using my own
string class and it worked OK. But then I felt the urge to stick more to the standard and decided to use
CString for all
string manipulations, only it doesn't have some of the functions I used so often in my
string class. At first, I decided to derive my own class from
CString, but quickly dropped this idea because the solution would be far from flexible, especially with dozens of constructors available in
CStringT. Instead, I made several global functions which do the things that
CString doesn't - this is what StringLib.h is.
Using the Code
StringLib is best used to supplement
CString. That is, you could use
CString as usual in your application, and call some of
StringLib's functions from time to time in order to perform special tasks. This can save you the time needed to write trivial and often used
string manipulation code.
So what can
StringLib do anyway?
bool IsRelativePath(const TCHAR* str);
Determines if the path that was passed to it is relative or absolute. Network paths are considered absolute.
bool RelativeToAbsolute(CString& str, HMODULE base = 0);
bool RelativeToAbsolute(CString& str, const TCHAR* base);
Converts the path contained in the
string from relative (to the given module or other file) into absolute. This one's very useful because you can get the full path to any file in your application's directory with just one line of code.
void TailBackslash(CString& str);
Adds a backslash character to the end of the
string if there isn't one already.
int GetTokenCount(const TCHAR* str, const TCHAR* delimiters);
Determines the number of tokens inside the
CString GetToken(const TCHAR* str, const TCHAR* delimiters, int index);
Retrieves the token with the given zero-based index from the
string. It's rather inefficient due to the nature of the operation, so be warned.
bool TrimRightToken(CString& str, const TCHAR* delimiters);
Removes the rightmost token in the given
void Explode(const TCHAR* str, const TCHAR* delimiters, CStringArray& arr);
Converts the given
string into an array of tokens.
CString Implode(TCHAR delimiter, const CStringArray& arr);
Converts the given array of tokens into a
int ToInt(const TCHAR* str);
integer. Looks much better than this ugly
CString ToCString(int num);
string. Much shorter than
Points of Interest
The code may look a bit inefficient in some of the functions. That's because it is. However, the reason for this inefficiency is that these functions can hardly be implemented much, much better due to the nature of the problems that they solve.
But just like many other developers, I believe code reuse and lower maintenance costs always warrant a trifle inefficient code. It's exactly what these functions do - help in making your code more readable by allowing you to concentrate on the task at hand rather than on
string tokenizing, for instance. No wonder this comes at a price.
Of course you may use this code as you please, but do so at your own risk, although there doesn't seem to be any immediate risk in using it.
- 31st January, 2007: Initial post