PDA

View Full Version : MFC-like CString class


Tommi
03-14-2005, 10:25 AM
Sorry for being off-topic, but I wonder whether there is a free MFC-like and prefereably platform-independent CString class which contains the Format-method.
I've found a couple of implementations but they were quite lacking and most of them didn't compile at all without modifications (I'm using the DevC++ IDE / MingW compiler).

Thanks in advance

playmesumch00ns
03-14-2005, 01:49 PM
What exactly does CString's format method do? A good starting point when looking for these sort of algorithms is always the boost libraries: http://www.boost.org/

Vertizor
03-14-2005, 03:21 PM
Google search for STL. Standard Template Library. Cross platform, the namespace is std:: inside is a String class. It has a lot of useful functions, but no it does not have a format function.

You could extend this String class and make your own. It has a function to return a C (as in the language) string pointer (char*). Use that char* in conjunction with sprintf() from the C runtime and you have a nice portable C++ class with formating.

Google sprintf for more info. Works just like MFC's CString::format(), infact CString::format() borrows from sprintf, I wouldn't be surprised it it actually calls sprintf because the arguments are identical. sprintf just takes an additional arugment which is the destination buffer to store the formated string into.

gga
03-14-2005, 09:09 PM
googSorry for being off-topic, but I wonder whether there is a free MFC-like and prefereably platform-independent CString class which contains the Format-method.
I've found a couple of implementations but they were quite lacking and most of them didn't compile at all without modifications (I'm using the DevC++ IDE / MingW compiler).

Thanks in advance

Learn the STL. This is a standard library you will find with *every* C++ compiler. The std::string class should offer equivalents to most of the things in MFC and in many cases, in a smarter way.
Unlike the CString class, most STL implementations of std::string are not reference counted strings as the MFC implementation is, but use copy on write, which offers pretty much the same benefits.
The format() function is just printf or sprintf. You can usually do:


std::string data; // defined and filled in somewhere else
char* buf[256];
sprintf( buf, "%-20s", data.c_str() );
std::string newdata( buf );

If you are needing to do sprintf so often, you may indeed consider creating your own function to operate on a std::string, like (warning: untested code):

#include <stdarg.h>
#include <string>
#include <iostream>

// How to deal with functions with ... (ie. unlimited number of parameters)
// is usually platform dependant.
// These days, the most common approach is to use something like va_list
// and then vsnprintf();
const std::string my_sprintf( const char* format, ... )
{
int size = 256;
char *p = NULL;
va_list ap;
if ((p = (char*)malloc(size)) == NULL) return std::string();
while (1) {
/* Try to print in the allocated space. */
va_start(ap, format);
int n = vsnprintf (p, size, format, ap);
va_end(ap);
/* If that worked, return the string. */
if (n > -1 && n < size) break;
/* Else try again with more space. */
if (n > -1) size = n+1; /* precisely what is needed */
if ((p = (char*)realloc(p, size)) == NULL) return std::string();
}
std::string r( p );
free(p);
return r;
}

int main()
{
using namespace std;
string res = my_sprintf("%d", 10 );
cout << res << endl;
cout << my_sprintf("%-20sAha!", res.c_str() ) << endl;
return 0;
}

mummey
03-14-2005, 10:02 PM
Off-topic!?! This is the programming forum! :)

like they said, check out the STL, if that doesn't work then check the boost libraries.

MDuffy
03-15-2005, 01:22 AM
I've found the STL to be a flaming pile of poo to actually use, especially when tying together other libraries that are using STL, possibly different versions of STL (xerces + houdini + sgistl (for multimap) + some of the Linux libs, etc, etc.) Plus STL in general requires a lot of typing for doing what should be simple things, is not all that cross-platform in practice (because various compiler writers have created poor implementations of the standard, and the standard has changed over the years), and overall is pretty much impossible to debug.

But that's just me.

Google for FastString. It is a pretty good replacement for the MFC CString class. It has a format call, and is mostly a drop-in replacement for CString. It isn't perfect though, and I had to customize it a lot internally when I used it. I've since written my own string class from scratch (with format and a lot of other useful string manipulation approaches I've seen in various libs), and that's what I currently use in all my code. Easy to view the contents of the string in a debugger, and whenever I need new functionality I can just go in and add it without fuss.

Cheers,
Michael Duffy

dotTom
04-08-2005, 08:30 PM
I've found the STL to be a flaming pile of poo to actually use, especially when tying together other libraries that are using STL, possibly different versions of STL (xerces + houdini + sgistl (for multimap) + some of the Linux libs, etc, etc.) Plus STL in general requires a lot of typing for doing what should be simple things, is not all that cross-platform in practice (because various compiler writers have created poor implementations of the standard, and the standard has changed over the years), and overall is pretty much impossible to debug

STL is awesome. Learning template based meta programming is a great way to take your C++ to the next level, with it you can mix static and dynamic polymorphism, use traits to avoid pointless inheritance etc. Well worth learning. Whilst it's true that older compilers produce template errors that look like line noise modern compilers make a much better job of it. If you want a first class STL implementation take a look at SGI or HP implementations (in that order).

tweeeker
04-10-2005, 12:59 PM
If you go with STL also check out stringstream (#include <sstream>). This offers similar functionallity to sprintf without resorting to char pointers and c functions (which there's nothing wrong with of course).
T

CGTalk Moderation
04-10-2005, 12:59 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.