Added bacnet_strnlen and bacnet_stricmp to avoid libc compiler problems (#857)

* Added bacnet_strnlen and bacnet_stricmp to avoid libc compiler problems

* FIxed compiler warnings in printf conversions.
This commit is contained in:
Steve Karg
2024-11-21 06:43:37 -06:00
committed by GitHub
parent c71b2e3a66
commit 1375347ab6
7 changed files with 64 additions and 63 deletions
+2 -6
View File
@@ -21,6 +21,7 @@
#include "bacnet/bacdef.h"
/* BACnet Stack API */
#include "bacnet/bactext.h"
#include "bacnet/bacstr.h"
#include "bacnet/bacerror.h"
#include "bacnet/iam.h"
#include "bacnet/arf.h"
@@ -36,11 +37,6 @@
#include "bacnet/datalink/datalink.h"
#include "bacnet/datalink/dlenv.h"
#if defined(__BORLANDC__)
#define _kbhit kbhit
#define _stricmp stricmp
#endif
#define MY_MAX_STR 32
#define MY_MAX_BLOCK 8
@@ -215,7 +211,7 @@ int main(int argc, char *argv[])
return 0;
}
/* decode the command line parameters */
if (_stricmp(argv[1], "server") == 0) {
if (bacnet_stricmp(argv[1], "server") == 0) {
Target_Mode = 1;
} else {
Target_Mode = 0;
+49 -26
View File
@@ -393,31 +393,6 @@ bool characterstring_init(
return status;
}
/**
* @brief Return the length of a string, within a maximum length
* @note The strnlen function is non-standard and not available in
* all libc implementations. This function is a workaround for that.
* @details The strnlen function computes the smaller of the number
* of characters in the array pointed to by s, not including any
* terminating null character, or the value of the maxlen argument.
* The strnlen function examines no more than maxlen bytes of the
* array pointed to by s.
* @param s - string to check
* @param maxlen - maximum length to check
* @return The strnlen function returns the number of bytes that
* precede the first null character in the array pointed to by s,
* if s contains a null character within the first maxlen characters;
* otherwise, it returns maxlen.
*/
size_t characterstring_strnlen(const char *str, size_t maxlen)
{
const char *p = memchr(str, 0, maxlen);
if (p == NULL) {
return maxlen;
}
return (p - str);
}
/**
* Initialize a BACnet character string.
* Returns false if the string exceeds capacity.
@@ -434,7 +409,7 @@ bool characterstring_init_ansi_safe(
{
return characterstring_init(
char_string, CHARACTER_ANSI_X34, value,
value ? characterstring_strnlen(value, tmax) : 0);
value ? bacnet_strnlen(value, tmax) : 0);
}
/**
@@ -1246,3 +1221,51 @@ bool octetstring_value_same(
return false;
}
#endif
/**
* @brief Compare two strings, case insensitive
* @param a - first string
* @param b - second string
* @return 0 if the strings are equal, non-zero if not
* @note The stricmp() function is not included C standard.
*/
int bacnet_stricmp(const char *a, const char *b)
{
int twin_a, twin_b;
do {
twin_a = *(const unsigned char *)a;
twin_b = *(const unsigned char *)b;
twin_a = tolower(toupper(twin_a));
twin_b = tolower(toupper(twin_b));
a++;
b++;
} while ((twin_a == twin_b) && (twin_a != '\0'));
return twin_a - twin_b;
}
/**
* @brief Return the length of a string, within a maximum length
* @note The strnlen function is non-standard and not available in
* all libc implementations. This function is a workaround for that.
* @details The strnlen function computes the smaller of the number
* of characters in the array pointed to by s, not including any
* terminating null character, or the value of the maxlen argument.
* The strnlen function examines no more than maxlen bytes of the
* array pointed to by s.
* @param s - string to check
* @param maxlen - maximum length to check
* @return The strnlen function returns the number of bytes that
* precede the first null character in the array pointed to by s,
* if s contains a null character within the first maxlen characters;
* otherwise, it returns maxlen.
*/
size_t bacnet_strnlen(const char *str, size_t maxlen)
{
const char *p = memchr(str, 0, maxlen);
if (p == NULL) {
return maxlen;
}
return (p - str);
}
+5 -2
View File
@@ -81,8 +81,6 @@ BACNET_STACK_EXPORT
bool characterstring_init_ansi(
BACNET_CHARACTER_STRING *char_string, const char *value);
BACNET_STACK_EXPORT
size_t characterstring_strnlen(const char *str, size_t maxlen);
BACNET_STACK_EXPORT
bool characterstring_init_ansi_safe(
BACNET_CHARACTER_STRING *char_string, const char *value, size_t tmax);
BACNET_STACK_EXPORT
@@ -169,6 +167,11 @@ bool octetstring_value_same(
const BACNET_OCTET_STRING *octet_string1,
const BACNET_OCTET_STRING *octet_string2);
BACNET_STACK_EXPORT
int bacnet_stricmp(const char *a, const char *b);
BACNET_STACK_EXPORT
size_t bacnet_strnlen(const char *str, size_t maxlen);
#ifdef __cplusplus
}
#endif /* __cplusplus */
+5 -2
View File
@@ -37,11 +37,14 @@ static void lighting_command_tracking_value_notify(
}
data->Tracking_Value_Callback(data->Key, old_value, value);
} else {
debug_printf("Lighting-Command[%lu]-Out-of-Service\n", data->Key);
debug_printf(
"Lighting-Command[%lu]-Out-of-Service\n",
(unsigned long)data->Key);
}
} else {
debug_printf(
"Lighting-Command[%lu]-Tracking-Value=%f\n", data->Key, value);
"Lighting-Command[%lu]-Tracking-Value=%f\n",
(unsigned long)data->Key, (double)value);
}
}
+2 -24
View File
@@ -9,31 +9,9 @@
#include <string.h>
#include <ctype.h>
#include "bacnet/bacdef.h"
#include "bacnet/bacstr.h"
#include "bacnet/indtext.h"
/**
* @brief Compare two strings, case insensitive
* @param a - first string
* @param b - second string
* @return 0 if the strings are equal, non-zero if not
* @note The stricmp() function is not included C standard.
*/
int indtext_stricmp(const char *a, const char *b)
{
int twin_a, twin_b;
do {
twin_a = *(const unsigned char *)a;
twin_b = *(const unsigned char *)b;
twin_a = tolower(toupper(twin_a));
twin_b = tolower(toupper(twin_b));
a++;
b++;
} while ((twin_a == twin_b) && (twin_a != '\0'));
return twin_a - twin_b;
}
/**
* @brief Search a list of strings to find a matching string
* @param data_list - list of strings and indices
@@ -80,7 +58,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;
-3
View File
@@ -24,9 +24,6 @@ typedef const struct {
extern "C" {
#endif /* __cplusplus */
BACNET_STACK_EXPORT
int indtext_stricmp(const char *a, const char *b);
/* Searches for a matching string and returns the index to the string
in the parameter found_index.
If the string is not found, false is returned
+1
View File
@@ -34,6 +34,7 @@ add_executable(${PROJECT_NAME}
# File(s) under test
${SRC_DIR}/bacnet/indtext.c
# Support files and stubs (pathname alphabetical)
${SRC_DIR}/bacnet/bacstr.c
# Test and test library files
./src/main.c
${ZTST_DIR}/ztest_mock.c