From 613d405fd452609087464080c3ea59990549a154 Mon Sep 17 00:00:00 2001 From: patacongo Date: Sun, 14 Jun 2009 15:36:18 +0000 Subject: [PATCH] Add strtoul, strtoll, strtoull, atol, and atoll. git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@1883 42af7a65-404d-4744-a932-0658087f49c3 --- ChangeLog | 2 + Documentation/NuttX.html | 4 +- TODO | 9 ++- include/stdlib.h | 14 ++++- lib/Makefile | 15 ++--- lib/lib_checkbase.c | 114 ++++++++++++++++++++++++++++++++++ lib/lib_internal.h | 12 ++++ lib/lib_isbasedigit.c | 103 ++++++++++++++++++++++++++++++ lib/lib_skipspace.c | 69 +++++++++++++++++++++ lib/lib_strtol.c | 131 +++++++++------------------------------ lib/lib_strtoll.c | 105 +++++++++++++++++++++++++++++++ lib/lib_strtoul.c | 97 +++++++++++++++++++++++++++++ lib/lib_strtoull.c | 99 +++++++++++++++++++++++++++++ 13 files changed, 660 insertions(+), 114 deletions(-) create mode 100644 lib/lib_checkbase.c create mode 100644 lib/lib_isbasedigit.c create mode 100644 lib/lib_skipspace.c create mode 100644 lib/lib_strtoll.c create mode 100644 lib/lib_strtoul.c create mode 100644 lib/lib_strtoull.c diff --git a/ChangeLog b/ChangeLog index c863f37d83..3fac746139 100644 --- a/ChangeLog +++ b/ChangeLog @@ -775,3 +775,5 @@ 0.4.8 2009-xx-xx Gregory Nutt + * Add strtoll() and strtoull(); Add macros for atol() and atoll(). + diff --git a/Documentation/NuttX.html b/Documentation/NuttX.html index 3235811d5c..044938d8d9 100644 --- a/Documentation/NuttX.html +++ b/Documentation/NuttX.html @@ -8,7 +8,7 @@

NuttX RTOS

-

Last Updated: June 13, 2009

+

Last Updated: June 14, 2009

@@ -1470,6 +1470,8 @@ buildroot-0.1.6 2009-xx-xx <spudmonkey@racsa.co.cr>
    nuttx-0.4.9 2009-xx-xx Gregory Nutt <spudmonkey@racsa.co.cr> + * Add strtoll() and strtoull(); Add macros for atol() and atoll(). + pascal-0.1.3 2009-xx-xx Gregory Nutt <spudmonkey@racsa.co.cr> buildroot-0.1.7 2009-xx-xx <spudmonkey@racsa.co.cr> diff --git a/TODO b/TODO index 001e511bb8..78c6870006 100644 --- a/TODO +++ b/TODO @@ -18,7 +18,7 @@ NuttX TODO List (Last updated April 12, 2009) (2) NuttShell (NSH) (examples/nsh) (3) Other Applications & Tests (examples/) (1) Linux/Cywgin simulation (arch/sim) - (2) ARM (arch/arm/) + (3) ARM (arch/arm/) (1) ARM/C5471 (arch/arm/src/c5471/) (3) ARM/DM320 (arch/arm/src/dm320/) (2) ARM/i.MX (arch/arm/src/imx/) @@ -428,6 +428,13 @@ o ARM (arch/arm/) Status: Open Priority: Low + Description: The Cortex-M3 user context swich logic uses SVCall instructions. + This user context switching time could be improved by eliminating + the SVCalls and developing assembly language implementations + of the context save and restore logic. + Status: Open + Priority: Low + o ARM/C5471 (arch/arm/src/c5471/) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/include/stdlib.h b/include/stdlib.h index 92437a574c..6459987543 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -120,9 +120,19 @@ EXTERN int atexit(void (*func)(void)); /* String to binary conversions */ -#define atoi(nptr) strtol((nptr), (FAR char**)NULL, 10) EXTERN long strtol(const char *, char **, int); -EXTERN double_t strtod(const char *, char **); +EXTERN unsigned long strtoul(const char *, char **, int); +#ifdef CONFIG_HAVE_LONG_LONG +EXTERN long long strtoll(const char *, char **, int); +EXTERN unsigned long long strtoull(const char *, char **, int); +#endif +EXTERN double_t strtod(const char *, char **); + +#define atoi(nptr) strtol((nptr), NULL, 10); +#define atol(nptr) strtol((nptr), NULL, 10); +#ifdef CONFIG_HAVE_LONG_LONG +#define atoll(nptr) strtoll((nptr), NULL, 10); +#endif /* Memory Management */ diff --git a/lib/Makefile b/lib/Makefile index 9b023d04e1..bef36aad84 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -1,7 +1,7 @@ ############################################################################ # lib/Makefile # -# Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved. +# Copyright (C) 2007-2009 Gregory Nutt. All rights reserved. # Author: Gregory Nutt # # Redistribution and use in source and binary forms, with or without @@ -43,12 +43,13 @@ ifneq ($(CONFIG_NFILE_STREAMS),0) MISC_SRCS += lib_streamsem.c endif -STRING_SRCS = lib_memset.c lib_memcpy.c lib_memcmp.c lib_memmove.c \ - lib_strcpy.c lib_strncpy.c lib_strcmp.c lib_strncmp.c \ - lib_strcasecmp.c lib_strncasecmp.c lib_strlen.c lib_strdup.c \ - lib_strcat.c lib_strncat.c lib_strtol.c lib_strchr.c \ - lib_strrchr.c lib_strspn.c lib_strcspn.c lib_strtok.c \ - lib_strtokr.c lib_strerror.c +STRING_SRCS = lib_checkbase.c lib_isbasedigit.c lib_memset.c lib_memcpy.c \ + lib_memcmp.c lib_memmove.c lib_skipspace.c lib_strcasecmp.c \ + lib_strcat.c lib_strchr.c lib_strcpy.c lib_strcmp.c lib_strcspn.c \ + lib_strdup.c lib_strerror.c lib_strlen.c lib_strncasecmp.c \ + lib_strncat.c lib_strncmp.c lib_strncpy.c lib_strrchr.c \ + lib_strspn.c lib_strtok.c lib_strtokr.c lib_strtol.c lib_strtoll.c \ + lib_strtoul.c lib_strtoull.c CTYPE_SRCS = diff --git a/lib/lib_checkbase.c b/lib/lib_checkbase.c new file mode 100644 index 0000000000..bf4dadf02d --- /dev/null +++ b/lib/lib_checkbase.c @@ -0,0 +1,114 @@ +/**************************************************************************** + * lib_checkbase.c + * + * Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include +#include +#include "lib_internal.h" + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: lib_checkbase + * + * Description: + * This is part of the strol() family implementation. This function checks + * the initial part of a string to see if it can determine the numeric + * base that is represented. + * + * Assumptions: + * *ptr points to the first, non-whitespace character in the string. + * + ****************************************************************************/ + +int lib_checkbase(int base, const char **pptr) +{ + const char *ptr = *pptr; + + /* Check for unspecified base */ + + if (!base) + { + /* Assume base 10 */ + + base = 10; + + /* Check for leading '0' - that would signify octal or hex (or binary) */ + + if (*ptr == '0') + { + /* Assume octal */ + + base = 8; + ptr++; + + /* Check for hexidecimal */ + + if ((*ptr == 'X' || *ptr == 'x') && + lib_isbasedigit(ptr[1], 16, NULL)) + { + base = 16; + ptr++; + } + } + } + + /* If it a hexidecimal representation, than discard any leading "0X" or "0x" */ + + else if (base == 16) + { + if (ptr[0] == '0' && (ptr[1] == 'X' || ptr[1] == 'x')) + { + ptr += 2; + } + } + + /* Return the updated pointer and base */ + + *pptr = ptr; + return base; +} + diff --git a/lib/lib_internal.h b/lib/lib_internal.h index f6eabe6bca..9e5b55f932 100644 --- a/lib/lib_internal.h +++ b/lib/lib_internal.h @@ -131,4 +131,16 @@ extern void lib_give_semaphore(FAR struct file_struct *stream); extern int lib_getbase(const char *nptr, const char **endptr); +/* Defined in lib_skipspace.c */ + +extern void lib_skipspace(const char **pptr); + +/* Defined in lib_isbasedigit.c */ + +extern boolean lib_isbasedigit(int ch, int base, int *value); + +/* Defined in lib_checkbase.c */ + +extern int lib_checkbase(int base, const char **pptr); + #endif /* __LIB_INTERNAL_H */ diff --git a/lib/lib_isbasedigit.c b/lib/lib_isbasedigit.c new file mode 100644 index 0000000000..c3976ea86a --- /dev/null +++ b/lib/lib_isbasedigit.c @@ -0,0 +1,103 @@ +/**************************************************************************** + * lib/lib_isbasedigit.c + * + * Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include +#include +#include "lib_internal.h" + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: lib_isbasedigit + * + * Description: + * Given an ASCII character, ch, and a base (1-36) do two + * things: 1) Determine if ch is a valid charcter, and 2) + * convert ch to its binary value. + * + ****************************************************************************/ + +boolean lib_isbasedigit(int ch, int base, int *value) +{ + boolean ret = FALSE; + int tmp = 0; + + if (base <= 10) + { + if (ch >= '0' && ch <= base + '0' - 1) + { + tmp = ch - '0'; + ret = TRUE; + } + } + else if (base <= 36) + { + if (ch >= '0' && ch <= '9') + { + tmp = ch - '0'; + ret =TRUE; + } + else if (ch >= 'a' && ch <= 'a' + base - 11) + { + tmp = ch - 'a' + 10; + ret = TRUE; + } + else if (ch >= 'A' && ch <= 'A' + base - 11) + { + tmp = ch - 'A' + 10; + ret = TRUE; + } + } + + if (value) + { + *value = tmp; + } + return ret; +} + + diff --git a/lib/lib_skipspace.c b/lib/lib_skipspace.c new file mode 100644 index 0000000000..d111645e78 --- /dev/null +++ b/lib/lib_skipspace.c @@ -0,0 +1,69 @@ +/**************************************************************************** + * lib/lib_skipspace.c + * + * Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include +#include +#include "lib_internal.h" + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: lib_skipspace + * + * Description: + * Skip over leading whitespace + * + ****************************************************************************/ + +void lib_skipspace(const char **pptr) +{ + const char *ptr = *pptr; + while (isspace(*ptr)) ptr++; + *pptr = ptr; +} + + diff --git a/lib/lib_strtol.c b/lib/lib_strtol.c index 8e2d26f3f6..dcd3765236 100644 --- a/lib/lib_strtol.c +++ b/lib/lib_strtol.c @@ -1,4 +1,4 @@ -/************************************************************ +/**************************************************************************** * lib_strtol.c * * Copyright (C) 2007 Gregory Nutt. All rights reserved. @@ -14,7 +14,7 @@ * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. - * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * 3. Neither the name NuttX nor the names of its contributors may be * used to endorse or promote products derived from this software * without specific prior written permission. * @@ -31,80 +31,41 @@ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Included Files - ************************************************************/ + ****************************************************************************/ #include #include -#include -#include +#include +#include "lib_internal.h" -/************************************************************ +/**************************************************************************** * Private Functions - ************************************************************/ + ****************************************************************************/ -/* Skip leading spaces */ - -static void lib_skipspace(const char **nptr) -{ - register const char *tmp = *nptr; - while (isspace(*tmp)) tmp++; - *nptr = tmp; -} - -static int lib_isbasedigit(int c, int base, int *value) -{ - int tmp = 0; - int ret = 0; - - if (base <= 10) - { - if (c >= '0' && c <= base + '0' - 1) - { - tmp = c - '0'; - ret = 1; - } - } - else if (base <= 36) - { - if (c >= '0' && c <= '9') - { - tmp = c - '0'; - ret = 1; - } - else if (c >= 'a' && c <= 'a' + base - 11) - { - tmp = c - 'a' + 10; - ret = 1; - } - else if (c >= 'A' && c <= 'A' + base - 11) - { - tmp = c - 'A' + 10; - ret = 1; - } - } - - if (value) - { - *value = tmp; - } - return ret; -} - -/************************************************************ +/**************************************************************************** * Public Functions - ************************************************************/ - -/* Limited to base 1-36 */ + ****************************************************************************/ +/**************************************************************************** + * Name: strtol + * + * Description: + * The strtol() function converts the initial part of the string in + * nptr to a long integer value according to the given base, which must be + * between 2 and 36 inclusive, or be the special value 0. + * + * Warning: does not check for integer overflow! + * + ****************************************************************************/ + long strtol(const char *nptr, char **endptr, int base) { unsigned long accum = 0; - int value; - int negate = 0; + boolean negate = FALSE; if (nptr) { @@ -116,51 +77,19 @@ long strtol(const char *nptr, char **endptr, int base) if (*nptr == '-') { - negate = 1; + negate = TRUE; nptr++; - lib_skipspace(&nptr); } else if (*nptr == '+') { nptr++; - lib_skipspace(&nptr); } - /* Check for unspecified base */ + /* Get the unsigned value */ - if (!base) - { - base = 10; - if (*nptr == '0') - { - base = 8; - nptr++; - if ((*nptr == 'X' || *nptr == 'x') && - lib_isbasedigit(nptr[1], 16, NULL)) - { - base = 16; - nptr++; - } - } - } - else if (base == 16) - { - if (nptr[0] == '0' && (nptr[1] == 'X' || nptr[1] == 'x')) - { - nptr += 2; - } - } + accum = strtoul(nptr, endptr, base); - while (lib_isbasedigit(*nptr, base, &value)) - { - accum = accum*base + value; - nptr++; - } - - if (endptr) - { - *endptr = (char *)nptr; - } + /* Correct the sign of the result */ if (negate) { @@ -170,7 +99,3 @@ long strtol(const char *nptr, char **endptr, int base) return (long)accum; } -unsigned long strtoul(const char *nptr, char **endptr, int base) -{ - return (unsigned long)strtol(nptr, endptr, base); -} diff --git a/lib/lib_strtoll.c b/lib/lib_strtoll.c new file mode 100644 index 0000000000..6566d75c62 --- /dev/null +++ b/lib/lib_strtoll.c @@ -0,0 +1,105 @@ +/**************************************************************************** + * lib_strtoll.c + * + * Copyright (C) 2009 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include +#include "lib_internal.h" + +#ifdef CONFIG_HAVE_LONG_LONG + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: strtoll + * + * Description: + * The strtol() function converts the initial part of the string in + * nptr to a long long integer value according to the given base, which + * must be between 2 and 36 inclusive, or be the special value 0. + * + * Warning: does not check for integer overflow! + * + ****************************************************************************/ + +long long strtoll(const char *nptr, char **endptr, int base) +{ + unsigned long long accum = 0; + boolean negate = FALSE; + + if (nptr) + { + /* Skip leading spaces */ + + lib_skipspace(&nptr); + + /* Check for leading + or - */ + + if (*nptr == '-') + { + negate = TRUE; + nptr++; + } + else if (*nptr == '+') + { + nptr++; + } + + /* Get the unsigned value */ + + accum = strtoull(nptr, endptr, base); + + /* Correct the sign of the result */ + + if (negate) + { + return -(long long)accum; + } + } + return (long long)accum; +} + +#endif + diff --git a/lib/lib_strtoul.c b/lib/lib_strtoul.c new file mode 100644 index 0000000000..6479d60c16 --- /dev/null +++ b/lib/lib_strtoul.c @@ -0,0 +1,97 @@ +/**************************************************************************** + * lib/lib_strtoul.c + * + * Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include +#include "lib_internal.h" + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: strtoul + * + * Description: + * The strtol() function converts the initial part of the string in + * nptr to a long unsigned integer value according to the given base, which + * must be between 2 and 36 inclusive, or be the special value 0. + * + * Warning: does not check for integer overflow! + * + ****************************************************************************/ + +unsigned long strtoul(const char *nptr, char **endptr, int base) +{ + unsigned long accum = 0; + int value; + + if (nptr) + { + /* Skip leading spaces */ + + lib_skipspace(&nptr); + + /* Check for unspecified base */ + + base = lib_checkbase(base, &nptr); + + /* Accumulate each "digit" */ + + while (lib_isbasedigit(*nptr, base, &value)) + { + accum = accum*base + value; + nptr++; + } + + /* Return the final pointer to the unused value */ + + if (endptr) + { + *endptr = (char *)nptr; + } + } + return accum; +} + diff --git a/lib/lib_strtoull.c b/lib/lib_strtoull.c new file mode 100644 index 0000000000..2a135b3c87 --- /dev/null +++ b/lib/lib_strtoull.c @@ -0,0 +1,99 @@ +/**************************************************************************** + * lib/lib_strtoull.c + * + * Copyright (C) 2009 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include +#include +#include "lib_internal.h" + +#ifdef CONFIG_HAVE_LONG_LONG + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: strtoull + * + * Description: + * The strtol() function converts the initial part of the string in + * nptr to a long unsigned integer value according to the given base, which + * must be between 2 and 36 inclusive, or be the special value 0. + * + ****************************************************************************/ + +unsigned long long strtoull(const char *nptr, char **endptr, int base) +{ + unsigned long long accum = 0; + int value; + + if (nptr) + { + /* Skip leading spaces */ + + lib_skipspace(&nptr); + + /* Check for unspecified base */ + + base = lib_checkbase(base, &nptr); + + /* Accumulate each "digit" */ + + while (lib_isbasedigit(*nptr, base, &value)) + { + accum = accum*base + value; + nptr++; + } + + /* Return the final pointer to the unused value */ + + if (endptr) + { + *endptr = (char *)nptr; + } + } + return accum; +} +#endif +