From 55f57e29dd1d9fb2c2142536586784b5fc56db54 Mon Sep 17 00:00:00 2001 From: Nathael Pajani Date: Thu, 17 Sep 2015 20:46:41 +0200 Subject: [PATCH] Add stdlib with strtoul() --- include/lib/stdlib.h | 34 +++++++++++++++++++++++++++++ lib/stdlib.c | 51 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 include/lib/stdlib.h create mode 100644 lib/stdlib.c diff --git a/include/lib/stdlib.h b/include/lib/stdlib.h new file mode 100644 index 0000000..f1f1efa --- /dev/null +++ b/include/lib/stdlib.h @@ -0,0 +1,34 @@ +/**************************************************************************** + * lib/stdlib.h + * + * Copyright 2014 Nathael Pajani + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + *************************************************************************** */ + +#ifndef LIB_STDLIB_H +#define LIB_STDLIB_H + +#include + +/* Simple atoi implementation. + * Returns the value convertes from the given string. + * Does not check that the base is respected aside for the use of letters in + * number representation. + */ +uint32_t strtoul(uint8_t* str, uint8_t base); + +#endif /* LIB_STDLIB_H */ + diff --git a/lib/stdlib.c b/lib/stdlib.c new file mode 100644 index 0000000..be3b2b1 --- /dev/null +++ b/lib/stdlib.c @@ -0,0 +1,51 @@ +/**************************************************************************** + * lib/stdlib.c + * + * Copyright 2014 Nathael Pajani + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + *************************************************************************** */ + +#include + +/* Simple atoi implementation. + * Returns the value convertes from the given string. + * Does not check that the base is respected aside for the use of letters in + * number representation. + */ +uint32_t strtoul(uint8_t* str, uint8_t base) +{ + uint32_t val = 0; + while (*str != '\0') { + if (*str >= '0' && *str <= '9') { + val = (val * base) + ((*str) - '0'); + str++; + continue; + } + if (*str >= 'A' && *str <= 'F' && base > 10) { + val = (val * base) + ((*str) - 'A' + 10); + str++; + continue; + } + if (*str >= 'a' && *str <= 'f' && base > 10) { + val = (val * base) + ((*str) - 'a' + 10); + str++; + continue; + } + break; + } + return val; +} + -- 2.43.0