Skip to content

Commit

Permalink
format characters in qprintf() with optional length (r821) ported fro…
Browse files Browse the repository at this point in the history
…m volker_dev

git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@918 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
  • Loading branch information
michael committed Dec 31, 2008
1 parent 503d5e3 commit fff158d
Showing 1 changed file with 63 additions and 9 deletions.
72 changes: 63 additions & 9 deletions qprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ' '
*/


Expand All @@ -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;
Expand All @@ -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;
}

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

Expand All @@ -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;
Expand All @@ -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, ...)
{

Expand All @@ -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 *);
Expand All @@ -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++;
Expand All @@ -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++;
Expand All @@ -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++;
Expand Down

0 comments on commit fff158d

Please sign in to comment.