Formating a String in VSL

From Virtools Wiki

Jump to: navigation, search


Formating a String - a VSL binding for XString::Format

A while back it was asked here http://www.theswapmeet-forum.com/viewtopic.php?t=3914 how one might format a string printf style in VSL...

(Or, why isnt the XString::Format function bound to VSL. The most likely answer being that theres no way to bind functions with variable numbers of arguments to vsl.)

To make up for this, if you have your own manager, some useful functions to add are:

void FormatFloat(XString &dest, const char *format, float f)
{ dest.Format(format, f);}

void FormatVector(XString &dest, const char *format, VxVector &v)
{ dest.Format(format, v.x, v.y, v.z);}

void FormatVector2D(XString &dest, const char *format, Vx2DVector &v)
{ dest.Format(format, v.x, v.y);}

etc.

and then bind with..

 
CKERROR myManager::OnCKInit()
{
STARTVSLBIND(m_Context)
DECLAREFUN_C_3(void, FormatFloat, XString &dest, const char *format, float f)
DECLAREFUN_C_3(void, FormatVector, XString &dest, const char *format, VxVector &v)
DECLAREFUN_C_3(void, FormatVector2D, XString &dest, const char *format, Vx2DVector &v)
STOPVSLBIND
return CK_OK;
}
 

Example VSL:

String s; float f; Vector v;
FormatFloat(s, "%2.2f", f);
FormatVector(s, "%f, %f, %f", v);
Personal tools
The Swap-Meet