aboutsummaryrefslogtreecommitdiffstats
path: root/qprintf.c
diff options
context:
space:
mode:
authormichael <michael@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>2008-12-31 06:07:29 +0000
committermichael <michael@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>2008-12-31 06:07:29 +0000
commitfff158da0a48a4ae2b32a927f02612d088bd2ebf (patch)
tree453968027fb0a15a5e978fc9a2e6a85e1151ee2c /qprintf.c
parent503d5e3b7f4a9ec26ab5d5f625948951c7d3fafa (diff)
downloadlcd4linux-fff158da0a48a4ae2b32a927f02612d088bd2ebf.tar.gz
format characters in qprintf() with optional length (r821) ported from volker_dev
git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@918 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
Diffstat (limited to 'qprintf.c')
-rw-r--r--qprintf.c72
1 files changed, 63 insertions, 9 deletions
diff --git a/qprintf.c b/qprintf.c
index 5704619..2cba4b7 100644
--- a/qprintf.c
+++ b/qprintf.c
@@ -31,7 +31,9 @@
* exported functions:
*
* int qprintf(char *str, size_t size, const char *format, ...)
- * works like snprintf(), but format only knows about %d and %s
+ * works like snprintf(), but format only knows about %d, %x, %u and %s
+ * and for the numbers an optional length like %<len>d. If <len> beginns
+ * with '0' the free space is filled with '0's, otherwise with ' '
*/
@@ -42,7 +44,7 @@
#include <stdarg.h>
#include <string.h>
-static char *itoa(char *buffer, const size_t size, int value)
+static char *itoa(char *buffer, const size_t size, int value, unsigned int fixedlen, unsigned int fill0)
{
char *p;
int sign;
@@ -55,6 +57,8 @@ static char *itoa(char *buffer, const size_t size, int value)
sign = 0;
if (value < 0) {
sign = 1;
+ if (fill0)
+ fixedlen -= 1;
value = -value;
}
@@ -69,14 +73,26 @@ static char *itoa(char *buffer, const size_t size, int value)
value = value / 10;
} while (value != 0 && p > buffer);
- if (sign && p > buffer)
+ if (sign && !fill0 && p > buffer)
+ *--p = '-';
+
+ /* fill fixed length */
+ while (p > buffer && strlen(p) < fixedlen) {
+ if (fill0) {
+ *--p = '0';
+ } else {
+ *--p = ' ';
+ }
+ }
+
+ if (sign && fill0 && p > buffer)
*--p = '-';
return p;
}
-static char *utoa(char *buffer, const size_t size, unsigned int value)
+static char *utoa(char *buffer, const size_t size, unsigned int value, unsigned int fixedlen, unsigned int fill0)
{
char *p;
@@ -95,11 +111,20 @@ static char *utoa(char *buffer, const size_t size, unsigned int value)
value = value / 10;
} while (value != 0 && p > buffer);
+ /* fill fixed length */
+ while (p > buffer && strlen(p) < fixedlen) {
+ if (fill0) {
+ *--p = '0';
+ } else {
+ *--p = ' ';
+ }
+ }
+
return p;
}
-static char *utox(char *buffer, const size_t size, unsigned int value)
+static char *utox(char *buffer, const size_t size, unsigned int value, unsigned int fixedlen, unsigned int fill0)
{
char *p;
int digit;
@@ -120,10 +145,29 @@ static char *utox(char *buffer, const size_t size, unsigned int value)
*--p = (digit < 10 ? '0' : 'a' - 10) + digit;
} while (value != 0 && p > buffer);
+ /* fill fixed length */
+ while (p > buffer && strlen(p) < fixedlen) {
+ if (fill0) {
+ *--p = '0';
+ } else {
+ *--p = ' ';
+ }
+ }
+
return p;
}
+/*!
+ @function qprintf
+ @abstract quick print values into string
+ @discussion similar to snprintf(), but only support for "%s", "%d", "%u", "%x" with optional length for the numbers
+ like "%5d" (filled with ' ') or "%05x" (filled with '0')
+ @param str destination
+ @param size maximum length of destination string
+ @param format (like printf() with reduced number of formats)
+ @result length of produced string
+*/
int qprintf(char *str, const size_t size, const char *format, ...)
{
@@ -145,7 +189,17 @@ int qprintf(char *str, const size_t size, const char *format, ...)
char buf[12], *s;
int d;
unsigned int u;
- switch (*++src) {
+ unsigned int fixedlen = 0;
+ unsigned int fill0 = 0;
+
+ if (*++src == '0')
+ fill0 = 1;
+ while (*src >= '0' && *src <= '9') {
+ fixedlen = fixedlen * 10 + (*src - '0');
+ src++;
+ }
+
+ switch (*src) {
case 's':
src++;
s = va_arg(ap, char *);
@@ -157,7 +211,7 @@ int qprintf(char *str, const size_t size, const char *format, ...)
case 'd':
src++;
d = va_arg(ap, int);
- s = itoa(buf, sizeof(buf), d);
+ s = itoa(buf, sizeof(buf), d, fixedlen, fill0);
while (len < size && *s != '\0') {
len++;
*dst++ = *s++;
@@ -166,7 +220,7 @@ int qprintf(char *str, const size_t size, const char *format, ...)
case 'u':
src++;
u = va_arg(ap, unsigned int);
- s = utoa(buf, sizeof(buf), u);
+ s = utoa(buf, sizeof(buf), u, fixedlen, fill0);
while (len < size - 1 && *s != '\0') {
len++;
*dst++ = *s++;
@@ -175,7 +229,7 @@ int qprintf(char *str, const size_t size, const char *format, ...)
case 'x':
src++;
u = va_arg(ap, unsigned int);
- s = utox(buf, sizeof(buf), u);
+ s = utox(buf, sizeof(buf), u, fixedlen, fill0);
while (len < size - 1 && *s != '\0') {
len++;
*dst++ = *s++;