Sourceforge.net - The VCF's Project Host
   The VCF Website Home   |   Online Discussion Forums   |   Sourceforge.net Project Page   

VCF::Format Class Reference

The Format class is used to format a string, similar to the sprintf/printf family of functions, only made typesafe for C++, and less likely to encounter buffer overflow errors that are so easy to do with sprintf and friends. More...

#include <vcf/FoundationKit/Format.h>

List of all members.

Public Member Functions

 Format (const String &fmtStr)
 ~Format ()
template<typename ValType>
Formatoperator% (const ValType &val)
template<String >
Formatoperator% (const String &val)
 This hack is here to work around the broken (as far as I can tell) unicode support of OSX (yes, this *again*).
 operator String () const

Protected Member Functions

int countFormatTokens () const
const VCFChargetNextFormatTokenEndPos (int startPos) const
void gobblePercents ()


Detailed Description

The Format class is used to format a string, similar to the sprintf/printf family of functions, only made typesafe for C++, and less likely to encounter buffer overflow errors that are so easy to do with sprintf and friends.

The inspiration for this class comes unashamedly from the Boost Format library (http://www.boost.org/libs/format/doc/format.html). Any mistakes in the "translation" are mine, not Boost's.

The basic idea is to pass in a string to the Format instance, and then use the "%" operator to separate your arguments. Currently the usage is exactly the same as you would use printf/sprintf, in terms of formatting. For example:

    //printf
    printf( "Hello World, %d times!", 10 );

    //Encoding the same using a Format object
    String s = Format("Hello World, %d times!") % 10;
The "s" variable will now contain "Hello World, 10 times!".

The Format object will throw assert exceptions (in debug mode) if too few arguments are passed in, or too many. For example:

    //BAD - too few arguments!!
    String s = Format("Hello World, %d times!");
The above example is expecting 1 argument, and didn't receive any, and will thus assert.

    //BAD too many arguments!!
    String s = Format("Hello World, %d times!") % 100 % 0.56564;
The above example expected 1 argument, but received 2, and will assert.

See also:
StringUtils::format(const Format&)

StringUtils::traceWithArgs(const Format&)

System::print(const Format&)

System::println(const Format&)


Constructor & Destructor Documentation

VCF::Format::Format const String fmtStr  )  [inline]
 

VCF::Format::~Format  )  [inline]
 


Member Function Documentation

int VCF::Format::countFormatTokens  )  const [inline, protected]
 

const VCFChar* VCF::Format::getNextFormatTokenEndPos int  startPos  )  const [inline, protected]
 

void VCF::Format::gobblePercents  )  [inline, protected]
 

VCF::Format::operator String  )  const [inline]
 

template<String >
Format& VCF::Format::operator% const String val  )  [inline]
 

This hack is here to work around the broken (as far as I can tell) unicode support of OSX (yes, this *again*).

It would appear that the onlu "widechar" support that printf has is for wchar_t types, not hte UTF16 16bit type that all the rest of the ^&%$$ OS uses (see CFStringRef - it's a 16bit unicode type, like all the other sane Unicode API's use). So using ls, or S both fail to print anything at all here when a WideChar* (a 16bit unsigned short*) and we get errors from snprintf() about this. To workaround this silliness (which of course isn't required on Win32 - yeah Microsoft), I've added this specialization for WideChar* and then converted down to ansi before passing it into sprintf. This is incredibly lame, and if someone ever figures out a better solution, PLEASE let me know!!!!!!!

template<typename ValType>
Format& VCF::Format::operator% const ValType &  val  )  [inline]
 


The documentation for this class was generated from the following file:
   Comments or Suggestions?    License Information