From a7b48cd1cf41c57b10e744c1c4d28495314eccc3 Mon Sep 17 00:00:00 2001 From: matt335672 <30179339+matt335672@users.noreply.github.com> Date: Mon, 25 Oct 2021 16:13:09 +0100 Subject: [PATCH] Added more string calls + fix prototypes - Added g_strrchr() and g_strstr() - Made C prototypes for g_strchr() and g_strnchr() compatible with expected C library functions --- common/string_calls.c | 37 +++++++++++++++++++++++++++++++++---- common/string_calls.h | 6 ++++-- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/common/string_calls.c b/common/string_calls.c index d1d7500a..9950fd09 100644 --- a/common/string_calls.c +++ b/common/string_calls.c @@ -188,7 +188,7 @@ g_strlen(const char *text) /*****************************************************************************/ /* locates char in text */ -const char * +char * g_strchr(const char *text, int c) { if (text == NULL) @@ -196,12 +196,27 @@ g_strchr(const char *text, int c) return 0; } - return strchr(text, c); + /* Cast needed to compile with C++ */ + return (char *)strchr(text, c); +} + +/*****************************************************************************/ +/* locates char in text */ +char * +g_strrchr(const char *text, int c) +{ + if (text == NULL) + { + return 0; + } + + /* Cast needed to compile with C++ */ + return (char *)strrchr(text, c); } /*****************************************************************************/ /* locates char in text with length */ -const char * +char * g_strnchr(const char *text, int c, int len) { if (text == NULL || len <= 0) @@ -209,7 +224,7 @@ g_strnchr(const char *text, int c, int len) return NULL; } - return (const char *)memchr(text, c, len); + return (char *)memchr(text, c, len); } /*****************************************************************************/ @@ -680,6 +695,20 @@ g_pos(const char *str, const char *to_find) return (pp - str); } +/*****************************************************************************/ + +char * +g_strstr(const char *haystack, const char *needle) +{ + if (haystack == NULL || needle == NULL) + { + return NULL; + } + + /* Cast needed to compile with C++ */ + return (char *)strstr(haystack, needle); +} + /*****************************************************************************/ int g_mbstowcs(twchar *dest, const char *src, int n) diff --git a/common/string_calls.h b/common/string_calls.h index 8682febe..990a98e6 100644 --- a/common/string_calls.h +++ b/common/string_calls.h @@ -175,8 +175,9 @@ g_bitmask_to_str(int bitmask, const struct bitmask_string[], char delim, char *buff, int bufflen); int g_strlen(const char *text); -const char *g_strchr(const char *text, int c); -const char *g_strnchr(const char *text, int c, int len); +char *g_strchr(const char *text, int c); +char *g_strrchr(const char *text, int c); +char *g_strnchr(const char *text, int c, int len); char *g_strcpy(char *dest, const char *src); char *g_strncpy(char *dest, const char *src, int len); char *g_strcat(char *dest, const char *src); @@ -202,6 +203,7 @@ int g_htoi(char *str); int g_bytes_to_hexstr(const void *bytes, int num_bytes, char *out_str, int bytes_out_str); int g_pos(const char *str, const char *to_find); +char *g_strstr(const char *haystack, const char *needle); int g_mbstowcs(twchar *dest, const char *src, int n); int g_wcstombs(char *dest, const twchar *src, int n); int g_strtrim(char *str, int trim_flags);