blob: 39bf321ce3fae471221eac93970fa049baea5bac [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>
30
Damien George51dfcb42015-01-01 20:27:54 +000031#include "py/pfenv.h"
Paul Sokolovskycb66f412014-07-13 23:07:42 +030032
33#if MICROPY_PY_BUILTINS_FLOAT
Damien George51dfcb42015-01-01 20:27:54 +000034#include "py/formatfloat.h"
Paul Sokolovskycb66f412014-07-13 23:07:42 +030035#endif
36
Damien George7eb23172014-12-10 22:11:01 +000037int pfenv_vprintf(const pfenv_t *pfenv, const char *fmt, va_list args) {
Paul Sokolovskycb66f412014-07-13 23:07:42 +030038 int chrs = 0;
39 for (;;) {
40 {
41 const char *f = fmt;
42 while (*f != '\0' && *f != '%') {
43 ++f; // XXX UTF8 advance char
44 }
45 if (f > fmt) {
46 pfenv->print_strn(pfenv->data, fmt, f - fmt);
47 chrs += f - fmt;
48 fmt = f;
49 }
50 }
51
52 if (*fmt == '\0') {
53 break;
54 }
55
56 // move past % character
57 ++fmt;
58
59 // parse flags, if they exist
60 int flags = 0;
61 char fill = ' ';
62 while (*fmt != '\0') {
63 if (*fmt == '-') flags |= PF_FLAG_LEFT_ADJUST;
64 else if (*fmt == '+') flags |= PF_FLAG_SHOW_SIGN;
65 else if (*fmt == ' ') flags |= PF_FLAG_SPACE_SIGN;
66 else if (*fmt == '!') flags |= PF_FLAG_NO_TRAILZ;
67 else if (*fmt == '0') {
68 flags |= PF_FLAG_PAD_AFTER_SIGN;
69 fill = '0';
70 } else break;
71 ++fmt;
72 }
73
74 // parse width, if it exists
75 int width = 0;
76 for (; '0' <= *fmt && *fmt <= '9'; ++fmt) {
77 width = width * 10 + *fmt - '0';
78 }
79
80 // parse precision, if it exists
81 int prec = -1;
82 if (*fmt == '.') {
83 ++fmt;
84 if (*fmt == '*') {
85 ++fmt;
86 prec = va_arg(args, int);
87 } else {
88 prec = 0;
89 for (; '0' <= *fmt && *fmt <= '9'; ++fmt) {
90 prec = prec * 10 + *fmt - '0';
91 }
92 }
93 if (prec < 0) {
94 prec = 0;
95 }
96 }
97
98 // parse long specifiers (current not used)
99 //bool long_arg = false;
100 if (*fmt == 'l') {
101 ++fmt;
102 //long_arg = true;
103 }
104
105 if (*fmt == '\0') {
106 break;
107 }
108
109 switch (*fmt) {
110 case 'b':
111 if (va_arg(args, int)) {
112 chrs += pfenv_print_strn(pfenv, "true", 4, flags, fill, width);
113 } else {
114 chrs += pfenv_print_strn(pfenv, "false", 5, flags, fill, width);
115 }
116 break;
117 case 'c':
118 {
119 char str = va_arg(args, int);
120 chrs += pfenv_print_strn(pfenv, &str, 1, flags, fill, width);
121 break;
122 }
123 case 's':
124 {
125 const char *str = va_arg(args, const char*);
126 if (str) {
127 if (prec < 0) {
128 prec = strlen(str);
129 }
130 chrs += pfenv_print_strn(pfenv, str, prec, flags, fill, width);
131 } else {
132 chrs += pfenv_print_strn(pfenv, "(null)", 6, flags, fill, width);
133 }
134 break;
135 }
136 case 'u':
137 chrs += pfenv_print_int(pfenv, va_arg(args, int), 0, 10, 'a', flags, fill, width);
138 break;
139 case 'd':
140 chrs += pfenv_print_int(pfenv, va_arg(args, int), 1, 10, 'a', flags, fill, width);
141 break;
142 case 'x':
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300143 chrs += pfenv_print_int(pfenv, va_arg(args, int), 0, 16, 'a', flags, fill, width);
144 break;
145 case 'X':
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300146 chrs += pfenv_print_int(pfenv, va_arg(args, int), 0, 16, 'A', flags, fill, width);
147 break;
Paul Sokolovskydf732bb2014-11-26 21:17:16 +0200148 case 'p':
149 case 'P': // don't bother to handle upcase for 'P'
150 chrs += pfenv_print_int(pfenv, va_arg(args, mp_uint_t), 0, 16, 'a', flags, fill, width);
151 break;
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300152#if MICROPY_PY_BUILTINS_FLOAT
153 case 'e':
154 case 'E':
155 case 'f':
156 case 'F':
157 case 'g':
158 case 'G':
159 {
160#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
161 mp_float_t f = va_arg(args, double);
162 chrs += pfenv_print_float(pfenv, f, *fmt, flags, fill, width, prec);
163#elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
164 // Currently pfenv_print_float uses snprintf, but snprintf
165 // itself may be implemented in terms of pfenv_vprintf() for
166 // some ports. So, for extra caution, this case is handled
167 // with assert below. Note that currently ports which
168 // use MICROPY_FLOAT_IMPL_DOUBLE, don't call pfenv_vprintf()
169 // with float format specifier at all.
170 // TODO: resolve this completely
171 assert(0);
172//#error Calling pfenv_print_float with double not supported from within printf
173#else
174#error Unknown MICROPY FLOAT IMPL
175#endif
176 break;
177 }
178#endif
179 default:
180 pfenv->print_strn(pfenv->data, fmt, 1);
181 chrs += 1;
182 break;
183 }
184 ++fmt;
185 }
186 return chrs;
187}
188
189int pfenv_printf(const pfenv_t *pfenv, const char *fmt, ...) {
190 va_list ap;
191 va_start(ap, fmt);
192 int ret = pfenv_vprintf(pfenv, fmt, ap);
193 va_end(ap);
194 return ret;
195}