Allow indicating the format when dumping data.

git-svn-id: http://voip.null.ro/svn/yate@6106 acf43c95-373e-0410-b603-e72c3f656dc1
This commit is contained in:
marian 2016-03-29 07:22:26 +00:00
parent 1595bc6ee5
commit 78b1771518
2 changed files with 39 additions and 15 deletions

View File

@ -18,6 +18,7 @@
*/ */
#include "yatemath.h" #include "yatemath.h"
#include <stdio.h>
using namespace TelEngine; using namespace TelEngine;
@ -312,17 +313,33 @@ String& BitVector::appendTo(String& buf, unsigned int offs, int len) const
// Math // Math
// //
// Append a Complex number to a String (using "%g%+gi" format) // Append a Complex number to a String (using "%g%+gi" format)
String& Math::dumpComplex(String& dest, const Complex& val, const char* sep) String& Math::dumpComplex(String& dest, const Complex& val, const char* sep,
const char* fmt)
{ {
String tmp; if (TelEngine::null(fmt))
return dest.append(tmp.printf("%g%+gi",val.re(),val.im()),sep); fmt = "%g%+gi";
else if (::strlen(fmt) > 30) {
String tmp;
return dest.append(tmp.printf(512,fmt,val.re(),val.im()),sep);
}
char s[60];
::sprintf(s,fmt,val.re(),val.im());
return dest.append(s,sep);
} }
// Append float value to a String (using %g format) // Append float value to a String (using %g format)
String& Math::dumpFloat(String& dest, const float& val, const char* sep) String& Math::dumpFloat(String& dest, const float& val, const char* sep,
const char* fmt)
{ {
String tmp; if (TelEngine::null(fmt))
return dest.append(tmp.printf("%g",val),sep); fmt = "%g";
else if (::strlen(fmt) > 30) {
String tmp;
return dest.append(tmp.printf(512,fmt,val),sep);
}
char s[60];
::sprintf(s,fmt,val);
return dest.append(s,sep);
} }
/* vi: set ts=8 sw=4 sts=4 noet: */ /* vi: set ts=8 sw=4 sts=4 noet: */

View File

@ -1074,16 +1074,18 @@ public:
* @param func Pointer to function who appends the object to a String * @param func Pointer to function who appends the object to a String
* (0 to dump all available from offset) * (0 to dump all available from offset)
* @param sep Vector elements separator * @param sep Vector elements separator
* @param fmt Optional format to use
* @return Destination string address * @return Destination string address
*/ */
String& dump(String& buf, String& (*func)(String& s, const Obj& o, const char* sep), String& dump(String& buf,
const char* sep = ",") const { String& (*func)(String& s, const Obj& o, const char* sep, const char* fmt),
const char* sep = ",", const char* fmt = 0) const {
const Obj* d = data(); const Obj* d = data();
if (!(d && func)) if (!(d && func))
return buf; return buf;
String localBuf; String localBuf;
for (const Obj* last = end(d,length()); d != last; ++d) for (const Obj* last = end(d,length()); d != last; ++d)
(*func)(localBuf,*d,sep); (*func)(localBuf,*d,sep,fmt);
return buf.append(localBuf); return buf.append(localBuf);
} }
@ -1099,23 +1101,24 @@ public:
* @param linePrefix Prefix for new lines. Empty string to use the suffix * @param linePrefix Prefix for new lines. Empty string to use the suffix
* @param suffix String to always add to final result (even if no data dumped) * @param suffix String to always add to final result (even if no data dumped)
* @param sep Vector elements separator * @param sep Vector elements separator
* @param fmt Optional format to use
* @return Destination string address * @return Destination string address
*/ */
String& dump(String& buf, unsigned int lineLen, String& dump(String& buf, unsigned int lineLen,
String& (*func)(String& s, const Obj& o, const char* sep), String& (*func)(String& s, const Obj& o, const char* sep, const char* fmt),
unsigned int offset = 0, const char* linePrefix = 0, unsigned int offset = 0, const char* linePrefix = 0,
const char* suffix = "\r\n", const char* sep = ",") const { const char* suffix = "\r\n", const char* sep = ",", const char* fmt = 0) const {
const Obj* d = data(); const Obj* d = data();
if (!(d && func)) if (!(d && func))
return buf.append(suffix); return buf.append(suffix);
if (TelEngine::null(linePrefix)) if (TelEngine::null(linePrefix))
linePrefix = suffix; linePrefix = suffix;
if (!lineLen || TelEngine::null(linePrefix)) if (!lineLen || TelEngine::null(linePrefix))
return dump(buf,func,sep) << suffix; return dump(buf,func,sep,fmt) << suffix;
String localBuf; String localBuf;
for (const Obj* last = end(d,length()); d != last;) { for (const Obj* last = end(d,length()); d != last;) {
String tmp; String tmp;
(*func)(tmp,*d,0); (*func)(tmp,*d,0,fmt);
if (++d != last) if (++d != last)
tmp << sep; tmp << sep;
offset += tmp.length(); offset += tmp.length();
@ -1536,18 +1539,22 @@ public:
* @param buf Destination string * @param buf Destination string
* @param val Value to dump * @param val Value to dump
* @param sep Optional separator * @param sep Optional separator
* @param fmt Format to use ("%g%+gi" if not given)
* @return Destination string address * @return Destination string address
*/ */
static String& dumpComplex(String& buf, const Complex& val, const char* sep = 0); static String& dumpComplex(String& buf, const Complex& val, const char* sep = 0,
const char* fmt = 0);
/** /**
* Dump a float number to a String * Dump a float number to a String
* @param buf Destination string * @param buf Destination string
* @param val Value to dump * @param val Value to dump
* @param sep Optional separator * @param sep Optional separator
* @param fmt Format to use ("%g" if not given)
* @return Destination string address * @return Destination string address
*/ */
static String& dumpFloat(String& buf, const float& val, const char* sep = 0); static String& dumpFloat(String& buf, const float& val, const char* sep = 0,
const char* fmt = 0);
}; };