TempStr
by Rick Overman · 01/14/2001 (12:11 am) · 1 comments
Download Code File
I am always in search of code that helps me code or makes my programs cleaner and easier to read. TempStr is an amazingly simple chunk of code that does both. One of the more common and mundane programming tasks is string formatting. How many times have you typed code like this?
There are two C++ features that are key to making TempStr possible. The first is templates. Being a template allows us to specify a unique buffer size for each instance of TempStr. For example TempStr<15> creates a buffer 15 characters long and TempStr<256> a buffer 256 characters long. The advantage to using specific buffer sizes is you don't waste any memory/stack-space and you can use the buffer length assertions to validate that the strings you are processing are the size you are expecting. Buffer overflows are one of the most common errors and one of the most difficult to track down. When using sprintf it is so easy for the output string to be longer than expected and exceed the length of the buffer you provided. Do you check or assert all your sprintf calls for buffer overflows? probably not. By using TempStr you will catch all those buffer overflows before they start causing mysterious crashes.
The second feature is the ability to specify a user defined type conversion operator. In the case of TempStr we define the conversion between a TempStr to a char* by providing the operator char*() member function. This allows us to use TempStr where ever we would normally use a character pointer. I am not even going to start to list the numerous places and ways you can use character pointers (aka strings)... you get the picture, specifying the type conversion operator makes TempStr a very transparently useful class.
If you are wondering why I called the class TTempStr and not TempStr, it was not a mistake, I always prefix all my template classes with 'T' so that is is obvious they are a templates.
Advantages of using TempStr:
1) Produces clean, easy to read code
2) Transparent, does not require changing other code to start taking advantage of it.
3) Buffer overflow checking.
4) Allows you to keep other api's clean by not polluting then with variable argument processing and guesses about temporary buffer size requirements.
5) All the cool programmers are using it. ;)
Well that's about it. You can download the header file via the link above.
If you have any comments or suggestions post them here.
I am always in search of code that helps me code or makes my programs cleaner and easier to read. TempStr is an amazingly simple chunk of code that does both. One of the more common and mundane programming tasks is string formatting. How many times have you typed code like this?
char filename[256]; int length = sprintf(filename, "%s:\%s\%s", drive, path, file); assert(length<256); FILE *handle = fopen(filename, "r");or maybe like this
char text[48]; int length = sprintf(text, "Target Range: %d meters", targetRange); assert(length<48); draw_text(100,100, text);I will bet you have seen this a lot. The repetitive pattern is obvious but how to cleanly collapse the code may not be. TempStr encapsulates the variable length buffer creation, assertion checking and printf style string formatting into a simple to use C++ class. Using TempStr the code we looked at above can be rewritten like this:
// all inline example
FILE *handle = fopen( TTempStr<256>("%s:\%s\%s", drive, path, file),"r");
// using it like a variable
TTempStr<48> text;
text.set("Target Range: %d meters", targetRange);
draw_text(100,100, text);What makes it work?There are two C++ features that are key to making TempStr possible. The first is templates. Being a template allows us to specify a unique buffer size for each instance of TempStr. For example TempStr<15> creates a buffer 15 characters long and TempStr<256> a buffer 256 characters long. The advantage to using specific buffer sizes is you don't waste any memory/stack-space and you can use the buffer length assertions to validate that the strings you are processing are the size you are expecting. Buffer overflows are one of the most common errors and one of the most difficult to track down. When using sprintf it is so easy for the output string to be longer than expected and exceed the length of the buffer you provided. Do you check or assert all your sprintf calls for buffer overflows? probably not. By using TempStr you will catch all those buffer overflows before they start causing mysterious crashes.
The second feature is the ability to specify a user defined type conversion operator. In the case of TempStr we define the conversion between a TempStr
If you are wondering why I called the class TTempStr and not TempStr, it was not a mistake, I always prefix all my template classes with 'T' so that is is obvious they are a templates.
Advantages of using TempStr:
1) Produces clean, easy to read code
2) Transparent, does not require changing other code to start taking advantage of it.
3) Buffer overflow checking.
4) Allows you to keep other api's clean by not polluting then with variable argument processing and guesses about temporary buffer size requirements.
5) All the cool programmers are using it. ;)
Well that's about it. You can download the header file via the link above.
If you have any comments or suggestions post them here.

Anthony "ynohtna" Bowyer-Lowe
I did notice, however, that the constructor and set method contain identical instructions apart from the return value of the set method. Personally, I would factor out this common functionality into a private method, thus avoiding the duplication and providing a single location for modifications.