Create bacnet strnlen and stricmp to avoid libc compiler problems

This commit is contained in:
Steve Karg
2024-07-22 16:22:36 -05:00
parent 3864421ac8
commit 766e0c020d
4 changed files with 49 additions and 38 deletions
+42 -1
View File
@@ -408,7 +408,7 @@ bool characterstring_init_ansi_safe(
BACNET_CHARACTER_STRING *char_string, const char *value, size_t tmax)
{
return characterstring_init(char_string, CHARACTER_ANSI_X34, value,
value ? strnlen(value, tmax) : 0);
value ? bacnet_strnlen(value, tmax) : 0);
}
/**
@@ -1191,3 +1191,44 @@ bool octetstring_value_same(
return false;
}
#endif
/**
* @brief Compare two strings ignoring case
* @param s1 - first string
* @param s2 - second string
* @return 0 if the strings are equal, otherwise non-zero
*/
int bacnet_stricmp(const char *s1, const char *s2)
{
unsigned char c1, c2;
do {
c1 = (unsigned char)*s1;
c2 = (unsigned char)*s2;
c1 = (unsigned char)tolower(c1);
c2 = (unsigned char)tolower(c2);
s1++;
s2++;
} while ((c1 == c2) && (c1 != '\0'));
return (int)c1 - c2;
}
/**
* @brief non-standard strnlen function
* @param s - string to check
* @param maxlen - maximum length to check
* @return length of string, up to maxlen
*/
size_t bacnet_strnlen(const char *s, size_t maxlen)
{
size_t len;
for (len = 0; len < maxlen; len++, s++) {
if (!*s) {
break;
}
}
return len;
}
+5
View File
@@ -215,6 +215,11 @@ extern "C" {
BACNET_OCTET_STRING * octet_string1,
BACNET_OCTET_STRING * octet_string2);
BACNET_STACK_EXPORT
int bacnet_stricmp(const char *s1, const char *s2);
BACNET_STACK_EXPORT
size_t bacnet_strnlen(const char *s, size_t maxlen);
#ifdef __cplusplus
}
#endif /* __cplusplus */
-11
View File
@@ -77,17 +77,6 @@ __inline int c99_snprintf(char *outBuf, size_t size, const char *format, ...)
return count;
}
#endif
#elif defined(__ZEPHYR__)
#include <strings.h>
/* For some reason my Zephyr build for non-native targets does not
* see a definition for strnlen(), but it is visible in when
* compiling for native_posix. This results in the compiler
* emitting a warning, forcing Zephyr's sanitycheck() script to stop.
* Until this is chased down, the definition is being provided here.
*/
#if !CONFIG_NATIVE_APPLICATION
size_t strnlen(const char *, size_t);
#endif
#endif
/* some common min/max as defined in windef.h */
+2 -26
View File
@@ -7,33 +7,9 @@
*/
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include "bacnet/bacstr.h"
#include "bacnet/indtext.h"
/** @file indtext.c Maps text strings and indices of type INDTEXT_DATA */
/**
* @brief Compare two strings ignoring case
* @param s1 - first string
* @param s2 - second string
* @return 0 if the strings are equal, otherwise non-zero
*/
static int indtext_stricmp(const char *s1, const char *s2)
{
unsigned char c1, c2;
do {
c1 = (unsigned char)*s1;
c2 = (unsigned char)*s2;
c1 = (unsigned char)tolower(c1);
c2 = (unsigned char)tolower(c2);
s1++;
s2++;
} while ((c1 == c2) && (c1 != '\0'));
return (int)c1 - c2;
}
/**
* @brief Search a list of strings to find a matching string
* @param data_list - list of strings and indices
@@ -80,7 +56,7 @@ bool indtext_by_istring(
if (data_list && search_name) {
while (data_list->pString) {
if (indtext_stricmp(data_list->pString, search_name) == 0) {
if (bacnet_stricmp(data_list->pString, search_name) == 0) {
index = data_list->index;
found = true;
break;