|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2020 Arduino. All rights reserved. |
3 |
| - */ |
| 2 | + Copyright (c) 2014 Arduino LLC. All right reserved. |
4 | 3 |
|
5 |
| -/************************************************************************************** |
6 |
| - * INCLUDE |
7 |
| - **************************************************************************************/ |
| 4 | + This library is free software; you can redistribute it and/or |
| 5 | + modify it under the terms of the GNU Lesser General Public |
| 6 | + License as published by the Free Software Foundation; either |
| 7 | + version 2.1 of the License, or (at your option) any later version. |
8 | 8 |
|
9 |
| -#include <api/itoa.h> |
| 9 | + This library is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + See the GNU Lesser General Public License for more details. |
10 | 13 |
|
11 |
| -#include <string> |
12 |
| -#include <stdexcept> |
| 14 | + You should have received a copy of the GNU Lesser General Public |
| 15 | + License along with this library; if not, write to the Free Software |
| 16 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | +*/ |
13 | 18 |
|
14 |
| -#include <stdio.h> |
| 19 | +#include <string.h> |
15 | 20 |
|
16 |
| -/************************************************************************************** |
17 |
| - * FUNCTION IMPLEMENTATION |
18 |
| - **************************************************************************************/ |
| 21 | +#ifdef __cplusplus |
| 22 | +extern "C" { |
| 23 | +#endif |
19 | 24 |
|
20 |
| -std::string radixToFmtString(int const radix) |
| 25 | +char* ltoa( long value, char *string, int radix ) |
21 | 26 | {
|
22 |
| - if (radix == 8) return std::string("%o"); |
23 |
| - else if (radix == 10) return std::string("%d"); |
24 |
| - else if (radix == 16) return std::string("%X"); |
25 |
| - else throw std::runtime_error("Invalid radix."); |
26 |
| -} |
| 27 | + char tmp[33]; |
| 28 | + char *tp = tmp; |
| 29 | + long i; |
| 30 | + unsigned long v; |
| 31 | + int sign; |
| 32 | + char *sp; |
27 | 33 |
|
28 |
| -char * itoa(int value, char * str, int radix) |
29 |
| -{ |
30 |
| -#pragma GCC diagnostic push |
31 |
| -#pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
32 |
| - sprintf(str, radixToFmtString(radix).c_str(), value); |
33 |
| -#pragma GCC diagnostic pop |
34 |
| - return str; |
| 34 | + if ( string == NULL ) |
| 35 | + { |
| 36 | + return 0 ; |
| 37 | + } |
| 38 | + |
| 39 | + if (radix > 36 || radix <= 1) |
| 40 | + { |
| 41 | + return 0 ; |
| 42 | + } |
| 43 | + |
| 44 | + sign = (radix == 10 && value < 0); |
| 45 | + if (sign) |
| 46 | + { |
| 47 | + v = -value; |
| 48 | + } |
| 49 | + else |
| 50 | + { |
| 51 | + v = (unsigned long)value; |
| 52 | + } |
| 53 | + |
| 54 | + while (v || tp == tmp) |
| 55 | + { |
| 56 | + i = v % radix; |
| 57 | + v = v / radix; |
| 58 | + if (i < 10) |
| 59 | + *tp++ = i+'0'; |
| 60 | + else |
| 61 | + *tp++ = i + 'a' - 10; |
| 62 | + } |
| 63 | + |
| 64 | + sp = string; |
| 65 | + |
| 66 | + if (sign) |
| 67 | + *sp++ = '-'; |
| 68 | + while (tp > tmp) |
| 69 | + *sp++ = *--tp; |
| 70 | + *sp = 0; |
| 71 | + |
| 72 | + return string; |
35 | 73 | }
|
36 | 74 |
|
37 |
| -char * ltoa(long value, char * str, int radix) |
| 75 | +char* ultoa( unsigned long value, char *string, int radix ) |
38 | 76 | {
|
39 |
| -#pragma GCC diagnostic push |
40 |
| -#pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
41 |
| - sprintf(str, radixToFmtString(radix).c_str(), value); |
42 |
| -#pragma GCC diagnostic pop |
43 |
| - return str; |
| 77 | + char tmp[33]; |
| 78 | + char *tp = tmp; |
| 79 | + long i; |
| 80 | + unsigned long v = value; |
| 81 | + char *sp; |
| 82 | + |
| 83 | + if ( string == NULL ) |
| 84 | + { |
| 85 | + return 0; |
| 86 | + } |
| 87 | + |
| 88 | + if (radix > 36 || radix <= 1) |
| 89 | + { |
| 90 | + return 0; |
| 91 | + } |
| 92 | + |
| 93 | + while (v || tp == tmp) |
| 94 | + { |
| 95 | + i = v % radix; |
| 96 | + v = v / radix; |
| 97 | + if (i < 10) |
| 98 | + *tp++ = i+'0'; |
| 99 | + else |
| 100 | + *tp++ = i + 'a' - 10; |
| 101 | + } |
| 102 | + |
| 103 | + sp = string; |
| 104 | + |
| 105 | + |
| 106 | + while (tp > tmp) |
| 107 | + *sp++ = *--tp; |
| 108 | + *sp = 0; |
| 109 | + |
| 110 | + return string; |
44 | 111 | }
|
45 | 112 |
|
46 |
| -char * utoa(unsigned value, char *str, int radix) |
| 113 | +char* itoa( int value, char *string, int radix ) |
47 | 114 | {
|
48 |
| -#pragma GCC diagnostic push |
49 |
| -#pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
50 |
| - sprintf(str, radixToFmtString(radix).c_str(), value); |
51 |
| -#pragma GCC diagnostic pop |
52 |
| - return str; |
| 115 | + return ltoa( value, string, radix ) ; |
53 | 116 | }
|
54 | 117 |
|
55 |
| -char * ultoa(unsigned long value, char * str, int radix) |
| 118 | +char* utoa( unsigned int value, char *string, int radix ) |
56 | 119 | {
|
57 |
| -#pragma GCC diagnostic push |
58 |
| -#pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
59 |
| - sprintf(str, radixToFmtString(radix).c_str(), value); |
60 |
| -#pragma GCC diagnostic pop |
61 |
| - return str; |
| 120 | + return ultoa( value, string, radix ) ; |
62 | 121 | }
|
| 122 | + |
| 123 | +#ifdef __cplusplus |
| 124 | +} // extern "C" |
| 125 | +#endif |
0 commit comments