summaryrefslogtreecommitdiff
path: root/os
diff options
context:
space:
mode:
authorPeter Hutterer <peter.hutterer@who-t.net>2013-01-10 13:20:12 +1000
committerPeter Hutterer <peter.hutterer@who-t.net>2013-01-17 17:17:41 +1000
commitcde7cbe9674e8a771f9a4e646c1772a46a8230fb (patch)
tree6614374b40805df9eddbd83bb7eb1994765daa23 /os
parent20def57632583aef095ca18792c7fce16d2d9004 (diff)
os: add support for %f to pnprintf
This is the lazy man's %f support. Print the decimal part of the number, then append a decimal point, then print the first two digits of the fractional part. So %f in sigsafe printing is really %.2f. No boundary checks in place here. Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net> Reviewed-by: Keith Packard <keithp@keithp.com>
Diffstat (limited to 'os')
-rw-r--r--os/log.c11
-rw-r--r--os/utils.c32
2 files changed, 42 insertions, 1 deletions
diff --git a/os/log.c b/os/log.c
index 213906420..7b5c9ee1c 100644
--- a/os/log.c
+++ b/os/log.c
@@ -351,7 +351,16 @@ pnprintf(char *string, size_t size, const char *f, va_list args)
for (i = 0; i < p_len && s_idx < size - 1; i++)
string[s_idx++] = number[i];
break;
-
+ case 'f':
+ {
+ double d = va_arg(args, double);
+ FormatDouble(d, number);
+ p_len = strlen_sigsafe(number);
+
+ for (i = 0; i < p_len && s_idx < size - 1; i++)
+ string[s_idx++] = number[i];
+ }
+ break;
default:
va_arg(args, char*);
string[s_idx++] = '%';
diff --git a/os/utils.c b/os/utils.c
index e396ba4b5..3ba6499c4 100644
--- a/os/utils.c
+++ b/os/utils.c
@@ -1990,6 +1990,38 @@ FormatUInt64(uint64_t num, char *string)
string[len] = '\0';
}
+/**
+ * Format a double number as %.2f.
+ */
+void
+FormatDouble(double dbl, char *string)
+{
+ int slen = 0;
+ uint64_t frac;
+
+ frac = (dbl > 0 ? dbl : -dbl) * 100.0;
+ frac %= 100;
+
+ /* write decimal part to string */
+ if (dbl < 0 && dbl > -1)
+ string[slen++] = '-';
+ FormatInt64((int64_t)dbl, &string[slen]);
+
+ while(string[slen] != '\0')
+ slen++;
+
+ /* append fractional part, but only if we have enough characters. We
+ * expect string to be 21 chars (incl trailing \0) */
+ if (slen <= 17) {
+ string[slen++] = '.';
+ if (frac < 10)
+ string[slen++] = '0';
+
+ FormatUInt64(frac, &string[slen]);
+ }
+}
+
+
/* Format a number into a hexadecimal string in a signal safe manner. The string
* should be at least 17 characters in order to handle all uint64_t values. */
void