diff options
author | px4dev <px4@purgatory.org> | 2013-01-11 00:39:22 -0800 |
---|---|---|
committer | px4dev <px4@purgatory.org> | 2013-01-11 00:39:22 -0800 |
commit | ccf9882dc5dbe38b621110f82c4e2ff63aef900e (patch) | |
tree | 18b0af628174bf27815dd52a376c8b72b6a626d4 /nuttx/libc | |
parent | 40dfbf0d977729951d73bcb089ca8f89c7b83efe (diff) | |
parent | 0f2decb70f505b108999fcdb80e89d7aae6760ce (diff) | |
download | px4-firmware-ccf9882dc5dbe38b621110f82c4e2ff63aef900e.tar.gz px4-firmware-ccf9882dc5dbe38b621110f82c4e2ff63aef900e.tar.bz2 px4-firmware-ccf9882dc5dbe38b621110f82c4e2ff63aef900e.zip |
Merge branch 'master' into nuttx-merge-5447
Diffstat (limited to 'nuttx/libc')
-rw-r--r-- | nuttx/libc/math/Make.defs | 62 | ||||
-rw-r--r-- | nuttx/libc/math/lib_round.c | 40 | ||||
-rw-r--r-- | nuttx/libc/math/lib_roundf.c | 38 | ||||
-rw-r--r-- | nuttx/libc/math/lib_roundl.c | 40 | ||||
-rw-r--r-- | nuttx/libc/stdlib/lib_rand.c | 277 | ||||
-rw-r--r-- | nuttx/libc/string/lib_memcmp.c | 74 | ||||
-rw-r--r-- | nuttx/libc/string/lib_memmove.c | 77 | ||||
-rw-r--r-- | nuttx/libc/string/lib_strcasestr.c | 136 | ||||
-rw-r--r-- | nuttx/libc/string/lib_strstr.c | 106 |
9 files changed, 850 insertions, 0 deletions
diff --git a/nuttx/libc/math/Make.defs b/nuttx/libc/math/Make.defs new file mode 100644 index 000000000..ece25f4e5 --- /dev/null +++ b/nuttx/libc/math/Make.defs @@ -0,0 +1,62 @@ +############################################################################ +# libc/math/Make.defs +# +# Copyright (C) 2012 Gregory Nutt. All rights reserved. +# Author: Gregory Nutt <gnutt@nuttx.org> +# +# 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. +# +############################################################################ + +ifeq ($(CONFIG_LIBM),y) + +# Add the floating point math C files to the build + +CSRCS += lib_acosf.c lib_asinf.c lib_atan2f.c lib_atanf.c lib_ceilf.c lib_cosf.c +CSRCS += lib_coshf.c lib_expf.c lib_fabsf.c lib_floorf.c lib_fmodf.c lib_frexpf.c +CSRCS += lib_ldexpf.c lib_logf.c lib_log10f.c lib_log2f.c lib_modff.c lib_powf.c +CSRCS += lib_roundf.c lib_sinf.c lib_sinhf.c lib_sqrtf.c lib_tanf.c lib_tanhf.c + +CSRCS += lib_acos.c lib_asin.c lib_atan.c lib_atan2.c lib_ceil.c lib_cos.c +CSRCS += lib_cosh.c lib_exp.c lib_fabs.c lib_floor.c lib_fmod.c lib_frexp.c +CSRCS += lib_ldexp.c lib_log.c lib_log10.c lib_log2.c lib_modf.c lib_pow.c +CSRCS += lib_round.c lib_sin.c lib_sinh.c lib_sqrt.c lib_tan.c lib_tanh.c + +CSRCS += lib_acosl.c lib_asinl.c lib_atan2l.c lib_atanl.c lib_ceill.c lib_cosl.c +CSRCS += lib_coshl.c lib_expl.c lib_fabsl.c lib_floorl.c lib_fmodl.c lib_frexpl.c +CSRCS += lib_ldexpl.c lib_logl.c lib_log10l.c lib_log2l.c lib_modfl.c lib_powl.c +CSRCS += lib_roundl.c lib_sinl.c lib_sinhl.c lib_sqrtl.c lib_tanl.c lib_tanhl.c + +CSRCS += lib_libexpi.c lib_libsqrtapprox.c + +# Add the floating point math directory to the build + +DEPPATH += --dep-path math +VPATH += :math + +endif diff --git a/nuttx/libc/math/lib_round.c b/nuttx/libc/math/lib_round.c new file mode 100644 index 000000000..6191cee5b --- /dev/null +++ b/nuttx/libc/math/lib_round.c @@ -0,0 +1,40 @@ +/************************************************************************ + * lib/math/lib_round.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * (C) 2012 Petteri Aimonen <jpa@nx.mail.kapsi.fi> + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#ifdef CONFIG_HAVE_DOUBLE +double round(double x) +{ + double f = modf(x, &x); + if (x <= 0.0 && f <= -0.5) + { + x -= 1.0; + } + + if (x >= 0.0 && f >= 0.5) + { + x += 1.0; + } + + return x; +} +#endif diff --git a/nuttx/libc/math/lib_roundf.c b/nuttx/libc/math/lib_roundf.c new file mode 100644 index 000000000..145cf3df6 --- /dev/null +++ b/nuttx/libc/math/lib_roundf.c @@ -0,0 +1,38 @@ +/************************************************************************ + * lib/math/lib_roundf.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * (C) 2012 Petteri Aimonen <jpa@nx.mail.kapsi.fi> + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +float roundf(float x) +{ + float f = modff(x, &x); + if (x <= 0.0f && f <= -0.5f) + { + x -= 1.0f; + } + + if (x >= 0.0f && f >= 0.5f) + { + x += 1.0f; + } + + return x; +} diff --git a/nuttx/libc/math/lib_roundl.c b/nuttx/libc/math/lib_roundl.c new file mode 100644 index 000000000..b2ddba670 --- /dev/null +++ b/nuttx/libc/math/lib_roundl.c @@ -0,0 +1,40 @@ +/************************************************************************ + * lib/math/lib_round.c + * + * This file is a part of NuttX: + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * (C) 2012 Petteri Aimonen <jpa@nx.mail.kapsi.fi> + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <math.h> + +/************************************************************************ + * Public Functions + ************************************************************************/ + +#ifdef CONFIG_HAVE_LONG_DOUBLE +long double roundl(long double x) +{ + long double f = modfl(x, &x); + if (x <= 0.0 && f <= -0.5) + { + x -= 1.0; + } + + if (x >= 0.0 && f >= 0.5) + { + x += 1.0; + } + + return x; +} +#endif diff --git a/nuttx/libc/stdlib/lib_rand.c b/nuttx/libc/stdlib/lib_rand.c new file mode 100644 index 000000000..453a4537a --- /dev/null +++ b/nuttx/libc/stdlib/lib_rand.c @@ -0,0 +1,277 @@ +/**************************************************************************** + * libc/stdlib/lib_rand.c + * + * Copyright (C) 2007, 2011 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <gnutt@nuttx.org> + * + * 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 <sys/types.h> +#include <stdlib.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ +/* First, second, and thired order congruential generators are supported */ + +#ifndef CONFIG_LIB_RAND_ORDER +# define CONFIG_LIB_RAND_ORDER 1 +#endif + +#if CONFIG_LIB_RAND_ORDER > 3 +# undef CONFIG_LIB_RAND_ORDER +# define CONFIG_LIB_RAND_ORDER 3 +#endif + +/* Values needed by the random number generator */ + +#define RND1_CONSTK 470001 +#define RND1_CONSTP 999563 +#define RND2_CONSTK1 366528 +#define RND2_CONSTK2 508531 +#define RND2_CONSTP 998917 +#define RND3_CONSTK1 360137 +#define RND3_CONSTK2 519815 +#define RND3_CONSTK3 616087 +#define RND3_CONSTP 997783 + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static unsigned int nrand(unsigned int nLimit); + +/* First order congruential generators */ + +static inline unsigned long fgenerate1(void); +#if (CONFIG_LIB_RAND_ORDER == 1) +static double_t frand1(void); +#endif + +/* Second order congruential generators */ + +#if (CONFIG_LIB_RAND_ORDER > 1) +static inline unsigned long fgenerate2(void); +#if (CONFIG_LIB_RAND_ORDER == 2) +static double_t frand2(void); +#endif + +/* Third order congruential generators */ + +#if (CONFIG_LIB_RAND_ORDER > 2) +static inline unsigned long fgenerate3(void); +static double_t frand3(void); +#endif +#endif + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static unsigned long g_randint1; +#if (CONFIG_LIB_RAND_ORDER > 1) +static unsigned long g_randint2; +#if (CONFIG_LIB_RAND_ORDER > 2) +static unsigned long g_randint3; +#endif +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static unsigned int nrand(unsigned int nLimit) +{ + unsigned long result; + double_t ratio; + + /* Loop to be sure a legal random number is generated */ + + do + { + /* Get a random integer in the requested range */ + +#if (CONFIG_LIB_RAND_ORDER == 1) + ratio = frand1(); +#elif (CONFIG_LIB_RAND_ORDER == 2) + ratio = frand2(); +#else /* if (CONFIG_LIB_RAND_ORDER > 2) */ + ratio = frand3(); +#endif + + /* Then, produce the return-able value */ + + result = (unsigned long)(((double_t)nLimit) * ratio); + } + while (result >= (unsigned long)nLimit); + + return (unsigned int)result; +} + +/* First order congruential generators */ + +static inline unsigned long fgenerate1(void) +{ + unsigned long randint; + + /* First order congruential generator. One may be added to the result of the + * generated value to avoid the value zero. This would be fatal for the + * first order random number generator. + */ + + randint = (RND1_CONSTK * g_randint1) % RND1_CONSTP; + g_randint1 = (randint == 0 ? 1 : randint); + return randint; +} + +#if (CONFIG_LIB_RAND_ORDER == 1) +static double_t frand1(void) +{ + /* First order congruential generator. */ + + unsigned long randint = fgenerate1(); + + /* Construct an floating point value in the range from 0.0 up to 1.0 */ + + return ((double_t)randint) / ((double_t)RND1_CONSTP); +} +#endif + +/* Second order congruential generators */ + +#if (CONFIG_LIB_RAND_ORDER > 1) +static inline unsigned long fgenerate2(void) +{ + unsigned long randint; + + /* Second order congruential generator. */ + + randint = (RND2_CONSTK1 * g_randint1 + + RND2_CONSTK2 * g_randint2) % RND2_CONSTP; + + g_randint2 = g_randint1; + g_randint1 = randint; + + /* We cannot permit both values to become zero. That would be fatal for the + * second order random number generator. + */ + + if (g_randint2 == 0 && g_randint1 == 0) + { + g_randint2 = 1; + } + + return randint; +} + +#if (CONFIG_LIB_RAND_ORDER == 2) +static double_t frand2(void) +{ + /* Second order congruential generator */ + + unsigned long randint = fgenerate2(); + + /* Construct an floating point value in the range from 0.0 up to 1.0 */ + + return ((double_t)randint) / ((double_t)RND2_CONSTP); +} +#endif + +/* Third order congruential generators */ + +#if (CONFIG_LIB_RAND_ORDER > 2) +static inline unsigned long fgenerate3(void) +{ + unsigned long randint; + + /* Third order congruential generator. */ + + randint = (RND3_CONSTK1 * g_randint1 + + RND3_CONSTK2 * g_randint2 + + RND3_CONSTK2 * g_randint3) % RND3_CONSTP; + + g_randint3 = g_randint2; + g_randint2 = g_randint1; + g_randint1 = randint; + + /* We cannot permit all three values to become zero. That would be fatal for the + * third order random number generator. + */ + + if (g_randint3 == 0 && g_randint2 == 0 && g_randint1 == 0) + { + g_randint3 = 1; + } + + return randint; +} + +static double_t frand3(void) +{ + /* Third order congruential generator */ + + unsigned long randint = fgenerate3(); + + /* Construct an floating point value in the range from 0.0 up to 1.0 */ + + return ((double_t)randint) / ((double_t)RND3_CONSTP); +} +#endif +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Function: srand, rand + ****************************************************************************/ + +void srand(unsigned int seed) +{ + g_randint1 = seed; +#if (CONFIG_LIB_RAND_ORDER > 1) + g_randint2 = seed; + (void)fgenerate1(); +#if (CONFIG_LIB_RAND_ORDER > 2) + g_randint3 = seed; + (void)fgenerate2(); +#endif +#endif +} + +int rand(void) +{ + return (int)nrand(32768); +} diff --git a/nuttx/libc/string/lib_memcmp.c b/nuttx/libc/string/lib_memcmp.c new file mode 100644 index 000000000..5434bb847 --- /dev/null +++ b/nuttx/libc/string/lib_memcmp.c @@ -0,0 +1,74 @@ +/************************************************************ + * libc/string/lib_memcmp.c + * + * Copyright (C) 2007, 2011-2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <gnutt@nuttx.org> + * + * 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. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <nuttx/config.h> +#include <sys/types.h> +#include <string.h> + +/************************************************************ + * Global Functions + ************************************************************/ + +#ifndef CONFIG_ARCH_MEMCMP +int memcmp(FAR const void *s1, FAR const void *s2, size_t n) +{ + unsigned char *p1 = (unsigned char *)s1; + unsigned char *p2 = (unsigned char *)s2; + + while (n-- > 0) + { + if (*p1 < *p2) + { + return -1; + } + else if (*p1 > *p2) + { + return 1; + } + + p1++; + p2++; + } + return 0; +} +#endif diff --git a/nuttx/libc/string/lib_memmove.c b/nuttx/libc/string/lib_memmove.c new file mode 100644 index 000000000..cc8317223 --- /dev/null +++ b/nuttx/libc/string/lib_memmove.c @@ -0,0 +1,77 @@ +/************************************************************ + * libc/string/lib_memmove.c + * + * Copyright (C) 2007, 2011 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <gnutt@nuttx.org> + * + * 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. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <nuttx/config.h> +#include <sys/types.h> +#include <string.h> + +/************************************************************ + * Global Functions + ************************************************************/ + +#ifndef CONFIG_ARCH_MEMMOVE +FAR void *memmove(FAR void *dest, FAR const void *src, size_t count) +{ + char *tmp, *s; + if (dest <= src) + { + tmp = (char*) dest; + s = (char*) src; + while (count--) + { + *tmp++ = *s++; + } + } + else + { + tmp = (char*) dest + count; + s = (char*) src + count; + while (count--) + { + *--tmp = *--s; + } + } + + return dest; +} +#endif diff --git a/nuttx/libc/string/lib_strcasestr.c b/nuttx/libc/string/lib_strcasestr.c new file mode 100644 index 000000000..7f17a686d --- /dev/null +++ b/nuttx/libc/string/lib_strcasestr.c @@ -0,0 +1,136 @@ +/**************************************************************************** + * libc/string/lib_strstr.c + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <gnutt@nuttx.org> + * + * Redistribution and use str 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 str binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer str + * 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 <nuttx/config.h> + +#include <string.h> +#include <ctype.h> + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static FAR char *strcasechr(FAR const char *s, int uc) +{ + register char ch; + + if (s) + { + for (; *s; s++) + { + ch = *s; + if (toupper(ch) == uc) + { + return (FAR char*)s; + } + } + } + + return NULL; +} + +/**************************************************************************** + * Global Functions + ****************************************************************************/ + +FAR char *strcasestr(FAR const char *str, FAR const char *substr) +{ + const char *candidate; /* Candidate in str with matching start character */ + char ch; /* First character of the substring */ + int len; /* The length of the substring */ + + /* Special case the empty substring */ + + len = strlen(substr); + ch = *substr; + + if (!ch) + { + /* We'll say that an empty substring matches at the beginning of + * the string + */ + + return (char*)str; + } + + /* Search for the substring */ + + candidate = str; + ch = toupper(ch); + + for (;;) + { + /* strcasechr() will return a pointer to the next occurrence of the + * character ch in the string (ignoring case) + */ + + candidate = strcasechr(candidate, ch); + if (!candidate || strlen(candidate) < len) + { + /* First character of the substring does not appear in the string + * or the remainder of the string is not long enough to contain the + * substring. + */ + + return NULL; + } + + /* Check if this is the beginning of a matching substring (ignoring case) */ + + if (strncasecmp(candidate, substr, len) == 0) + { + /* Yes.. return the pointer to the first occurrence of the matching + * substring. + */ + + return (char*)candidate; + } + + /* No, find the next candidate after this one */ + + candidate++; + } + + /* Won't get here, but some compilers might complain. Others might complain + * about this code being unreachable too. + */ + + return NULL; +} + diff --git a/nuttx/libc/string/lib_strstr.c b/nuttx/libc/string/lib_strstr.c new file mode 100644 index 000000000..7a60a680d --- /dev/null +++ b/nuttx/libc/string/lib_strstr.c @@ -0,0 +1,106 @@ +/**************************************************************************** + * libc/string/lib_strstr.c + * + * Copyright (C) 2009, 2011 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <gnutt@nuttx.org> + * + * Redistribution and use str 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 str binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer str + * 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 <nuttx/config.h> + +#include <string.h> + +/**************************************************************************** + * Global Functions + ****************************************************************************/ + +char *strstr(const char *str, const char *substr) +{ + const char *candidate; /* Candidate in str with matching start character */ + char ch; /* First character of the substring */ + int len; /* The length of the substring */ + + /* Special case the empty substring */ + + len = strlen(substr); + ch = *substr; + + if (!ch) + { + /* We'll say that an empty substring matches at the beginning of + * the string + */ + + return (char*)str; + } + + /* Search for the substring */ + + candidate = str; + for (;;) + { + /* strchr() will return a pointer to the next occurrence of the + * character ch in the string + */ + + candidate = strchr(candidate, ch); + if (!candidate || strlen(candidate) < len) + { + /* First character of the substring does not appear in the string + * or the remainder of the string is not long enough to contain the + * substring. + */ + + return NULL; + } + + /* Check if this is the beginning of a matching substring */ + + if (strncmp(candidate, substr, len) == 0) + { + return (char*)candidate; + } + + /* No, find the next candidate after this one */ + + candidate++; + } + + /* Won't get here, but some compilers might complain. Other compilers + * might complain about this code being unreachable too. + */ + + return NULL; +} + |