1
/* $Id: qprintf.c 918 2008-12-31 06:07:29Z michael $
2
* $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/qprintf.c $
4
* simple but quick snprintf() replacement
6
* Copyright (C) 2004 Michael Reinelt <michael@reinelt.co.at>
7
* Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
9
* derived from a patch from Martin Hejl which is
10
* Copyright (C) 2003 Martin Hejl (martin@hejl.de)
12
* This file is part of LCD4Linux.
14
* LCD4Linux is free software; you can redistribute it and/or modify
15
* it under the terms of the GNU General Public License as published by
16
* the Free Software Foundation; either version 2, or (at your option)
19
* LCD4Linux is distributed in the hope that it will be useful,
20
* but WITHOUT ANY WARRANTY; without even the implied warranty of
21
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
* GNU General Public License for more details.
24
* You should have received a copy of the GNU General Public License
25
* along with this program; if not, write to the Free Software
26
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33
* int qprintf(char *str, size_t size, const char *format, ...)
34
* works like snprintf(), but format only knows about %d, %x, %u and %s
35
* and for the numbers an optional length like %<len>d. If <len> beginns
36
* with '0' the free space is filled with '0's, otherwise with ' '
47
static char *itoa(char *buffer, const size_t size, int value, unsigned int fixedlen, unsigned int fill0)
53
if (buffer == NULL || size < 2)
56
/* remember sign of value */
65
/* p points to last char */
66
p = buffer + size - 1;
68
/* set terminating zero */
72
*--p = value % 10 + '0';
74
} while (value != 0 && p > buffer);
76
if (sign && !fill0 && p > buffer)
79
/* fill fixed length */
80
while (p > buffer && strlen(p) < fixedlen) {
88
if (sign && fill0 && p > buffer)
95
static char *utoa(char *buffer, const size_t size, unsigned int value, unsigned int fixedlen, unsigned int fill0)
100
if (buffer == NULL || size < 2)
103
/* p points to last char */
104
p = buffer + size - 1;
106
/* set terminating zero */
110
*--p = value % 10 + '0';
112
} while (value != 0 && p > buffer);
114
/* fill fixed length */
115
while (p > buffer && strlen(p) < fixedlen) {
127
static char *utox(char *buffer, const size_t size, unsigned int value, unsigned int fixedlen, unsigned int fill0)
133
if (buffer == NULL || size < 2)
136
/* p points to last char */
137
p = buffer + size - 1;
139
/* set terminating zero */
145
*--p = (digit < 10 ? '0' : 'a' - 10) + digit;
146
} while (value != 0 && p > buffer);
148
/* fill fixed length */
149
while (p > buffer && strlen(p) < fixedlen) {
163
@abstract quick print values into string
164
@discussion similar to snprintf(), but only support for "%s", "%d", "%u", "%x" with optional length for the numbers
165
like "%5d" (filled with ' ') or "%05x" (filled with '0')
166
@param str destination
167
@param size maximum length of destination string
168
@param format (like printf() with reduced number of formats)
169
@result length of produced string
171
int qprintf(char *str, const size_t size, const char *format, ...)
183
va_start(ap, format);
185
/* use size-1 for terminating zero */
186
while (len < size - 1) {
192
unsigned int fixedlen = 0;
193
unsigned int fill0 = 0;
197
while (*src >= '0' && *src <= '9') {
198
fixedlen = fixedlen * 10 + (*src - '0');
205
s = va_arg(ap, char *);
206
while (len < size - 1 && *s != '\0') {
214
s = itoa(buf, sizeof(buf), d, fixedlen, fill0);
215
while (len < size && *s != '\0') {
222
u = va_arg(ap, unsigned int);
223
s = utoa(buf, sizeof(buf), u, fixedlen, fill0);
224
while (len < size - 1 && *s != '\0') {
231
u = va_arg(ap, unsigned int);
232
s = utox(buf, sizeof(buf), u, fixedlen, fill0);
233
while (len < size - 1 && *s != '\0') {
252
/* enforce terminating zero */
253
if (len >= size - 1 && *(dst - 1) != '\0') {
258
/* do not count terminating zero */