mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-21 10:26:06 +03:00
199 lines
4.4 KiB
C
199 lines
4.4 KiB
C
/*
|
|
core_esp8266_noniso.c - nonstandard (but usefull) conversion functions
|
|
|
|
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
|
|
This file is part of the esp8266 core for Arduino environment.
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU Lesser General Public
|
|
License as published by the Free Software Foundation; either
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
License along with this library; if not, write to the Free Software
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
Modified 03 April 2015 by Markus Sattler
|
|
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
#include "stdlib_noniso.h"
|
|
#include "ets_sys.h"
|
|
|
|
#define sprintf ets_sprintf
|
|
#define strcpy ets_strcpy
|
|
|
|
int atoi(const char* s) {
|
|
return (int) atol(s);
|
|
}
|
|
|
|
long atol(const char* s) {
|
|
char * tmp;
|
|
return strtol(s, &tmp, 10);
|
|
}
|
|
|
|
double atof(const char* s) {
|
|
char * tmp;
|
|
return strtod(s, &tmp);
|
|
}
|
|
|
|
void reverse(char* begin, char* end) {
|
|
char *is = begin;
|
|
char *ie = end - 1;
|
|
while(is < ie) {
|
|
char tmp = *ie;
|
|
*ie = *is;
|
|
*is = tmp;
|
|
++is;
|
|
--ie;
|
|
}
|
|
}
|
|
|
|
char* itoa(int value, char* result, int base) {
|
|
if(base < 2 || base > 16) {
|
|
*result = 0;
|
|
return result;
|
|
}
|
|
|
|
char* out = result;
|
|
int quotient = abs(value);
|
|
|
|
do {
|
|
const int tmp = quotient / base;
|
|
*out = "0123456789abcdef"[quotient - (tmp * base)];
|
|
++out;
|
|
quotient = tmp;
|
|
} while(quotient);
|
|
|
|
// Apply negative sign
|
|
if(value < 0)
|
|
*out++ = '-';
|
|
|
|
reverse(result, out);
|
|
*out = 0;
|
|
return result;
|
|
}
|
|
|
|
char* ltoa(long value, char* result, int base) {
|
|
if(base < 2 || base > 16) {
|
|
*result = 0;
|
|
return result;
|
|
}
|
|
|
|
char* out = result;
|
|
long quotient = abs(value);
|
|
|
|
do {
|
|
const long tmp = quotient / base;
|
|
*out = "0123456789abcdef"[quotient - (tmp * base)];
|
|
++out;
|
|
quotient = tmp;
|
|
} while(quotient);
|
|
|
|
// Apply negative sign
|
|
if(value < 0)
|
|
*out++ = '-';
|
|
|
|
reverse(result, out);
|
|
*out = 0;
|
|
return result;
|
|
}
|
|
|
|
char* utoa(unsigned value, char* result, int base) {
|
|
if(base < 2 || base > 16) {
|
|
*result = 0;
|
|
return result;
|
|
}
|
|
|
|
char* out = result;
|
|
unsigned quotient = value;
|
|
|
|
do {
|
|
const unsigned tmp = quotient / base;
|
|
*out = "0123456789abcdef"[quotient - (tmp * base)];
|
|
++out;
|
|
quotient = tmp;
|
|
} while(quotient);
|
|
|
|
reverse(result, out);
|
|
*out = 0;
|
|
return result;
|
|
}
|
|
|
|
char* ultoa(unsigned long value, char* result, int base) {
|
|
if(base < 2 || base > 16) {
|
|
*result = 0;
|
|
return result;
|
|
}
|
|
|
|
char* out = result;
|
|
unsigned long quotient = value;
|
|
|
|
do {
|
|
const unsigned long tmp = quotient / base;
|
|
*out = "0123456789abcdef"[quotient - (tmp * base)];
|
|
++out;
|
|
quotient = tmp;
|
|
} while(quotient);
|
|
|
|
reverse(result, out);
|
|
*out = 0;
|
|
return result;
|
|
}
|
|
|
|
char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
|
|
|
|
if(isnan(number)) {
|
|
strcpy(s, "nan");
|
|
return s;
|
|
}
|
|
if(isinf(number)) {
|
|
strcpy(s, "inf");
|
|
return s;
|
|
}
|
|
|
|
if(number > 4294967040.0 || number < -4294967040.0) {
|
|
strcpy(s, "ovf");
|
|
return s;
|
|
}
|
|
char* out = s;
|
|
// Handle negative numbers
|
|
if(number < 0.0) {
|
|
*out = '-';
|
|
++out;
|
|
number = -number;
|
|
}
|
|
|
|
// Round correctly so that print(1.999, 2) prints as "2.00"
|
|
double rounding = 0.5;
|
|
for(uint8_t i = 0; i < prec; ++i)
|
|
rounding /= 10.0;
|
|
|
|
number += rounding;
|
|
|
|
// Extract the integer part of the number and print it
|
|
unsigned long int_part = (unsigned long) number;
|
|
double remainder = number - (double) int_part;
|
|
out += sprintf(out, "%ld", int_part);
|
|
|
|
// Print the decimal point, but only if there are digits beyond
|
|
if(prec > 0) {
|
|
*out = '.';
|
|
++out;
|
|
}
|
|
|
|
for (unsigned char decShift = prec; decShift > 0; decShift--) {
|
|
remainder *= 10.0;
|
|
}
|
|
sprintf(out, "%0*d", prec, (int)remainder);
|
|
|
|
return s;
|
|
}
|
|
|