Added printf style format method for String.

Added utility method to append and align a string on a specified size.


git-svn-id: http://yate.null.ro/svn/yate/trunk@5959 acf43c95-373e-0410-b603-e72c3f656dc1
This commit is contained in:
andrei 2015-03-13 13:25:35 +00:00
parent b3bf7d962b
commit ca3a15437c
2 changed files with 112 additions and 0 deletions

View File

@ -998,6 +998,77 @@ String& String::append(double value, unsigned int decimals)
return operator+=(buf);
}
char* string_printf(unsigned int length, const char* format, va_list& va)
{
if (!format)
return 0;
char* buf = (char*)::malloc(length + 1);
if (!buf) {
Debug("String",DebugFail,"malloc(%d) returned NULL!",length);
return 0;
}
int len = ::vsnprintf(buf,length,format,va);
buf[len] = 0;
return buf;
}
String& String::printf(unsigned int length, const char* format, ...)
{
va_list va;
va_start(va,format);
char* buf = string_printf(length,format,va);
va_end(va);
if (!buf)
return *this;
char* old = m_string;
m_string = buf;
::free(old);
changed();
return *this;
}
String& String::printf(const char* format, ...)
{
va_list va;
va_start(va,format);
char* buf = string_printf(256,format,va);
va_end(va);
if (!buf)
return *this;
char* old = m_string;
m_string = buf;
::free(old);
changed();
return *this;
}
String& String::appendFixed(unsigned int space, const char* str, unsigned int len, char fill, int align)
{
if (len == (unsigned int)-1)
len = ::strlen(str);
if (!str || len == 0)
return *this;
int alignPos = 0;
if (len < space) {
if (align == Center)
alignPos = space / 2 - len / 2;
else if (align == Right)
alignPos = space - len;
} else
len = space;
char* buf = (char*)::malloc(space + 1);
if (!buf) {
Debug("String",DebugFail,"malloc(%d) returned NULL!",space);
return *this;
}
::memset(buf,fill,space);
::memcpy(buf + alignPos,str,len);
buf[space] = 0;
operator+=(buf);
::free(buf);
return *this;
}
bool String::operator==(const char* value) const
{
if (!m_string)

View File

@ -31,6 +31,7 @@
#include <stddef.h>
#include <unistd.h>
#include <errno.h>
#include <stdarg.h>
#ifndef _WORDSIZE
#if defined(__arch64__) || defined(__x86_64__) \
@ -1822,6 +1823,12 @@ private:
class YATE_API String : public GenObject
{
public:
enum Align {
Left = 0,
Center,
Right
};
/**
* Creates a new, empty string.
*/
@ -2445,6 +2452,40 @@ public:
*/
String& append(double value, unsigned int decimals = 3);
/**
* Add the parameters in a printf style
* @param format The output format.
* NOTE: The length of the resulting string will be at most 256
*/
String& printf(const char* format, ...) FORMAT_CHECK(2);
/**
* Add the parameters in a printf style
* @param length maximum length of the resulting string
* @param format The output format.
*/
String& printf(unsigned int length, const char* format, ...) FORMAT_CHECK(3);
/**
* Append a string and align it.
* @param space The space in wich the str will pe aligned.
* @param str The string to append
* @param len The str length
* @param fill Character to fill the empty space.
* @param align The alignment mode.
*/
String& appendFixed(unsigned int space, const char* str, unsigned int len = -1, char fill = ' ', int align = Left);
/**
* Append a string and align it.
* @param space The space in wich the str will pe aligned.
* @param str The string to append
* @param fill Character to fill the empty space.
* @param align The alignment mode.
*/
inline String& appendFixed(unsigned int space, const String& str, char fill = ' ', int align = Left)
{ return appendFixed(space,str.c_str(),str.length(),fill,align); }
/**
* Locate the first instance of a character in the string
* @param what Character to search for