Refactor/snprintf next common shift function (#656)
This commit is contained in:
+33
-28
@@ -35,7 +35,10 @@
|
||||
#include <stdint.h> /* for standard integer types uint8_t etc. */
|
||||
#include <stdbool.h> /* for the standard bool type. */
|
||||
#include <stdio.h>
|
||||
#include "bacnet/bacenum.h"
|
||||
/* BACnet Stack defines - first */
|
||||
#include "bacnet/bacdef.h"
|
||||
/* BACnet Stack API */
|
||||
#include "bacnet/bacapp.h"
|
||||
#include "bacnet/bacdcode.h"
|
||||
#include "bacnet/bacint.h"
|
||||
#include "bacnet/datalink/bvlc6.h"
|
||||
@@ -641,6 +644,32 @@ bool bvlc6_address_get(BACNET_IP6_ADDRESS *addr,
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Shift the buffer pointer and decrease the size after an snprintf
|
||||
* @param len - number of bytes (excluding terminating NULL byte) from snprintf
|
||||
* @param buf - pointer to the buffer pointer
|
||||
* @param buf_size - pointer to the buffer size
|
||||
* @return number of bytes (excluding terminating NULL byte) from snprintf
|
||||
*/
|
||||
static int snprintf_shift(int len, char **buf, size_t *buf_size)
|
||||
{
|
||||
if (buf) {
|
||||
if (*buf) {
|
||||
*buf += len;
|
||||
}
|
||||
}
|
||||
if (buf_size) {
|
||||
if ((*buf_size) >= len) {
|
||||
*buf_size -= len;
|
||||
} else {
|
||||
*buf_size = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
/** Convert IPv6 Address from ASCII
|
||||
*
|
||||
* IPv6 addresses are represented as eight groups, separated by colons,
|
||||
@@ -689,41 +718,17 @@ int bvlc6_address_to_ascii(BACNET_IP6_ADDRESS *addr, char *buf, size_t buf_size)
|
||||
if ((a == 0) && (f >= 0)) {
|
||||
if (f++ == 0) {
|
||||
len = snprintf(buf, buf_size, "::");
|
||||
if (buf) {
|
||||
buf += len;
|
||||
}
|
||||
if (len > buf_size) {
|
||||
buf_size = 0;
|
||||
} else {
|
||||
buf_size -= len;
|
||||
}
|
||||
n += len;
|
||||
n += snprintf_shift(len, &buf, &buf_size);
|
||||
}
|
||||
} else {
|
||||
if (f > 0) {
|
||||
f = -1;
|
||||
} else if (i > 0) {
|
||||
len = snprintf(buf, buf_size, ":");
|
||||
if (buf) {
|
||||
buf += len;
|
||||
}
|
||||
if (len > buf_size) {
|
||||
buf_size = 0;
|
||||
} else {
|
||||
buf_size -= len;
|
||||
}
|
||||
n += len;
|
||||
n += snprintf_shift(len, &buf, &buf_size);
|
||||
}
|
||||
len = snprintf(buf, buf_size, "%x", a);
|
||||
if (buf) {
|
||||
buf += len;
|
||||
}
|
||||
if (len > buf_size) {
|
||||
buf_size = 0;
|
||||
} else {
|
||||
buf_size -= len;
|
||||
}
|
||||
n += len;
|
||||
n += snprintf_shift(len, &buf, &buf_size);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user