From 2fc4cac1a7f70438b21905a4bd4be86f4d9bfe45 Mon Sep 17 00:00:00 2001 From: Jeroen Koekkoek Date: Wed, 19 Sep 2018 18:40:19 +0200 Subject: [PATCH] Consolidate some duplicate functions Signed-off-by: Jeroen Koekkoek --- src/os/src/snippets/code/os_stdlib.c | 79 +-------------- src/os/src/snippets/code/os_stdlib_rindex.c | 28 ++++++ .../src/snippets/code/os_stdlib_strcasecmp.c | 32 ++++++ src/os/src/snippets/code/os_stdlib_strdup.c | 28 ++++++ .../src/snippets/code/os_stdlib_strncasecmp.c | 35 +++++++ src/os/src/windows/os_platform_stdlib.c | 98 +------------------ 6 files changed, 132 insertions(+), 168 deletions(-) create mode 100644 src/os/src/snippets/code/os_stdlib_rindex.c create mode 100644 src/os/src/snippets/code/os_stdlib_strcasecmp.c create mode 100644 src/os/src/snippets/code/os_stdlib_strdup.c create mode 100644 src/os/src/snippets/code/os_stdlib_strncasecmp.c diff --git a/src/os/src/snippets/code/os_stdlib.c b/src/os/src/snippets/code/os_stdlib.c index f8a89e2..a79b40e 100644 --- a/src/os/src/snippets/code/os_stdlib.c +++ b/src/os/src/snippets/code/os_stdlib.c @@ -21,8 +21,11 @@ #include #include - #include "os_stdlib_strsep.c" +#include "os_stdlib_rindex.c" +#include "os_stdlib_strcasecmp.c" +#include "os_stdlib_strncasecmp.c" +#include "os_stdlib_strdup.c" _Ret_opt_z_ const char * os_getenv( @@ -79,38 +82,6 @@ os_access( return result; } -char * -os_rindex( - const char *s, - int c) -{ - char *last = NULL; - - while (*s) { - if (*s == c) { - last = (char *)s; - } - s++; - } - return last; -} - -_Ret_z_ -_Check_return_ -char * -os_strdup( - _In_z_ const char *s1) -{ - size_t len; - char *dup; - - len = strlen(s1) + 1; - dup = os_malloc(len); - memcpy(dup, s1, len); - - return dup; -} - int os_vsnprintf( char *str, @@ -157,48 +128,6 @@ os_vfprintfnosigpipe( return result; } -int -os_strcasecmp( - const char *s1, - const char *s2) -{ - int cr; - - while (*s1 && *s2) { - cr = tolower(*s1) - tolower(*s2); - if (cr) { - return cr; - } - s1++; - s2++; - } - cr = tolower(*s1) - tolower(*s2); - return cr; -} - -int -os_strncasecmp( - const char *s1, - const char *s2, - size_t n) -{ - int cr = 0; - - while (*s1 && *s2 && n) { - cr = tolower(*s1) - tolower(*s2); - if (cr) { - return cr; - } - s1++; - s2++; - n--; - } - if (n) { - cr = tolower(*s1) - tolower(*s2); - } - return cr; -} - os_result os_stat( const char *path, diff --git a/src/os/src/snippets/code/os_stdlib_rindex.c b/src/os/src/snippets/code/os_stdlib_rindex.c new file mode 100644 index 0000000..a1a8f1a --- /dev/null +++ b/src/os/src/snippets/code/os_stdlib_rindex.c @@ -0,0 +1,28 @@ +/* + * Copyright(c) 2006 to 2018 ADLINK Technology Limited and others + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License + * v. 1.0 which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause + */ +#include + +char * +os_rindex( + const char *s, + int c) +{ + char *last = NULL; + + while (*s) { + if (*s == c) { + last = (char *)s; + } + s++; + } + return last; +} diff --git a/src/os/src/snippets/code/os_stdlib_strcasecmp.c b/src/os/src/snippets/code/os_stdlib_strcasecmp.c new file mode 100644 index 0000000..ae1e250 --- /dev/null +++ b/src/os/src/snippets/code/os_stdlib_strcasecmp.c @@ -0,0 +1,32 @@ +/* + * Copyright(c) 2006 to 2018 ADLINK Technology Limited and others + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License + * v. 1.0 which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause + */ +#include "os/os.h" + +#pragma warning( default : 4996 ) +int +os_strcasecmp( + const char *s1, + const char *s2) +{ + int cr; + + while (*s1 && *s2) { + cr = tolower(*s1) - tolower(*s2); + if (cr) { + return cr; + } + s1++; + s2++; + } + cr = tolower(*s1) - tolower(*s2); + return cr; +} diff --git a/src/os/src/snippets/code/os_stdlib_strdup.c b/src/os/src/snippets/code/os_stdlib_strdup.c new file mode 100644 index 0000000..3acc6d8 --- /dev/null +++ b/src/os/src/snippets/code/os_stdlib_strdup.c @@ -0,0 +1,28 @@ +/* + * Copyright(c) 2006 to 2018 ADLINK Technology Limited and others + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License + * v. 1.0 which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause + */ +#include "os/os.h" + +_Ret_z_ +_Check_return_ +char * +os_strdup( + _In_z_ const char *s1) +{ + size_t len; + char *dup; + + len = strlen(s1) + 1; + dup = os_malloc(len); + memcpy(dup, s1, len); + + return dup; +} diff --git a/src/os/src/snippets/code/os_stdlib_strncasecmp.c b/src/os/src/snippets/code/os_stdlib_strncasecmp.c new file mode 100644 index 0000000..753042d --- /dev/null +++ b/src/os/src/snippets/code/os_stdlib_strncasecmp.c @@ -0,0 +1,35 @@ +/* + * Copyright(c) 2006 to 2018 ADLINK Technology Limited and others + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License + * v. 1.0 which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause + */ +#include "os/os.h" + +int +os_strncasecmp( + const char *s1, + const char *s2, + size_t n) +{ + int cr = 0; + + while (*s1 && *s2 && n) { + cr = tolower(*s1) - tolower(*s2); + if (cr) { + return cr; + } + s1++; + s2++; + n--; + } + if (n) { + cr = tolower(*s1) - tolower(*s2); + } + return cr; +} diff --git a/src/os/src/windows/os_platform_stdlib.c b/src/os/src/windows/os_platform_stdlib.c index 6b05cab..27f2acb 100644 --- a/src/os/src/windows/os_platform_stdlib.c +++ b/src/os/src/windows/os_platform_stdlib.c @@ -18,7 +18,11 @@ #include "../snippets/code/os_stdlib_strtod.c" #include "../snippets/code/os_stdlib_strtol.c" #include "../snippets/code/os_stdlib_strtok_r.c" - +#include "../snippets/code/os_stdlib_strsep.c" +#include "../snippets/code/os_stdlib_strdup.c" +#include "../snippets/code/os_stdlib_rindex.c" +#include "../snippets/code/os_stdlib_strcasecmp.c" +#include "../snippets/code/os_stdlib_strncasecmp.c" static int32_t os__ensurePathExists( @@ -140,55 +144,6 @@ os_access( return result; } -char * -os_rindex( - const char *s, - int c) -{ - char *last = NULL; - - while (*s) { - if (*s == c) { - last = (char *)s; - } - s++; - } - return last; -} - -_Ret_z_ -_Check_return_ -char * -os_strdup( - _In_z_ const char *s1) -{ - size_t len; - char *dup; - - len = strlen(s1) + 1; - dup = os_malloc(len); - memcpy(dup, s1, len); - - return dup; -} - -char * -os_strsep(char **str, const char *sep) -{ - char *ret; - if (**str == '\0') - return 0; - ret = *str; - while (**str && strchr(sep, **str) == 0) - (*str)++; - if (**str != '\0') - { - **str = '\0'; - (*str)++; - } - return ret; -} - #pragma warning( disable : 4996 ) int os_vfprintfnosigpipe( @@ -199,49 +154,6 @@ os_vfprintfnosigpipe( return vfprintf(file, format, args); } -#pragma warning( default : 4996 ) -int -os_strcasecmp( - const char *s1, - const char *s2) -{ - int cr; - - while (*s1 && *s2) { - cr = tolower(*s1) - tolower(*s2); - if (cr) { - return cr; - } - s1++; - s2++; - } - cr = tolower(*s1) - tolower(*s2); - return cr; -} - -int -os_strncasecmp( - const char *s1, - const char *s2, - size_t n) -{ - int cr = 0; - - while (*s1 && *s2 && n) { - cr = tolower(*s1) - tolower(*s2); - if (cr) { - return cr; - } - s1++; - s2++; - n--; - } - if (n) { - cr = tolower(*s1) - tolower(*s2); - } - return cr; -} - os_result os_stat( const char *path,