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:
+49
-26
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user