Added a better description and more suggestive parameters for String::appendFixed method.
Clear the String in 'printf' method if 'malloc' fails. git-svn-id: http://voip.null.ro/svn/yate@5960 acf43c95-373e-0410-b603-e72c3f656dc1
This commit is contained in:
parent
e6c6214688
commit
5faef36bb6
|
@ -1018,8 +1018,10 @@ String& String::printf(unsigned int length, const char* format, ...)
|
|||
va_start(va,format);
|
||||
char* buf = string_printf(length,format,va);
|
||||
va_end(va);
|
||||
if (!buf)
|
||||
if (!buf) {
|
||||
clear();
|
||||
return *this;
|
||||
}
|
||||
char* old = m_string;
|
||||
m_string = buf;
|
||||
::free(old);
|
||||
|
@ -1042,28 +1044,28 @@ String& String::printf(const char* format, ...)
|
|||
return *this;
|
||||
}
|
||||
|
||||
String& String::appendFixed(unsigned int space, const char* str, unsigned int len, char fill, int align)
|
||||
String& String::appendFixed(unsigned int fixedLength, 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 (len < fixedLength) {
|
||||
if (align == Center)
|
||||
alignPos = space / 2 - len / 2;
|
||||
alignPos = fixedLength / 2 - len / 2;
|
||||
else if (align == Right)
|
||||
alignPos = space - len;
|
||||
alignPos = fixedLength - len;
|
||||
} else
|
||||
len = space;
|
||||
char* buf = (char*)::malloc(space + 1);
|
||||
len = fixedLength;
|
||||
char* buf = (char*)::malloc(fixedLength + 1);
|
||||
if (!buf) {
|
||||
Debug("String",DebugFail,"malloc(%d) returned NULL!",space);
|
||||
Debug("String",DebugFail,"malloc(%d) returned NULL!",fixedLength + 1);
|
||||
return *this;
|
||||
}
|
||||
::memset(buf,fill,space);
|
||||
::memset(buf,fill,fixedLength);
|
||||
::memcpy(buf + alignPos,str,len);
|
||||
buf[space] = 0;
|
||||
buf[fixedLength] = 0;
|
||||
operator+=(buf);
|
||||
::free(buf);
|
||||
return *this;
|
||||
|
|
32
yateclass.h
32
yateclass.h
|
@ -2453,38 +2453,38 @@ public:
|
|||
String& append(double value, unsigned int decimals = 3);
|
||||
|
||||
/**
|
||||
* Add the parameters in a printf style
|
||||
* Build a String 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
|
||||
* Build a String 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
|
||||
* Build a fixed aligned string from str and append it.
|
||||
* @param fixedLength The fixed length in which the 'str' will be aligned.
|
||||
* @param str The string to align
|
||||
* @param len The number of characters to use from str.
|
||||
* @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);
|
||||
String& appendFixed(unsigned int fixedLength, 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); }
|
||||
/**
|
||||
* Build a fixed aligned string from str and append it.
|
||||
* @param fixedLength The fixed length in which the 'str' will be aligned.
|
||||
* @param str The string to align
|
||||
* @param fill Character to fill the empty space.
|
||||
* @param align The alignment mode.
|
||||
*/
|
||||
inline String& appendFixed(unsigned int fixedLength, const String& str, char fill = ' ', int align = Left)
|
||||
{ return appendFixed(fixedLength,str.c_str(),str.length(),fill,align); }
|
||||
|
||||
/**
|
||||
* Locate the first instance of a character in the string
|
||||
|
|
Loading…
Reference in New Issue