CString

Derived from: BString

Declared in: CString.h

Library: Chronology.so

This class adds some powerfull functionality to the BString class like better conversion of numbers and better and more flexible formating.

The BString class was already shipped with BeOS R4, but the documentation was not linked to the BeBook. I'm not sure, if they want to change something in it, so it could be possible, that in one of the next system releases, I have to change CString, but I think the risk is worth it.

You should also know, that the CString-formatting is slower than sprintf().


Additional Constructors


CTime()

      CString(int16 anInt16, const char *format = "%d")
      CString(int32 anInt32, const char *format = "%ld")
      CString(int64 anInt64, const char *format = "%Ld")
      CString(float aFloat, const char *format = "%f")

These constructors take the number, format it with the standard C formating sequence and initialize the BString with it.


Member Functions


Format()

      CString &Format(const char *format = NULL)

This formats the object using format. But format is not a standard C format string, it has much more functionality, which you can very easily expand with the hook function AddToken().

The formatstring may contain several tokens enclosed by the delimiter "%", for each token AddToken() is called, with the name of the token as parameter.

Thus you can have a formatstring like: "%hour%:%minute%:%second%". This string contains the tokens hour, minute and second, enclosed by the "%". Your derived class would contain public variables for hour, minute and second, which would be set from outside before formating and AddToken() would be implemented to convert these values to a string and append them.

It also returns a reference to the CString as result, so you could possible write:

      printf( "%s\n", dformater.Format(...).String());

to printf() the const char* of the CString.


Hook Functions


AddToken()

      virtual void AddToken(const BString &name)

By implementing this function, you can add your own tokens to the already known. To add the three tokens from above, you would implement AddToken() like this:

void MyClass::AddToken( const BString &name ) {
    if ( name == "hour" ) {
        *this += CString( hour );
    } else if ( name == "minute" ) {
        *this += CString( minute, "%02ld" );	// to have a leading zero
    } else if ( name == "second" ) {
        *this += CString( second, "%02ld" );
    } else CString::AddToken( name );           // always remember to call the base function
}

You can also add a string to the object by writing:

    *this += "Hello World";	// ofcourse it may also be a pointer

Currently there are only two default tokens:


Have a look at the example programm to see a (almost) real life example.

If you have a suggestion, bug reports or ideas of how to speed things up, send me an email.




The Cronology Library, in lovely HTML, for BeOS Release 4.

Copyright © 1999 Michael Praschl. All rights reserved.

Last modified: 18. Feb 1999