~siretart/lcd4linux/debian

« back to all changes in this revision

Viewing changes to qprintf.c

  • Committer: Reinhard Tartler
  • Date: 2011-04-27 17:24:15 UTC
  • mto: This revision was merged to the branch mainline in revision 750.
  • Revision ID: siretart@tauware.de-20110427172415-6n4aptmvmz0eztvm
Tags: upstream-0.11.0~svn1143
ImportĀ upstreamĀ versionĀ 0.11.0~svn1143

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: qprintf.c 918 2008-12-31 06:07:29Z michael $
 
2
 * $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/qprintf.c $
 
3
 *
 
4
 * simple but quick snprintf() replacement
 
5
 *
 
6
 * Copyright (C) 2004 Michael Reinelt <michael@reinelt.co.at>
 
7
 * Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
 
8
 *
 
9
 * derived from a patch from Martin Hejl which is
 
10
 * Copyright (C) 2003 Martin Hejl (martin@hejl.de)
 
11
 *
 
12
 * This file is part of LCD4Linux.
 
13
 *
 
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)
 
17
 * any later version.
 
18
 *
 
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.
 
23
 *
 
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.
 
27
 *
 
28
 */
 
29
 
 
30
/* 
 
31
 * exported functions:
 
32
 * 
 
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 ' '
 
37
 */
 
38
 
 
39
 
 
40
#include "config.h"
 
41
 
 
42
#include <stdlib.h>
 
43
#include <stdio.h>
 
44
#include <stdarg.h>
 
45
#include <string.h>
 
46
 
 
47
static char *itoa(char *buffer, const size_t size, int value, unsigned int fixedlen, unsigned int fill0)
 
48
{
 
49
    char *p;
 
50
    int sign;
 
51
 
 
52
    /* sanity checks */
 
53
    if (buffer == NULL || size < 2)
 
54
        return (NULL);
 
55
 
 
56
    /* remember sign of value */
 
57
    sign = 0;
 
58
    if (value < 0) {
 
59
        sign = 1;
 
60
        if (fill0)
 
61
            fixedlen -= 1;
 
62
        value = -value;
 
63
    }
 
64
 
 
65
    /* p points to last char */
 
66
    p = buffer + size - 1;
 
67
 
 
68
    /* set terminating zero */
 
69
    *p = '\0';
 
70
 
 
71
    do {
 
72
        *--p = value % 10 + '0';
 
73
        value = value / 10;
 
74
    } while (value != 0 && p > buffer);
 
75
 
 
76
    if (sign && !fill0 && p > buffer)
 
77
        *--p = '-';
 
78
 
 
79
    /* fill fixed length */
 
80
    while (p > buffer && strlen(p) < fixedlen) {
 
81
        if (fill0) {
 
82
            *--p = '0';
 
83
        } else {
 
84
            *--p = ' ';
 
85
        }
 
86
    }
 
87
 
 
88
    if (sign && fill0 && p > buffer)
 
89
        *--p = '-';
 
90
 
 
91
    return p;
 
92
}
 
93
 
 
94
 
 
95
static char *utoa(char *buffer, const size_t size, unsigned int value, unsigned int fixedlen, unsigned int fill0)
 
96
{
 
97
    char *p;
 
98
 
 
99
    /* sanity checks */
 
100
    if (buffer == NULL || size < 2)
 
101
        return (NULL);
 
102
 
 
103
    /* p points to last char */
 
104
    p = buffer + size - 1;
 
105
 
 
106
    /* set terminating zero */
 
107
    *p = '\0';
 
108
 
 
109
    do {
 
110
        *--p = value % 10 + '0';
 
111
        value = value / 10;
 
112
    } while (value != 0 && p > buffer);
 
113
 
 
114
    /* fill fixed length */
 
115
    while (p > buffer && strlen(p) < fixedlen) {
 
116
        if (fill0) {
 
117
            *--p = '0';
 
118
        } else {
 
119
            *--p = ' ';
 
120
        }
 
121
    }
 
122
 
 
123
    return p;
 
124
}
 
125
 
 
126
 
 
127
static char *utox(char *buffer, const size_t size, unsigned int value, unsigned int fixedlen, unsigned int fill0)
 
128
{
 
129
    char *p;
 
130
    int digit;
 
131
 
 
132
    /* sanity checks */
 
133
    if (buffer == NULL || size < 2)
 
134
        return (NULL);
 
135
 
 
136
    /* p points to last char */
 
137
    p = buffer + size - 1;
 
138
 
 
139
    /* set terminating zero */
 
140
    *p = '\0';
 
141
 
 
142
    do {
 
143
        digit = value % 16;
 
144
        value = value / 16;
 
145
        *--p = (digit < 10 ? '0' : 'a' - 10) + digit;
 
146
    } while (value != 0 && p > buffer);
 
147
 
 
148
    /* fill fixed length */
 
149
    while (p > buffer && strlen(p) < fixedlen) {
 
150
        if (fill0) {
 
151
            *--p = '0';
 
152
        } else {
 
153
            *--p = ' ';
 
154
        }
 
155
    }
 
156
 
 
157
    return p;
 
158
}
 
159
 
 
160
 
 
161
/*!
 
162
    @function   qprintf
 
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
 
170
*/
 
171
int qprintf(char *str, const size_t size, const char *format, ...)
 
172
{
 
173
 
 
174
    va_list ap;
 
175
    const char *src;
 
176
    char *dst;
 
177
    unsigned int len;
 
178
 
 
179
    src = format;
 
180
    dst = str;
 
181
    len = 0;
 
182
 
 
183
    va_start(ap, format);
 
184
 
 
185
    /* use size-1 for terminating zero */
 
186
    while (len < size - 1) {
 
187
 
 
188
        if (*src == '%') {
 
189
            char buf[12], *s;
 
190
            int d;
 
191
            unsigned int u;
 
192
            unsigned int fixedlen = 0;
 
193
            unsigned int fill0 = 0;
 
194
 
 
195
            if (*++src == '0')
 
196
                fill0 = 1;
 
197
            while (*src >= '0' && *src <= '9') {
 
198
                fixedlen = fixedlen * 10 + (*src - '0');
 
199
                src++;
 
200
            }
 
201
 
 
202
            switch (*src) {
 
203
            case 's':
 
204
                src++;
 
205
                s = va_arg(ap, char *);
 
206
                while (len < size - 1 && *s != '\0') {
 
207
                    len++;
 
208
                    *dst++ = *s++;
 
209
                }
 
210
                break;
 
211
            case 'd':
 
212
                src++;
 
213
                d = va_arg(ap, int);
 
214
                s = itoa(buf, sizeof(buf), d, fixedlen, fill0);
 
215
                while (len < size && *s != '\0') {
 
216
                    len++;
 
217
                    *dst++ = *s++;
 
218
                }
 
219
                break;
 
220
            case 'u':
 
221
                src++;
 
222
                u = va_arg(ap, unsigned int);
 
223
                s = utoa(buf, sizeof(buf), u, fixedlen, fill0);
 
224
                while (len < size - 1 && *s != '\0') {
 
225
                    len++;
 
226
                    *dst++ = *s++;
 
227
                }
 
228
                break;
 
229
            case 'x':
 
230
                src++;
 
231
                u = va_arg(ap, unsigned int);
 
232
                s = utox(buf, sizeof(buf), u, fixedlen, fill0);
 
233
                while (len < size - 1 && *s != '\0') {
 
234
                    len++;
 
235
                    *dst++ = *s++;
 
236
                }
 
237
                break;
 
238
            default:
 
239
                len++;
 
240
                *dst++ = '%';
 
241
            }
 
242
        } else {
 
243
            len++;
 
244
            *dst++ = *src;
 
245
            if (*src++ == '\0')
 
246
                break;
 
247
        }
 
248
    }
 
249
 
 
250
    va_end(ap);
 
251
 
 
252
    /* enforce terminating zero */
 
253
    if (len >= size - 1 && *(dst - 1) != '\0') {
 
254
        len++;
 
255
        *dst = '\0';
 
256
    }
 
257
 
 
258
    /* do not count terminating zero */
 
259
    return len - 1;
 
260
}