Consolidate some duplicate functions

Signed-off-by: Jeroen Koekkoek <jeroen@koekkoek.nl>
This commit is contained in:
Jeroen Koekkoek 2018-09-19 18:40:19 +02:00
parent 2cee550f6f
commit 2fc4cac1a7
6 changed files with 132 additions and 168 deletions

View file

@ -21,8 +21,11 @@
#include <string.h>
#include <ctype.h>
#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,

View file

@ -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 <stddef.h>
char *
os_rindex(
const char *s,
int c)
{
char *last = NULL;
while (*s) {
if (*s == c) {
last = (char *)s;
}
s++;
}
return last;
}

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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,