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://yate.null.ro/svn/yate/trunk@5960 acf43c95-373e-0410-b603-e72c3f656dc1
This commit is contained in:
andrei 2015-03-13 16:03:42 +00:00
parent ca3a15437c
commit 8ef9679c3d
2 changed files with 28 additions and 26 deletions

View File

@ -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;

View File

@ -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