Fahad Mirza
/
Nucleo_HXC900
A demo application for HXC900 LoRaWAN module using Nucleo-L053R8.
Utilities/tiny_vsnprintf.c@5:53302861bfea, 2018-07-16 (annotated)
- Committer:
- fahadmirza
- Date:
- Mon Jul 16 20:12:42 2018 +0000
- Revision:
- 5:53302861bfea
- Parent:
- tiny_vsnprintf.c@0:a0c5877bd360
Updated directories;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fahadmirza | 0:a0c5877bd360 | 1 | /****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 2 | * @file tiny_vsnprintf.c |
fahadmirza | 0:a0c5877bd360 | 3 | * @author MCD Application Team |
fahadmirza | 0:a0c5877bd360 | 4 | * @version V1.1.4 |
fahadmirza | 0:a0c5877bd360 | 5 | * @date 08-January-2018 |
fahadmirza | 0:a0c5877bd360 | 6 | * @brief Tiny implementation of vsnprintf like function |
fahadmirza | 0:a0c5877bd360 | 7 | ****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 8 | * @attention |
fahadmirza | 0:a0c5877bd360 | 9 | * |
fahadmirza | 0:a0c5877bd360 | 10 | * <h2><center>© Copyright (c) 2017 STMicroelectronics International N.V. |
fahadmirza | 0:a0c5877bd360 | 11 | * All rights reserved.</center></h2> |
fahadmirza | 0:a0c5877bd360 | 12 | * |
fahadmirza | 0:a0c5877bd360 | 13 | * Copyright (c) 1990, 1993 |
fahadmirza | 0:a0c5877bd360 | 14 | * The Regents of the University of California. All rights reserved. |
fahadmirza | 0:a0c5877bd360 | 15 | * |
fahadmirza | 0:a0c5877bd360 | 16 | * This code is derived from software contributed to Berkeley by |
fahadmirza | 0:a0c5877bd360 | 17 | * Chris Torek. |
fahadmirza | 0:a0c5877bd360 | 18 | * Redistribution and use in source and binary forms, with or without |
fahadmirza | 0:a0c5877bd360 | 19 | * modification, are permitted, provided that the following conditions are met: |
fahadmirza | 0:a0c5877bd360 | 20 | * |
fahadmirza | 0:a0c5877bd360 | 21 | * 1. Redistribution of source code must retain the above copyright notice, |
fahadmirza | 0:a0c5877bd360 | 22 | * this list of conditions and the following disclaimer. |
fahadmirza | 0:a0c5877bd360 | 23 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
fahadmirza | 0:a0c5877bd360 | 24 | * this list of conditions and the following disclaimer in the documentation |
fahadmirza | 0:a0c5877bd360 | 25 | * and/or other materials provided with the distribution. |
fahadmirza | 0:a0c5877bd360 | 26 | * 3. Neither the name of STMicroelectronics nor the names of other |
fahadmirza | 0:a0c5877bd360 | 27 | * contributors to this software may be used to endorse or promote products |
fahadmirza | 0:a0c5877bd360 | 28 | * derived from this software without specific written permission. |
fahadmirza | 0:a0c5877bd360 | 29 | * 4. This software, including modifications and/or derivative works of this |
fahadmirza | 0:a0c5877bd360 | 30 | * software, must execute solely and exclusively on microcontroller or |
fahadmirza | 0:a0c5877bd360 | 31 | * microprocessor devices manufactured by or for STMicroelectronics. |
fahadmirza | 0:a0c5877bd360 | 32 | * 5. Redistribution and use of this software other than as permitted under |
fahadmirza | 0:a0c5877bd360 | 33 | * this license is void and will automatically terminate your rights under |
fahadmirza | 0:a0c5877bd360 | 34 | * this license. |
fahadmirza | 0:a0c5877bd360 | 35 | * |
fahadmirza | 0:a0c5877bd360 | 36 | * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" |
fahadmirza | 0:a0c5877bd360 | 37 | * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT |
fahadmirza | 0:a0c5877bd360 | 38 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
fahadmirza | 0:a0c5877bd360 | 39 | * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY |
fahadmirza | 0:a0c5877bd360 | 40 | * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT |
fahadmirza | 0:a0c5877bd360 | 41 | * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
fahadmirza | 0:a0c5877bd360 | 42 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
fahadmirza | 0:a0c5877bd360 | 43 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
fahadmirza | 0:a0c5877bd360 | 44 | * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
fahadmirza | 0:a0c5877bd360 | 45 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
fahadmirza | 0:a0c5877bd360 | 46 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
fahadmirza | 0:a0c5877bd360 | 47 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
fahadmirza | 0:a0c5877bd360 | 48 | * |
fahadmirza | 0:a0c5877bd360 | 49 | ****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 50 | */ |
fahadmirza | 0:a0c5877bd360 | 51 | |
fahadmirza | 0:a0c5877bd360 | 52 | /* File : barebones/ee_printf.c |
fahadmirza | 0:a0c5877bd360 | 53 | This file contains an implementation of ee_printf that only requires a method to output a char to a UART without pulling in library code. |
fahadmirza | 0:a0c5877bd360 | 54 | This code is based on a file that contains the following: |
fahadmirza | 0:a0c5877bd360 | 55 | Copyright (C) 2002 Michael Ringgaard. All rights reserved. |
fahadmirza | 0:a0c5877bd360 | 56 | Redistribution and use in source and binary forms, with or without |
fahadmirza | 0:a0c5877bd360 | 57 | modification, are permitted provided that the following conditions |
fahadmirza | 0:a0c5877bd360 | 58 | are met: |
fahadmirza | 0:a0c5877bd360 | 59 | |
fahadmirza | 0:a0c5877bd360 | 60 | 1. Redistributions of source code must retain the above copyright |
fahadmirza | 0:a0c5877bd360 | 61 | notice, this list of conditions and the following disclaimer. |
fahadmirza | 0:a0c5877bd360 | 62 | 2. Redistributions in binary form must reproduce the above copyright |
fahadmirza | 0:a0c5877bd360 | 63 | notice, this list of conditions and the following disclaimer in the |
fahadmirza | 0:a0c5877bd360 | 64 | documentation and/or other materials provided with the distribution. |
fahadmirza | 0:a0c5877bd360 | 65 | 3. Neither the name of the project nor the names of its contributors |
fahadmirza | 0:a0c5877bd360 | 66 | may be used to endorse or promote products derived from this software |
fahadmirza | 0:a0c5877bd360 | 67 | without specific prior written permission. |
fahadmirza | 0:a0c5877bd360 | 68 | |
fahadmirza | 0:a0c5877bd360 | 69 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
fahadmirza | 0:a0c5877bd360 | 70 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
fahadmirza | 0:a0c5877bd360 | 71 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
fahadmirza | 0:a0c5877bd360 | 72 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
fahadmirza | 0:a0c5877bd360 | 73 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
fahadmirza | 0:a0c5877bd360 | 74 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
fahadmirza | 0:a0c5877bd360 | 75 | OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
fahadmirza | 0:a0c5877bd360 | 76 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
fahadmirza | 0:a0c5877bd360 | 77 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
fahadmirza | 0:a0c5877bd360 | 78 | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
fahadmirza | 0:a0c5877bd360 | 79 | SUCH DAMAGE. |
fahadmirza | 0:a0c5877bd360 | 80 | */ |
fahadmirza | 0:a0c5877bd360 | 81 | |
fahadmirza | 0:a0c5877bd360 | 82 | /* |
fahadmirza | 0:a0c5877bd360 | 83 | * Following implementation is adapted from original one |
fahadmirza | 0:a0c5877bd360 | 84 | * https://github.com/jpbonn/coremark_lm32/blob/master/ee_printf.c |
fahadmirza | 0:a0c5877bd360 | 85 | */ |
fahadmirza | 0:a0c5877bd360 | 86 | |
fahadmirza | 0:a0c5877bd360 | 87 | #define TINY_PRINTF |
fahadmirza | 0:a0c5877bd360 | 88 | |
fahadmirza | 0:a0c5877bd360 | 89 | #include <stdarg.h> |
fahadmirza | 0:a0c5877bd360 | 90 | #include <string.h> |
fahadmirza | 0:a0c5877bd360 | 91 | #include "tiny_vsnprintf.h" |
fahadmirza | 0:a0c5877bd360 | 92 | |
fahadmirza | 0:a0c5877bd360 | 93 | #define ZEROPAD (1<<0) /* Pad with zero */ |
fahadmirza | 0:a0c5877bd360 | 94 | #define SIGN (1<<1) /* Unsigned/signed long */ |
fahadmirza | 0:a0c5877bd360 | 95 | #define UPPERCASE (1<<6) /* 'ABCDEF' */ |
fahadmirza | 0:a0c5877bd360 | 96 | #ifdef TINY_PRINTF |
fahadmirza | 0:a0c5877bd360 | 97 | #else |
fahadmirza | 0:a0c5877bd360 | 98 | #define PLUS (1<<2) /* Show plus */ |
fahadmirza | 0:a0c5877bd360 | 99 | #define HEX_PREP (1<<5) /* 0x */ |
fahadmirza | 0:a0c5877bd360 | 100 | #define SPACE (1<<3) /* Spacer */ |
fahadmirza | 0:a0c5877bd360 | 101 | #define LEFT (1<<4) /* Left justified */ |
fahadmirza | 0:a0c5877bd360 | 102 | #endif |
fahadmirza | 0:a0c5877bd360 | 103 | |
fahadmirza | 0:a0c5877bd360 | 104 | #define is_digit(c) ((c) >= '0' && (c) <= '9') |
fahadmirza | 0:a0c5877bd360 | 105 | |
fahadmirza | 0:a0c5877bd360 | 106 | static char *lower_digits = "0123456789abcdefghijklmnopqrstuvwxyz"; |
fahadmirza | 0:a0c5877bd360 | 107 | static char *upper_digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
fahadmirza | 0:a0c5877bd360 | 108 | #ifdef TINY_PRINTF |
fahadmirza | 0:a0c5877bd360 | 109 | #else |
fahadmirza | 0:a0c5877bd360 | 110 | static size_t strnlen(const char *s, size_t count); |
fahadmirza | 0:a0c5877bd360 | 111 | |
fahadmirza | 0:a0c5877bd360 | 112 | static size_t strnlen(const char *s, size_t count) |
fahadmirza | 0:a0c5877bd360 | 113 | { |
fahadmirza | 0:a0c5877bd360 | 114 | const char *sc; |
fahadmirza | 0:a0c5877bd360 | 115 | for (sc = s; *sc != '\0' && count--; ++sc); |
fahadmirza | 0:a0c5877bd360 | 116 | return sc - s; |
fahadmirza | 0:a0c5877bd360 | 117 | } |
fahadmirza | 0:a0c5877bd360 | 118 | #endif |
fahadmirza | 0:a0c5877bd360 | 119 | |
fahadmirza | 0:a0c5877bd360 | 120 | static int ee_skip_atoi(const char **s) |
fahadmirza | 0:a0c5877bd360 | 121 | { |
fahadmirza | 0:a0c5877bd360 | 122 | int i = 0; |
fahadmirza | 0:a0c5877bd360 | 123 | while (is_digit(**s)) i = i*10 + *((*s)++) - '0'; |
fahadmirza | 0:a0c5877bd360 | 124 | return i; |
fahadmirza | 0:a0c5877bd360 | 125 | } |
fahadmirza | 0:a0c5877bd360 | 126 | |
fahadmirza | 0:a0c5877bd360 | 127 | #define ASSIGN_STR(_c) do { *str++ = (_c); max_size--; if (max_size == 0) return str; } while (0) |
fahadmirza | 0:a0c5877bd360 | 128 | |
fahadmirza | 0:a0c5877bd360 | 129 | static char *ee_number(char *str, int max_size, long num, int base, int size, int precision, int type) |
fahadmirza | 0:a0c5877bd360 | 130 | { |
fahadmirza | 0:a0c5877bd360 | 131 | char c; |
fahadmirza | 0:a0c5877bd360 | 132 | char sign, tmp[66]; |
fahadmirza | 0:a0c5877bd360 | 133 | char *dig = lower_digits; |
fahadmirza | 0:a0c5877bd360 | 134 | int i; |
fahadmirza | 0:a0c5877bd360 | 135 | |
fahadmirza | 0:a0c5877bd360 | 136 | if (type & UPPERCASE) dig = upper_digits; |
fahadmirza | 0:a0c5877bd360 | 137 | #ifdef TINY_PRINTF |
fahadmirza | 0:a0c5877bd360 | 138 | #else |
fahadmirza | 0:a0c5877bd360 | 139 | if (type & LEFT) type &= ~ZEROPAD; |
fahadmirza | 0:a0c5877bd360 | 140 | #endif |
fahadmirza | 0:a0c5877bd360 | 141 | if (base < 2 || base > 36) return 0; |
fahadmirza | 0:a0c5877bd360 | 142 | |
fahadmirza | 0:a0c5877bd360 | 143 | c = (type & ZEROPAD) ? '0' : ' '; |
fahadmirza | 0:a0c5877bd360 | 144 | sign = 0; |
fahadmirza | 0:a0c5877bd360 | 145 | if (type & SIGN) |
fahadmirza | 0:a0c5877bd360 | 146 | { |
fahadmirza | 0:a0c5877bd360 | 147 | if (num < 0) |
fahadmirza | 0:a0c5877bd360 | 148 | { |
fahadmirza | 0:a0c5877bd360 | 149 | sign = '-'; |
fahadmirza | 0:a0c5877bd360 | 150 | num = -num; |
fahadmirza | 0:a0c5877bd360 | 151 | size--; |
fahadmirza | 0:a0c5877bd360 | 152 | } |
fahadmirza | 0:a0c5877bd360 | 153 | #ifdef TINY_PRINTF |
fahadmirza | 0:a0c5877bd360 | 154 | #else |
fahadmirza | 0:a0c5877bd360 | 155 | else if (type & PLUS) |
fahadmirza | 0:a0c5877bd360 | 156 | { |
fahadmirza | 0:a0c5877bd360 | 157 | sign = '+'; |
fahadmirza | 0:a0c5877bd360 | 158 | size--; |
fahadmirza | 0:a0c5877bd360 | 159 | } |
fahadmirza | 0:a0c5877bd360 | 160 | else if (type & SPACE) |
fahadmirza | 0:a0c5877bd360 | 161 | { |
fahadmirza | 0:a0c5877bd360 | 162 | sign = ' '; |
fahadmirza | 0:a0c5877bd360 | 163 | size--; |
fahadmirza | 0:a0c5877bd360 | 164 | } |
fahadmirza | 0:a0c5877bd360 | 165 | #endif |
fahadmirza | 0:a0c5877bd360 | 166 | } |
fahadmirza | 0:a0c5877bd360 | 167 | |
fahadmirza | 0:a0c5877bd360 | 168 | #ifdef TINY_PRINTF |
fahadmirza | 0:a0c5877bd360 | 169 | #else |
fahadmirza | 0:a0c5877bd360 | 170 | if (type & HEX_PREP) |
fahadmirza | 0:a0c5877bd360 | 171 | { |
fahadmirza | 0:a0c5877bd360 | 172 | if (base == 16) |
fahadmirza | 0:a0c5877bd360 | 173 | size -= 2; |
fahadmirza | 0:a0c5877bd360 | 174 | else if (base == 8) |
fahadmirza | 0:a0c5877bd360 | 175 | size--; |
fahadmirza | 0:a0c5877bd360 | 176 | } |
fahadmirza | 0:a0c5877bd360 | 177 | #endif |
fahadmirza | 0:a0c5877bd360 | 178 | |
fahadmirza | 0:a0c5877bd360 | 179 | i = 0; |
fahadmirza | 0:a0c5877bd360 | 180 | |
fahadmirza | 0:a0c5877bd360 | 181 | if (num == 0) |
fahadmirza | 0:a0c5877bd360 | 182 | tmp[i++] = '0'; |
fahadmirza | 0:a0c5877bd360 | 183 | else |
fahadmirza | 0:a0c5877bd360 | 184 | { |
fahadmirza | 0:a0c5877bd360 | 185 | while (num != 0) |
fahadmirza | 0:a0c5877bd360 | 186 | { |
fahadmirza | 0:a0c5877bd360 | 187 | tmp[i++] = dig[((unsigned long) num) % (unsigned) base]; |
fahadmirza | 0:a0c5877bd360 | 188 | num = ((unsigned long) num) / (unsigned) base; |
fahadmirza | 0:a0c5877bd360 | 189 | } |
fahadmirza | 0:a0c5877bd360 | 190 | } |
fahadmirza | 0:a0c5877bd360 | 191 | |
fahadmirza | 0:a0c5877bd360 | 192 | if (i > precision) precision = i; |
fahadmirza | 0:a0c5877bd360 | 193 | size -= precision; |
fahadmirza | 0:a0c5877bd360 | 194 | if (!(type & (ZEROPAD /* TINY option | LEFT */))) while (size-- > 0) ASSIGN_STR(' '); |
fahadmirza | 0:a0c5877bd360 | 195 | if (sign) ASSIGN_STR(sign); |
fahadmirza | 0:a0c5877bd360 | 196 | |
fahadmirza | 0:a0c5877bd360 | 197 | #ifdef TINY_PRINTF |
fahadmirza | 0:a0c5877bd360 | 198 | #else |
fahadmirza | 0:a0c5877bd360 | 199 | if (type & HEX_PREP) |
fahadmirza | 0:a0c5877bd360 | 200 | { |
fahadmirza | 0:a0c5877bd360 | 201 | if (base == 8) |
fahadmirza | 0:a0c5877bd360 | 202 | ASSIGN_STR('0'); |
fahadmirza | 0:a0c5877bd360 | 203 | else if (base == 16) |
fahadmirza | 0:a0c5877bd360 | 204 | { |
fahadmirza | 0:a0c5877bd360 | 205 | ASSIGN_STR('0'); |
fahadmirza | 0:a0c5877bd360 | 206 | ASSIGN_STR(lower_digits[33]); |
fahadmirza | 0:a0c5877bd360 | 207 | } |
fahadmirza | 0:a0c5877bd360 | 208 | } |
fahadmirza | 0:a0c5877bd360 | 209 | #endif |
fahadmirza | 0:a0c5877bd360 | 210 | |
fahadmirza | 0:a0c5877bd360 | 211 | #ifdef TINY_PRINTF |
fahadmirza | 0:a0c5877bd360 | 212 | while (size-- > 0) ASSIGN_STR(c); |
fahadmirza | 0:a0c5877bd360 | 213 | #else |
fahadmirza | 0:a0c5877bd360 | 214 | if (!(type & LEFT)) while (size-- > 0) ASSIGN_STR(c); |
fahadmirza | 0:a0c5877bd360 | 215 | #endif |
fahadmirza | 0:a0c5877bd360 | 216 | while (i < precision--) ASSIGN_STR('0'); |
fahadmirza | 0:a0c5877bd360 | 217 | while (i-- > 0) ASSIGN_STR(tmp[i]); |
fahadmirza | 0:a0c5877bd360 | 218 | while (size-- > 0) ASSIGN_STR(' '); |
fahadmirza | 0:a0c5877bd360 | 219 | |
fahadmirza | 0:a0c5877bd360 | 220 | return str; |
fahadmirza | 0:a0c5877bd360 | 221 | } |
fahadmirza | 0:a0c5877bd360 | 222 | |
fahadmirza | 0:a0c5877bd360 | 223 | #ifdef TINY_PRINTF |
fahadmirza | 0:a0c5877bd360 | 224 | #else |
fahadmirza | 0:a0c5877bd360 | 225 | static char *eaddr(char *str, unsigned char *addr, int size, int precision, int type) |
fahadmirza | 0:a0c5877bd360 | 226 | { |
fahadmirza | 0:a0c5877bd360 | 227 | char tmp[24]; |
fahadmirza | 0:a0c5877bd360 | 228 | char *dig = lower_digits; |
fahadmirza | 0:a0c5877bd360 | 229 | int i, len; |
fahadmirza | 0:a0c5877bd360 | 230 | |
fahadmirza | 0:a0c5877bd360 | 231 | if (type & UPPERCASE) dig = upper_digits; |
fahadmirza | 0:a0c5877bd360 | 232 | len = 0; |
fahadmirza | 0:a0c5877bd360 | 233 | for (i = 0; i < 6; i++) |
fahadmirza | 0:a0c5877bd360 | 234 | { |
fahadmirza | 0:a0c5877bd360 | 235 | if (i != 0) tmp[len++] = ':'; |
fahadmirza | 0:a0c5877bd360 | 236 | tmp[len++] = dig[addr[i] >> 4]; |
fahadmirza | 0:a0c5877bd360 | 237 | tmp[len++] = dig[addr[i] & 0x0F]; |
fahadmirza | 0:a0c5877bd360 | 238 | } |
fahadmirza | 0:a0c5877bd360 | 239 | |
fahadmirza | 0:a0c5877bd360 | 240 | if (!(type & LEFT)) while (len < size--) *str++ = ' '; |
fahadmirza | 0:a0c5877bd360 | 241 | for (i = 0; i < len; ++i) *str++ = tmp[i]; |
fahadmirza | 0:a0c5877bd360 | 242 | while (len < size--) *str++ = ' '; |
fahadmirza | 0:a0c5877bd360 | 243 | |
fahadmirza | 0:a0c5877bd360 | 244 | return str; |
fahadmirza | 0:a0c5877bd360 | 245 | } |
fahadmirza | 0:a0c5877bd360 | 246 | |
fahadmirza | 0:a0c5877bd360 | 247 | static char *iaddr(char *str, unsigned char *addr, int size, int precision, int type) |
fahadmirza | 0:a0c5877bd360 | 248 | { |
fahadmirza | 0:a0c5877bd360 | 249 | char tmp[24]; |
fahadmirza | 0:a0c5877bd360 | 250 | int i, n, len; |
fahadmirza | 0:a0c5877bd360 | 251 | |
fahadmirza | 0:a0c5877bd360 | 252 | len = 0; |
fahadmirza | 0:a0c5877bd360 | 253 | for (i = 0; i < 4; i++) |
fahadmirza | 0:a0c5877bd360 | 254 | { |
fahadmirza | 0:a0c5877bd360 | 255 | if (i != 0) tmp[len++] = '.'; |
fahadmirza | 0:a0c5877bd360 | 256 | n = addr[i]; |
fahadmirza | 0:a0c5877bd360 | 257 | |
fahadmirza | 0:a0c5877bd360 | 258 | if (n == 0) |
fahadmirza | 0:a0c5877bd360 | 259 | tmp[len++] = lower_digits[0]; |
fahadmirza | 0:a0c5877bd360 | 260 | else |
fahadmirza | 0:a0c5877bd360 | 261 | { |
fahadmirza | 0:a0c5877bd360 | 262 | if (n >= 100) |
fahadmirza | 0:a0c5877bd360 | 263 | { |
fahadmirza | 0:a0c5877bd360 | 264 | tmp[len++] = lower_digits[n / 100]; |
fahadmirza | 0:a0c5877bd360 | 265 | n = n % 100; |
fahadmirza | 0:a0c5877bd360 | 266 | tmp[len++] = lower_digits[n / 10]; |
fahadmirza | 0:a0c5877bd360 | 267 | n = n % 10; |
fahadmirza | 0:a0c5877bd360 | 268 | } |
fahadmirza | 0:a0c5877bd360 | 269 | else if (n >= 10) |
fahadmirza | 0:a0c5877bd360 | 270 | { |
fahadmirza | 0:a0c5877bd360 | 271 | tmp[len++] = lower_digits[n / 10]; |
fahadmirza | 0:a0c5877bd360 | 272 | n = n % 10; |
fahadmirza | 0:a0c5877bd360 | 273 | } |
fahadmirza | 0:a0c5877bd360 | 274 | |
fahadmirza | 0:a0c5877bd360 | 275 | tmp[len++] = lower_digits[n]; |
fahadmirza | 0:a0c5877bd360 | 276 | } |
fahadmirza | 0:a0c5877bd360 | 277 | } |
fahadmirza | 0:a0c5877bd360 | 278 | |
fahadmirza | 0:a0c5877bd360 | 279 | if (!(type & LEFT)) while (len < size--) *str++ = ' '; |
fahadmirza | 0:a0c5877bd360 | 280 | for (i = 0; i < len; ++i) *str++ = tmp[i]; |
fahadmirza | 0:a0c5877bd360 | 281 | while (len < size--) *str++ = ' '; |
fahadmirza | 0:a0c5877bd360 | 282 | |
fahadmirza | 0:a0c5877bd360 | 283 | return str; |
fahadmirza | 0:a0c5877bd360 | 284 | } |
fahadmirza | 0:a0c5877bd360 | 285 | #endif |
fahadmirza | 0:a0c5877bd360 | 286 | |
fahadmirza | 0:a0c5877bd360 | 287 | #ifdef HAS_FLOAT |
fahadmirza | 0:a0c5877bd360 | 288 | |
fahadmirza | 0:a0c5877bd360 | 289 | char *ecvtbuf(double arg, int ndigits, int *decpt, int *sign, char *buf); |
fahadmirza | 0:a0c5877bd360 | 290 | char *fcvtbuf(double arg, int ndigits, int *decpt, int *sign, char *buf); |
fahadmirza | 0:a0c5877bd360 | 291 | static void ee_bufcpy(char *d, char *s, int count); |
fahadmirza | 0:a0c5877bd360 | 292 | |
fahadmirza | 0:a0c5877bd360 | 293 | void ee_bufcpy(char *pd, char *ps, int count) { |
fahadmirza | 0:a0c5877bd360 | 294 | char *pe=ps+count; |
fahadmirza | 0:a0c5877bd360 | 295 | while (ps!=pe) |
fahadmirza | 0:a0c5877bd360 | 296 | *pd++=*ps++; |
fahadmirza | 0:a0c5877bd360 | 297 | } |
fahadmirza | 0:a0c5877bd360 | 298 | |
fahadmirza | 0:a0c5877bd360 | 299 | static void parse_float(double value, char *buffer, char fmt, int precision) |
fahadmirza | 0:a0c5877bd360 | 300 | { |
fahadmirza | 0:a0c5877bd360 | 301 | int decpt, sign, exp, pos; |
fahadmirza | 0:a0c5877bd360 | 302 | char *fdigits = NULL; |
fahadmirza | 0:a0c5877bd360 | 303 | char cvtbuf[80]; |
fahadmirza | 0:a0c5877bd360 | 304 | int capexp = 0; |
fahadmirza | 0:a0c5877bd360 | 305 | int magnitude; |
fahadmirza | 0:a0c5877bd360 | 306 | |
fahadmirza | 0:a0c5877bd360 | 307 | if (fmt == 'G' || fmt == 'E') |
fahadmirza | 0:a0c5877bd360 | 308 | { |
fahadmirza | 0:a0c5877bd360 | 309 | capexp = 1; |
fahadmirza | 0:a0c5877bd360 | 310 | fmt += 'a' - 'A'; |
fahadmirza | 0:a0c5877bd360 | 311 | } |
fahadmirza | 0:a0c5877bd360 | 312 | |
fahadmirza | 0:a0c5877bd360 | 313 | if (fmt == 'g') |
fahadmirza | 0:a0c5877bd360 | 314 | { |
fahadmirza | 0:a0c5877bd360 | 315 | fdigits = ecvtbuf(value, precision, &decpt, &sign, cvtbuf); |
fahadmirza | 0:a0c5877bd360 | 316 | magnitude = decpt - 1; |
fahadmirza | 0:a0c5877bd360 | 317 | if (magnitude < -4 || magnitude > precision - 1) |
fahadmirza | 0:a0c5877bd360 | 318 | { |
fahadmirza | 0:a0c5877bd360 | 319 | fmt = 'e'; |
fahadmirza | 0:a0c5877bd360 | 320 | precision -= 1; |
fahadmirza | 0:a0c5877bd360 | 321 | } |
fahadmirza | 0:a0c5877bd360 | 322 | else |
fahadmirza | 0:a0c5877bd360 | 323 | { |
fahadmirza | 0:a0c5877bd360 | 324 | fmt = 'f'; |
fahadmirza | 0:a0c5877bd360 | 325 | precision -= decpt; |
fahadmirza | 0:a0c5877bd360 | 326 | } |
fahadmirza | 0:a0c5877bd360 | 327 | } |
fahadmirza | 0:a0c5877bd360 | 328 | |
fahadmirza | 0:a0c5877bd360 | 329 | if (fmt == 'e') |
fahadmirza | 0:a0c5877bd360 | 330 | { |
fahadmirza | 0:a0c5877bd360 | 331 | fdigits = ecvtbuf(value, precision + 1, &decpt, &sign, cvtbuf); |
fahadmirza | 0:a0c5877bd360 | 332 | |
fahadmirza | 0:a0c5877bd360 | 333 | if (sign) *buffer++ = '-'; |
fahadmirza | 0:a0c5877bd360 | 334 | *buffer++ = *fdigits; |
fahadmirza | 0:a0c5877bd360 | 335 | if (precision > 0) *buffer++ = '.'; |
fahadmirza | 0:a0c5877bd360 | 336 | ee_bufcpy(buffer, fdigits + 1, precision); |
fahadmirza | 0:a0c5877bd360 | 337 | buffer += precision; |
fahadmirza | 0:a0c5877bd360 | 338 | *buffer++ = capexp ? 'E' : 'e'; |
fahadmirza | 0:a0c5877bd360 | 339 | |
fahadmirza | 0:a0c5877bd360 | 340 | if (decpt == 0) |
fahadmirza | 0:a0c5877bd360 | 341 | { |
fahadmirza | 0:a0c5877bd360 | 342 | if (value == 0.0) |
fahadmirza | 0:a0c5877bd360 | 343 | exp = 0; |
fahadmirza | 0:a0c5877bd360 | 344 | else |
fahadmirza | 0:a0c5877bd360 | 345 | exp = -1; |
fahadmirza | 0:a0c5877bd360 | 346 | } |
fahadmirza | 0:a0c5877bd360 | 347 | else |
fahadmirza | 0:a0c5877bd360 | 348 | exp = decpt - 1; |
fahadmirza | 0:a0c5877bd360 | 349 | |
fahadmirza | 0:a0c5877bd360 | 350 | if (exp < 0) |
fahadmirza | 0:a0c5877bd360 | 351 | { |
fahadmirza | 0:a0c5877bd360 | 352 | *buffer++ = '-'; |
fahadmirza | 0:a0c5877bd360 | 353 | exp = -exp; |
fahadmirza | 0:a0c5877bd360 | 354 | } |
fahadmirza | 0:a0c5877bd360 | 355 | else |
fahadmirza | 0:a0c5877bd360 | 356 | *buffer++ = '+'; |
fahadmirza | 0:a0c5877bd360 | 357 | |
fahadmirza | 0:a0c5877bd360 | 358 | buffer[2] = (exp % 10) + '0'; |
fahadmirza | 0:a0c5877bd360 | 359 | exp = exp / 10; |
fahadmirza | 0:a0c5877bd360 | 360 | buffer[1] = (exp % 10) + '0'; |
fahadmirza | 0:a0c5877bd360 | 361 | exp = exp / 10; |
fahadmirza | 0:a0c5877bd360 | 362 | buffer[0] = (exp % 10) + '0'; |
fahadmirza | 0:a0c5877bd360 | 363 | buffer += 3; |
fahadmirza | 0:a0c5877bd360 | 364 | } |
fahadmirza | 0:a0c5877bd360 | 365 | else if (fmt == 'f') |
fahadmirza | 0:a0c5877bd360 | 366 | { |
fahadmirza | 0:a0c5877bd360 | 367 | fdigits = fcvtbuf(value, precision, &decpt, &sign, cvtbuf); |
fahadmirza | 0:a0c5877bd360 | 368 | if (sign) *buffer++ = '-'; |
fahadmirza | 0:a0c5877bd360 | 369 | if (*fdigits) |
fahadmirza | 0:a0c5877bd360 | 370 | { |
fahadmirza | 0:a0c5877bd360 | 371 | if (decpt <= 0) |
fahadmirza | 0:a0c5877bd360 | 372 | { |
fahadmirza | 0:a0c5877bd360 | 373 | *buffer++ = '0'; |
fahadmirza | 0:a0c5877bd360 | 374 | *buffer++ = '.'; |
fahadmirza | 0:a0c5877bd360 | 375 | for (pos = 0; pos < -decpt; pos++) *buffer++ = '0'; |
fahadmirza | 0:a0c5877bd360 | 376 | while (*fdigits) *buffer++ = *fdigits++; |
fahadmirza | 0:a0c5877bd360 | 377 | } |
fahadmirza | 0:a0c5877bd360 | 378 | else |
fahadmirza | 0:a0c5877bd360 | 379 | { |
fahadmirza | 0:a0c5877bd360 | 380 | pos = 0; |
fahadmirza | 0:a0c5877bd360 | 381 | while (*fdigits) |
fahadmirza | 0:a0c5877bd360 | 382 | { |
fahadmirza | 0:a0c5877bd360 | 383 | if (pos++ == decpt) *buffer++ = '.'; |
fahadmirza | 0:a0c5877bd360 | 384 | *buffer++ = *fdigits++; |
fahadmirza | 0:a0c5877bd360 | 385 | } |
fahadmirza | 0:a0c5877bd360 | 386 | } |
fahadmirza | 0:a0c5877bd360 | 387 | } |
fahadmirza | 0:a0c5877bd360 | 388 | else |
fahadmirza | 0:a0c5877bd360 | 389 | { |
fahadmirza | 0:a0c5877bd360 | 390 | *buffer++ = '0'; |
fahadmirza | 0:a0c5877bd360 | 391 | if (precision > 0) |
fahadmirza | 0:a0c5877bd360 | 392 | { |
fahadmirza | 0:a0c5877bd360 | 393 | *buffer++ = '.'; |
fahadmirza | 0:a0c5877bd360 | 394 | for (pos = 0; pos < precision; pos++) *buffer++ = '0'; |
fahadmirza | 0:a0c5877bd360 | 395 | } |
fahadmirza | 0:a0c5877bd360 | 396 | } |
fahadmirza | 0:a0c5877bd360 | 397 | } |
fahadmirza | 0:a0c5877bd360 | 398 | |
fahadmirza | 0:a0c5877bd360 | 399 | *buffer = '\0'; |
fahadmirza | 0:a0c5877bd360 | 400 | } |
fahadmirza | 0:a0c5877bd360 | 401 | |
fahadmirza | 0:a0c5877bd360 | 402 | static void decimal_point(char *buffer) |
fahadmirza | 0:a0c5877bd360 | 403 | { |
fahadmirza | 0:a0c5877bd360 | 404 | while (*buffer) |
fahadmirza | 0:a0c5877bd360 | 405 | { |
fahadmirza | 0:a0c5877bd360 | 406 | if (*buffer == '.') return; |
fahadmirza | 0:a0c5877bd360 | 407 | if (*buffer == 'e' || *buffer == 'E') break; |
fahadmirza | 0:a0c5877bd360 | 408 | buffer++; |
fahadmirza | 0:a0c5877bd360 | 409 | } |
fahadmirza | 0:a0c5877bd360 | 410 | |
fahadmirza | 0:a0c5877bd360 | 411 | if (*buffer) |
fahadmirza | 0:a0c5877bd360 | 412 | { |
fahadmirza | 0:a0c5877bd360 | 413 | int n = strnlen(buffer,256); |
fahadmirza | 0:a0c5877bd360 | 414 | while (n > 0) |
fahadmirza | 0:a0c5877bd360 | 415 | { |
fahadmirza | 0:a0c5877bd360 | 416 | buffer[n + 1] = buffer[n]; |
fahadmirza | 0:a0c5877bd360 | 417 | n--; |
fahadmirza | 0:a0c5877bd360 | 418 | } |
fahadmirza | 0:a0c5877bd360 | 419 | |
fahadmirza | 0:a0c5877bd360 | 420 | *buffer = '.'; |
fahadmirza | 0:a0c5877bd360 | 421 | } |
fahadmirza | 0:a0c5877bd360 | 422 | else |
fahadmirza | 0:a0c5877bd360 | 423 | { |
fahadmirza | 0:a0c5877bd360 | 424 | *buffer++ = '.'; |
fahadmirza | 0:a0c5877bd360 | 425 | *buffer = '\0'; |
fahadmirza | 0:a0c5877bd360 | 426 | } |
fahadmirza | 0:a0c5877bd360 | 427 | } |
fahadmirza | 0:a0c5877bd360 | 428 | |
fahadmirza | 0:a0c5877bd360 | 429 | static void cropzeros(char *buffer) |
fahadmirza | 0:a0c5877bd360 | 430 | { |
fahadmirza | 0:a0c5877bd360 | 431 | char *stop; |
fahadmirza | 0:a0c5877bd360 | 432 | |
fahadmirza | 0:a0c5877bd360 | 433 | while (*buffer && *buffer != '.') buffer++; |
fahadmirza | 0:a0c5877bd360 | 434 | if (*buffer++) |
fahadmirza | 0:a0c5877bd360 | 435 | { |
fahadmirza | 0:a0c5877bd360 | 436 | while (*buffer && *buffer != 'e' && *buffer != 'E') buffer++; |
fahadmirza | 0:a0c5877bd360 | 437 | stop = buffer--; |
fahadmirza | 0:a0c5877bd360 | 438 | while (*buffer == '0') buffer--; |
fahadmirza | 0:a0c5877bd360 | 439 | if (*buffer == '.') buffer--; |
fahadmirza | 0:a0c5877bd360 | 440 | while (buffer!=stop) |
fahadmirza | 0:a0c5877bd360 | 441 | *++buffer=0; |
fahadmirza | 0:a0c5877bd360 | 442 | } |
fahadmirza | 0:a0c5877bd360 | 443 | } |
fahadmirza | 0:a0c5877bd360 | 444 | |
fahadmirza | 0:a0c5877bd360 | 445 | static char *flt(char *str, double num, int size, int precision, char fmt, int flags) |
fahadmirza | 0:a0c5877bd360 | 446 | { |
fahadmirza | 0:a0c5877bd360 | 447 | char tmp[80]; |
fahadmirza | 0:a0c5877bd360 | 448 | char c, sign; |
fahadmirza | 0:a0c5877bd360 | 449 | int n, i; |
fahadmirza | 0:a0c5877bd360 | 450 | |
fahadmirza | 0:a0c5877bd360 | 451 | // Left align means no zero padding |
fahadmirza | 0:a0c5877bd360 | 452 | #ifdef TINY_PRINTF |
fahadmirza | 0:a0c5877bd360 | 453 | #else |
fahadmirza | 0:a0c5877bd360 | 454 | if (flags & LEFT) flags &= ~ZEROPAD; |
fahadmirza | 0:a0c5877bd360 | 455 | #endif |
fahadmirza | 0:a0c5877bd360 | 456 | |
fahadmirza | 0:a0c5877bd360 | 457 | // Determine padding and sign char |
fahadmirza | 0:a0c5877bd360 | 458 | c = (flags & ZEROPAD) ? '0' : ' '; |
fahadmirza | 0:a0c5877bd360 | 459 | sign = 0; |
fahadmirza | 0:a0c5877bd360 | 460 | if (flags & SIGN) |
fahadmirza | 0:a0c5877bd360 | 461 | { |
fahadmirza | 0:a0c5877bd360 | 462 | if (num < 0.0) |
fahadmirza | 0:a0c5877bd360 | 463 | { |
fahadmirza | 0:a0c5877bd360 | 464 | sign = '-'; |
fahadmirza | 0:a0c5877bd360 | 465 | num = -num; |
fahadmirza | 0:a0c5877bd360 | 466 | size--; |
fahadmirza | 0:a0c5877bd360 | 467 | } |
fahadmirza | 0:a0c5877bd360 | 468 | #ifdef TINY_PRINTF |
fahadmirza | 0:a0c5877bd360 | 469 | #else |
fahadmirza | 0:a0c5877bd360 | 470 | else if (flags & PLUS) |
fahadmirza | 0:a0c5877bd360 | 471 | { |
fahadmirza | 0:a0c5877bd360 | 472 | sign = '+'; |
fahadmirza | 0:a0c5877bd360 | 473 | size--; |
fahadmirza | 0:a0c5877bd360 | 474 | } |
fahadmirza | 0:a0c5877bd360 | 475 | else if (flags & SPACE) |
fahadmirza | 0:a0c5877bd360 | 476 | { |
fahadmirza | 0:a0c5877bd360 | 477 | sign = ' '; |
fahadmirza | 0:a0c5877bd360 | 478 | size--; |
fahadmirza | 0:a0c5877bd360 | 479 | } |
fahadmirza | 0:a0c5877bd360 | 480 | #endif |
fahadmirza | 0:a0c5877bd360 | 481 | } |
fahadmirza | 0:a0c5877bd360 | 482 | |
fahadmirza | 0:a0c5877bd360 | 483 | // Compute the precision value |
fahadmirza | 0:a0c5877bd360 | 484 | if (precision < 0) |
fahadmirza | 0:a0c5877bd360 | 485 | precision = 6; // Default precision: 6 |
fahadmirza | 0:a0c5877bd360 | 486 | |
fahadmirza | 0:a0c5877bd360 | 487 | // Convert floating point number to text |
fahadmirza | 0:a0c5877bd360 | 488 | parse_float(num, tmp, fmt, precision); |
fahadmirza | 0:a0c5877bd360 | 489 | |
fahadmirza | 0:a0c5877bd360 | 490 | #ifdef TINY_PRINTF |
fahadmirza | 0:a0c5877bd360 | 491 | #else |
fahadmirza | 0:a0c5877bd360 | 492 | if ((flags & HEX_PREP) && precision == 0) decimal_point(tmp); |
fahadmirza | 0:a0c5877bd360 | 493 | #endif |
fahadmirza | 0:a0c5877bd360 | 494 | if (fmt == 'g' && !(flags & HEX_PREP)) cropzeros(tmp); |
fahadmirza | 0:a0c5877bd360 | 495 | |
fahadmirza | 0:a0c5877bd360 | 496 | n = strnlen(tmp,256); |
fahadmirza | 0:a0c5877bd360 | 497 | |
fahadmirza | 0:a0c5877bd360 | 498 | // Output number with alignment and padding |
fahadmirza | 0:a0c5877bd360 | 499 | size -= n; |
fahadmirza | 0:a0c5877bd360 | 500 | if (!(flags & (ZEROPAD | LEFT))) while (size-- > 0) *str++ = ' '; |
fahadmirza | 0:a0c5877bd360 | 501 | if (sign) *str++ = sign; |
fahadmirza | 0:a0c5877bd360 | 502 | if (!(flags & LEFT)) while (size-- > 0) *str++ = c; |
fahadmirza | 0:a0c5877bd360 | 503 | for (i = 0; i < n; i++) *str++ = tmp[i]; |
fahadmirza | 0:a0c5877bd360 | 504 | while (size-- > 0) *str++ = ' '; |
fahadmirza | 0:a0c5877bd360 | 505 | |
fahadmirza | 0:a0c5877bd360 | 506 | return str; |
fahadmirza | 0:a0c5877bd360 | 507 | } |
fahadmirza | 0:a0c5877bd360 | 508 | |
fahadmirza | 0:a0c5877bd360 | 509 | #endif |
fahadmirza | 0:a0c5877bd360 | 510 | |
fahadmirza | 0:a0c5877bd360 | 511 | #define CHECK_STR_SIZE(_buf, _str, _size) \ |
fahadmirza | 0:a0c5877bd360 | 512 | if ((((_str) - (_buf)) >= ((_size)-1))) { break; } |
fahadmirza | 0:a0c5877bd360 | 513 | |
fahadmirza | 0:a0c5877bd360 | 514 | int tiny_vsnprintf_like(char *buf, const int size, const char *fmt, va_list args) |
fahadmirza | 0:a0c5877bd360 | 515 | { |
fahadmirza | 0:a0c5877bd360 | 516 | unsigned long num; |
fahadmirza | 0:a0c5877bd360 | 517 | int base; |
fahadmirza | 0:a0c5877bd360 | 518 | char *str; |
fahadmirza | 0:a0c5877bd360 | 519 | int len; |
fahadmirza | 0:a0c5877bd360 | 520 | int i; |
fahadmirza | 0:a0c5877bd360 | 521 | char *s; |
fahadmirza | 0:a0c5877bd360 | 522 | |
fahadmirza | 0:a0c5877bd360 | 523 | int flags; // Flags to number() |
fahadmirza | 0:a0c5877bd360 | 524 | |
fahadmirza | 0:a0c5877bd360 | 525 | int field_width; // Width of output field |
fahadmirza | 0:a0c5877bd360 | 526 | int precision; // Min. # of digits for integers; max number of chars for from string |
fahadmirza | 0:a0c5877bd360 | 527 | int qualifier; // 'h', 'l', or 'L' for integer fields |
fahadmirza | 0:a0c5877bd360 | 528 | |
fahadmirza | 0:a0c5877bd360 | 529 | if (size <= 0) |
fahadmirza | 0:a0c5877bd360 | 530 | { |
fahadmirza | 0:a0c5877bd360 | 531 | return 0; |
fahadmirza | 0:a0c5877bd360 | 532 | } |
fahadmirza | 0:a0c5877bd360 | 533 | |
fahadmirza | 0:a0c5877bd360 | 534 | for (str = buf; *fmt || ((str - buf) >= size-1); fmt++) |
fahadmirza | 0:a0c5877bd360 | 535 | { |
fahadmirza | 0:a0c5877bd360 | 536 | CHECK_STR_SIZE(buf, str, size); |
fahadmirza | 0:a0c5877bd360 | 537 | |
fahadmirza | 0:a0c5877bd360 | 538 | if (*fmt != '%') |
fahadmirza | 0:a0c5877bd360 | 539 | { |
fahadmirza | 0:a0c5877bd360 | 540 | *str++ = *fmt; |
fahadmirza | 0:a0c5877bd360 | 541 | continue; |
fahadmirza | 0:a0c5877bd360 | 542 | } |
fahadmirza | 0:a0c5877bd360 | 543 | |
fahadmirza | 0:a0c5877bd360 | 544 | // Process flags |
fahadmirza | 0:a0c5877bd360 | 545 | flags = 0; |
fahadmirza | 0:a0c5877bd360 | 546 | #ifdef TINY_PRINTF |
fahadmirza | 0:a0c5877bd360 | 547 | /* Support %0, but not %-, %+, %space and %# */ |
fahadmirza | 0:a0c5877bd360 | 548 | fmt++; |
fahadmirza | 0:a0c5877bd360 | 549 | if (*fmt == '0') |
fahadmirza | 0:a0c5877bd360 | 550 | { |
fahadmirza | 0:a0c5877bd360 | 551 | flags |= ZEROPAD; |
fahadmirza | 0:a0c5877bd360 | 552 | } |
fahadmirza | 0:a0c5877bd360 | 553 | #else |
fahadmirza | 0:a0c5877bd360 | 554 | repeat: |
fahadmirza | 0:a0c5877bd360 | 555 | fmt++; // This also skips first '%' |
fahadmirza | 0:a0c5877bd360 | 556 | switch (*fmt) |
fahadmirza | 0:a0c5877bd360 | 557 | { |
fahadmirza | 0:a0c5877bd360 | 558 | case '-': flags |= LEFT; goto repeat; |
fahadmirza | 0:a0c5877bd360 | 559 | case '+': flags |= PLUS; goto repeat; |
fahadmirza | 0:a0c5877bd360 | 560 | case ' ': flags |= SPACE; goto repeat; |
fahadmirza | 0:a0c5877bd360 | 561 | case '#': flags |= HEX_PREP; goto repeat; |
fahadmirza | 0:a0c5877bd360 | 562 | case '0': flags |= ZEROPAD; goto repeat; |
fahadmirza | 0:a0c5877bd360 | 563 | } |
fahadmirza | 0:a0c5877bd360 | 564 | #endif |
fahadmirza | 0:a0c5877bd360 | 565 | |
fahadmirza | 0:a0c5877bd360 | 566 | // Get field width |
fahadmirza | 0:a0c5877bd360 | 567 | field_width = -1; |
fahadmirza | 0:a0c5877bd360 | 568 | if (is_digit(*fmt)) |
fahadmirza | 0:a0c5877bd360 | 569 | field_width = ee_skip_atoi(&fmt); |
fahadmirza | 0:a0c5877bd360 | 570 | #ifdef TINY_PRINTF |
fahadmirza | 0:a0c5877bd360 | 571 | /* Does not support %* */ |
fahadmirza | 0:a0c5877bd360 | 572 | #else |
fahadmirza | 0:a0c5877bd360 | 573 | else if (*fmt == '*') |
fahadmirza | 0:a0c5877bd360 | 574 | { |
fahadmirza | 0:a0c5877bd360 | 575 | fmt++; |
fahadmirza | 0:a0c5877bd360 | 576 | field_width = va_arg(args, int); |
fahadmirza | 0:a0c5877bd360 | 577 | if (field_width < 0) |
fahadmirza | 0:a0c5877bd360 | 578 | { |
fahadmirza | 0:a0c5877bd360 | 579 | field_width = -field_width; |
fahadmirza | 0:a0c5877bd360 | 580 | flags |= LEFT; |
fahadmirza | 0:a0c5877bd360 | 581 | } |
fahadmirza | 0:a0c5877bd360 | 582 | } |
fahadmirza | 0:a0c5877bd360 | 583 | #endif |
fahadmirza | 0:a0c5877bd360 | 584 | |
fahadmirza | 0:a0c5877bd360 | 585 | // Get the precision |
fahadmirza | 0:a0c5877bd360 | 586 | precision = -1; |
fahadmirza | 0:a0c5877bd360 | 587 | #ifdef TINY_PRINTF |
fahadmirza | 0:a0c5877bd360 | 588 | /* Does not support %. */ |
fahadmirza | 0:a0c5877bd360 | 589 | #else |
fahadmirza | 0:a0c5877bd360 | 590 | if (*fmt == '.') |
fahadmirza | 0:a0c5877bd360 | 591 | { |
fahadmirza | 0:a0c5877bd360 | 592 | ++fmt; |
fahadmirza | 0:a0c5877bd360 | 593 | if (is_digit(*fmt)) |
fahadmirza | 0:a0c5877bd360 | 594 | precision = ee_skip_atoi(&fmt); |
fahadmirza | 0:a0c5877bd360 | 595 | else if (*fmt == '*') |
fahadmirza | 0:a0c5877bd360 | 596 | { |
fahadmirza | 0:a0c5877bd360 | 597 | ++fmt; |
fahadmirza | 0:a0c5877bd360 | 598 | precision = va_arg(args, int); |
fahadmirza | 0:a0c5877bd360 | 599 | } |
fahadmirza | 0:a0c5877bd360 | 600 | if (precision < 0) precision = 0; |
fahadmirza | 0:a0c5877bd360 | 601 | } |
fahadmirza | 0:a0c5877bd360 | 602 | #endif |
fahadmirza | 0:a0c5877bd360 | 603 | |
fahadmirza | 0:a0c5877bd360 | 604 | // Get the conversion qualifier |
fahadmirza | 0:a0c5877bd360 | 605 | qualifier = -1; |
fahadmirza | 0:a0c5877bd360 | 606 | |
fahadmirza | 0:a0c5877bd360 | 607 | if (*fmt == 'l' || *fmt == 'L') |
fahadmirza | 0:a0c5877bd360 | 608 | { |
fahadmirza | 0:a0c5877bd360 | 609 | qualifier = *fmt; |
fahadmirza | 0:a0c5877bd360 | 610 | fmt++; |
fahadmirza | 0:a0c5877bd360 | 611 | } |
fahadmirza | 0:a0c5877bd360 | 612 | |
fahadmirza | 0:a0c5877bd360 | 613 | |
fahadmirza | 0:a0c5877bd360 | 614 | // Default base |
fahadmirza | 0:a0c5877bd360 | 615 | base = 10; |
fahadmirza | 0:a0c5877bd360 | 616 | |
fahadmirza | 0:a0c5877bd360 | 617 | switch (*fmt) |
fahadmirza | 0:a0c5877bd360 | 618 | { |
fahadmirza | 0:a0c5877bd360 | 619 | case 'c': |
fahadmirza | 0:a0c5877bd360 | 620 | #ifdef TINY_PRINTF |
fahadmirza | 0:a0c5877bd360 | 621 | #else |
fahadmirza | 0:a0c5877bd360 | 622 | if (!(flags & LEFT)) |
fahadmirza | 0:a0c5877bd360 | 623 | #endif |
fahadmirza | 0:a0c5877bd360 | 624 | while (--field_width > 0) *str++ = ' '; |
fahadmirza | 0:a0c5877bd360 | 625 | *str++ = (unsigned char) va_arg(args, int); |
fahadmirza | 0:a0c5877bd360 | 626 | #ifdef TINY_PRINTF |
fahadmirza | 0:a0c5877bd360 | 627 | #else |
fahadmirza | 0:a0c5877bd360 | 628 | while (--field_width > 0) *str++ = ' '; |
fahadmirza | 0:a0c5877bd360 | 629 | #endif |
fahadmirza | 0:a0c5877bd360 | 630 | continue; |
fahadmirza | 0:a0c5877bd360 | 631 | |
fahadmirza | 0:a0c5877bd360 | 632 | case 's': |
fahadmirza | 0:a0c5877bd360 | 633 | s = va_arg(args, char *); |
fahadmirza | 0:a0c5877bd360 | 634 | if (!s) s = "<NULL>"; |
fahadmirza | 0:a0c5877bd360 | 635 | #ifdef TINY_PRINTF |
fahadmirza | 0:a0c5877bd360 | 636 | len = strlen(s); |
fahadmirza | 0:a0c5877bd360 | 637 | #else |
fahadmirza | 0:a0c5877bd360 | 638 | len = strnlen(s, precision); |
fahadmirza | 0:a0c5877bd360 | 639 | if (!(flags & LEFT)) |
fahadmirza | 0:a0c5877bd360 | 640 | #endif |
fahadmirza | 0:a0c5877bd360 | 641 | while (len < field_width--) *str++ = ' '; |
fahadmirza | 0:a0c5877bd360 | 642 | for (i = 0; i < len; ++i) *str++ = *s++; |
fahadmirza | 0:a0c5877bd360 | 643 | #ifdef TINY_PRINTF |
fahadmirza | 0:a0c5877bd360 | 644 | #else |
fahadmirza | 0:a0c5877bd360 | 645 | while (len < field_width--) *str++ = ' '; |
fahadmirza | 0:a0c5877bd360 | 646 | #endif |
fahadmirza | 0:a0c5877bd360 | 647 | continue; |
fahadmirza | 0:a0c5877bd360 | 648 | |
fahadmirza | 0:a0c5877bd360 | 649 | #ifdef TINY_PRINTF |
fahadmirza | 0:a0c5877bd360 | 650 | /* Does not support %p, %A, %a, %o */ |
fahadmirza | 0:a0c5877bd360 | 651 | #else |
fahadmirza | 0:a0c5877bd360 | 652 | case 'p': |
fahadmirza | 0:a0c5877bd360 | 653 | if (field_width == -1) |
fahadmirza | 0:a0c5877bd360 | 654 | { |
fahadmirza | 0:a0c5877bd360 | 655 | field_width = 2 * sizeof(void *); |
fahadmirza | 0:a0c5877bd360 | 656 | flags |= ZEROPAD; |
fahadmirza | 0:a0c5877bd360 | 657 | } |
fahadmirza | 0:a0c5877bd360 | 658 | str = ee_number(str, (size - (str - buf)), (unsigned long) va_arg(args, void *), 16, field_width, precision, flags); |
fahadmirza | 0:a0c5877bd360 | 659 | continue; |
fahadmirza | 0:a0c5877bd360 | 660 | |
fahadmirza | 0:a0c5877bd360 | 661 | case 'A': |
fahadmirza | 0:a0c5877bd360 | 662 | flags |= UPPERCASE; |
fahadmirza | 0:a0c5877bd360 | 663 | |
fahadmirza | 0:a0c5877bd360 | 664 | case 'a': |
fahadmirza | 0:a0c5877bd360 | 665 | if (qualifier == 'l') |
fahadmirza | 0:a0c5877bd360 | 666 | str = eaddr(str, va_arg(args, unsigned char *), field_width, precision, flags); |
fahadmirza | 0:a0c5877bd360 | 667 | else |
fahadmirza | 0:a0c5877bd360 | 668 | str = iaddr(str, va_arg(args, unsigned char *), field_width, precision, flags); |
fahadmirza | 0:a0c5877bd360 | 669 | continue; |
fahadmirza | 0:a0c5877bd360 | 670 | |
fahadmirza | 0:a0c5877bd360 | 671 | // Integer number formats - set up the flags and "break" |
fahadmirza | 0:a0c5877bd360 | 672 | case 'o': |
fahadmirza | 0:a0c5877bd360 | 673 | base = 8; |
fahadmirza | 0:a0c5877bd360 | 674 | break; |
fahadmirza | 0:a0c5877bd360 | 675 | #endif |
fahadmirza | 0:a0c5877bd360 | 676 | |
fahadmirza | 0:a0c5877bd360 | 677 | case 'X': |
fahadmirza | 0:a0c5877bd360 | 678 | flags |= UPPERCASE; |
fahadmirza | 0:a0c5877bd360 | 679 | |
fahadmirza | 0:a0c5877bd360 | 680 | case 'x': |
fahadmirza | 0:a0c5877bd360 | 681 | base = 16; |
fahadmirza | 0:a0c5877bd360 | 682 | break; |
fahadmirza | 0:a0c5877bd360 | 683 | |
fahadmirza | 0:a0c5877bd360 | 684 | case 'd': |
fahadmirza | 0:a0c5877bd360 | 685 | case 'i': |
fahadmirza | 0:a0c5877bd360 | 686 | flags |= SIGN; |
fahadmirza | 0:a0c5877bd360 | 687 | |
fahadmirza | 0:a0c5877bd360 | 688 | case 'u': |
fahadmirza | 0:a0c5877bd360 | 689 | break; |
fahadmirza | 0:a0c5877bd360 | 690 | |
fahadmirza | 0:a0c5877bd360 | 691 | #ifdef HAS_FLOAT |
fahadmirza | 0:a0c5877bd360 | 692 | |
fahadmirza | 0:a0c5877bd360 | 693 | case 'f': |
fahadmirza | 0:a0c5877bd360 | 694 | str = flt(str, va_arg(args, double), field_width, precision, *fmt, flags | SIGN); |
fahadmirza | 0:a0c5877bd360 | 695 | continue; |
fahadmirza | 0:a0c5877bd360 | 696 | |
fahadmirza | 0:a0c5877bd360 | 697 | #endif |
fahadmirza | 0:a0c5877bd360 | 698 | |
fahadmirza | 0:a0c5877bd360 | 699 | default: |
fahadmirza | 0:a0c5877bd360 | 700 | if (*fmt != '%') *str++ = '%'; |
fahadmirza | 0:a0c5877bd360 | 701 | CHECK_STR_SIZE(buf, str, size); |
fahadmirza | 0:a0c5877bd360 | 702 | if (*fmt) |
fahadmirza | 0:a0c5877bd360 | 703 | *str++ = *fmt; |
fahadmirza | 0:a0c5877bd360 | 704 | else |
fahadmirza | 0:a0c5877bd360 | 705 | --fmt; |
fahadmirza | 0:a0c5877bd360 | 706 | CHECK_STR_SIZE(buf, str, size); |
fahadmirza | 0:a0c5877bd360 | 707 | continue; |
fahadmirza | 0:a0c5877bd360 | 708 | } |
fahadmirza | 0:a0c5877bd360 | 709 | |
fahadmirza | 0:a0c5877bd360 | 710 | if (qualifier == 'l') |
fahadmirza | 0:a0c5877bd360 | 711 | num = va_arg(args, unsigned long); |
fahadmirza | 0:a0c5877bd360 | 712 | else if (flags & SIGN) |
fahadmirza | 0:a0c5877bd360 | 713 | num = va_arg(args, int); |
fahadmirza | 0:a0c5877bd360 | 714 | else |
fahadmirza | 0:a0c5877bd360 | 715 | num = va_arg(args, unsigned int); |
fahadmirza | 0:a0c5877bd360 | 716 | |
fahadmirza | 0:a0c5877bd360 | 717 | str = ee_number(str, ((size - 1) - (str - buf)), num, base, field_width, precision, flags); |
fahadmirza | 0:a0c5877bd360 | 718 | } |
fahadmirza | 0:a0c5877bd360 | 719 | |
fahadmirza | 0:a0c5877bd360 | 720 | *str = '\0'; |
fahadmirza | 0:a0c5877bd360 | 721 | return str - buf; |
fahadmirza | 0:a0c5877bd360 | 722 | } |
fahadmirza | 0:a0c5877bd360 | 723 |