blob: d4c5fc9df567fabcc458e2e15c67139c68b67109 [file] [log] [blame]
Paul Sokolovskycb66f412014-07-13 23:07:42 +03001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
27#include <assert.h>
Paul Sokolovskycb66f412014-07-13 23:07:42 +030028#include <string.h>
29#include <stdarg.h>
Damien Georgecd342072015-01-12 22:30:49 +000030#include <stdio.h>
Paul Sokolovskycb66f412014-07-13 23:07:42 +030031
Damien George51dfcb42015-01-01 20:27:54 +000032#include "py/pfenv.h"
Paul Sokolovskycb66f412014-07-13 23:07:42 +030033
34#if MICROPY_PY_BUILTINS_FLOAT
Damien George51dfcb42015-01-01 20:27:54 +000035#include "py/formatfloat.h"
Paul Sokolovskycb66f412014-07-13 23:07:42 +030036#endif
37
Damien George7eb23172014-12-10 22:11:01 +000038int pfenv_vprintf(const pfenv_t *pfenv, const char *fmt, va_list args) {
Paul Sokolovskycb66f412014-07-13 23:07:42 +030039 int chrs = 0;
40 for (;;) {
41 {
42 const char *f = fmt;
43 while (*f != '\0' && *f != '%') {
44 ++f; // XXX UTF8 advance char
45 }
46 if (f > fmt) {
47 pfenv->print_strn(pfenv->data, fmt, f - fmt);
48 chrs += f - fmt;
49 fmt = f;
50 }
51 }
52
53 if (*fmt == '\0') {
54 break;
55 }
56
57 // move past % character
58 ++fmt;
59
60 // parse flags, if they exist
61 int flags = 0;
62 char fill = ' ';
63 while (*fmt != '\0') {
64 if (*fmt == '-') flags |= PF_FLAG_LEFT_ADJUST;
65 else if (*fmt == '+') flags |= PF_FLAG_SHOW_SIGN;
66 else if (*fmt == ' ') flags |= PF_FLAG_SPACE_SIGN;
67 else if (*fmt == '!') flags |= PF_FLAG_NO_TRAILZ;
68 else if (*fmt == '0') {
69 flags |= PF_FLAG_PAD_AFTER_SIGN;
70 fill = '0';
71 } else break;
72 ++fmt;
73 }
74
75 // parse width, if it exists
76 int width = 0;
77 for (; '0' <= *fmt && *fmt <= '9'; ++fmt) {
78 width = width * 10 + *fmt - '0';
79 }
80
81 // parse precision, if it exists
82 int prec = -1;
83 if (*fmt == '.') {
84 ++fmt;
85 if (*fmt == '*') {
86 ++fmt;
87 prec = va_arg(args, int);
88 } else {
89 prec = 0;
90 for (; '0' <= *fmt && *fmt <= '9'; ++fmt) {
91 prec = prec * 10 + *fmt - '0';
92 }
93 }
94 if (prec < 0) {
95 prec = 0;
96 }
97 }
98
99 // parse long specifiers (current not used)
100 //bool long_arg = false;
101 if (*fmt == 'l') {
102 ++fmt;
103 //long_arg = true;
104 }
105
106 if (*fmt == '\0') {
107 break;
108 }
109
110 switch (*fmt) {
111 case 'b':
112 if (va_arg(args, int)) {
113 chrs += pfenv_print_strn(pfenv, "true", 4, flags, fill, width);
114 } else {
115 chrs += pfenv_print_strn(pfenv, "false", 5, flags, fill, width);
116 }
117 break;
118 case 'c':
119 {
120 char str = va_arg(args, int);
121 chrs += pfenv_print_strn(pfenv, &str, 1, flags, fill, width);
122 break;
123 }
124 case 's':
125 {
126 const char *str = va_arg(args, const char*);
127 if (str) {
128 if (prec < 0) {
129 prec = strlen(str);
130 }
131 chrs += pfenv_print_strn(pfenv, str, prec, flags, fill, width);
132 } else {
133 chrs += pfenv_print_strn(pfenv, "(null)", 6, flags, fill, width);
134 }
135 break;
136 }
137 case 'u':
138 chrs += pfenv_print_int(pfenv, va_arg(args, int), 0, 10, 'a', flags, fill, width);
139 break;
140 case 'd':
141 chrs += pfenv_print_int(pfenv, va_arg(args, int), 1, 10, 'a', flags, fill, width);
142 break;
143 case 'x':
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300144 chrs += pfenv_print_int(pfenv, va_arg(args, int), 0, 16, 'a', flags, fill, width);
145 break;
146 case 'X':
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300147 chrs += pfenv_print_int(pfenv, va_arg(args, int), 0, 16, 'A', flags, fill, width);
148 break;
Paul Sokolovskydf732bb2014-11-26 21:17:16 +0200149 case 'p':
150 case 'P': // don't bother to handle upcase for 'P'
151 chrs += pfenv_print_int(pfenv, va_arg(args, mp_uint_t), 0, 16, 'a', flags, fill, width);
152 break;
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300153#if MICROPY_PY_BUILTINS_FLOAT
154 case 'e':
155 case 'E':
156 case 'f':
157 case 'F':
158 case 'g':
159 case 'G':
160 {
161#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
162 mp_float_t f = va_arg(args, double);
163 chrs += pfenv_print_float(pfenv, f, *fmt, flags, fill, width, prec);
164#elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
165 // Currently pfenv_print_float uses snprintf, but snprintf
166 // itself may be implemented in terms of pfenv_vprintf() for
167 // some ports. So, for extra caution, this case is handled
168 // with assert below. Note that currently ports which
169 // use MICROPY_FLOAT_IMPL_DOUBLE, don't call pfenv_vprintf()
170 // with float format specifier at all.
171 // TODO: resolve this completely
172 assert(0);
173//#error Calling pfenv_print_float with double not supported from within printf
174#else
175#error Unknown MICROPY FLOAT IMPL
176#endif
177 break;
178 }
179#endif
180 default:
181 pfenv->print_strn(pfenv->data, fmt, 1);
182 chrs += 1;
183 break;
184 }
185 ++fmt;
186 }
187 return chrs;
188}
189
190int pfenv_printf(const pfenv_t *pfenv, const char *fmt, ...) {
191 va_list ap;
192 va_start(ap, fmt);
193 int ret = pfenv_vprintf(pfenv, fmt, ap);
194 va_end(ap);
195 return ret;
196}
Damien Georgecd342072015-01-12 22:30:49 +0000197
198void printf_wrapper(void *env, const char *fmt, ...) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000199 (void)env;
Damien Georgecd342072015-01-12 22:30:49 +0000200 va_list args;
201 va_start(args, fmt);
202 vprintf(fmt, args);
203 va_end(args);
204}