neels has uploaded this change for review. (
https://gerrit.osmocom.org/c/libosmocore/+/35206?usp=email )
Change subject: util: add OSMO_STRBUF_REMAIN()
......................................................................
util: add OSMO_STRBUF_REMAIN()
This code already exists twice, and upcoming patch will need this as
well in logging.c. Add a macro to remove the code dup.
Related: OS#6284
Related: Ib577a5e0d7450ce93ff21f37ba3262704cbf4752
Change-Id: I6f2991125882bff948708bbb4ae218f9f3d1e50c
---
M include/osmocom/core/utils.h
1 file changed, 19 insertions(+), 2 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/06/35206/1
diff --git a/include/osmocom/core/utils.h b/include/osmocom/core/utils.h
index ee7cfa4..2a3670b 100644
--- a/include/osmocom/core/utils.h
+++ b/include/osmocom/core/utils.h
@@ -249,7 +249,7 @@
#define OSMO_STRBUF_APPEND(STRBUF, func, args...) do { \
if (!(STRBUF).pos) \
(STRBUF).pos = (STRBUF).buf; \
- size_t _sb_remain = (STRBUF).buf ? (STRBUF).len - ((STRBUF).pos - (STRBUF).buf) : 0; \
+ size_t _sb_remain = OSMO_STRBUF_REMAIN(STRBUF); \
int _sb_l = func((STRBUF).pos, _sb_remain, ##args); \
if (_sb_l < 0 || (size_t)_sb_l > _sb_remain) \
(STRBUF).pos = (STRBUF).buf + (STRBUF).len; \
@@ -281,6 +281,9 @@
#define OSMO_STRBUF_PRINTF(STRBUF, fmt, args...) \
OSMO_STRBUF_APPEND(STRBUF, snprintf, fmt, ##args)
+/*! Return remaining space for characters and terminating nul in the given struct
osmo_strbuf. */
+#define OSMO_STRBUF_REMAIN(STRBUF) ((STRBUF).buf ? (STRBUF).len - ((STRBUF).pos -
(STRBUF).buf) : 0)
+
/*! Like OSMO_STRBUF_APPEND(), but for function signatures that return the char* buffer
instead of a length.
* When using this function, the final STRBUF.chars_needed may not reflect the actual
number of characters needed, since
* that number cannot be obtained from this kind of function signature.
@@ -292,7 +295,7 @@
#define OSMO_STRBUF_APPEND_NOLEN(STRBUF, func, args...) do { \
if (!(STRBUF).pos) \
(STRBUF).pos = (STRBUF).buf; \
- size_t _sb_remain = (STRBUF).buf ? (STRBUF).len - ((STRBUF).pos - (STRBUF).buf) : 0; \
+ size_t _sb_remain = OSMO_STRBUF_REMAIN(STRBUF); \
if (_sb_remain) { \
func((STRBUF).pos, _sb_remain, ##args); \
} \
--
To view, visit
https://gerrit.osmocom.org/c/libosmocore/+/35206?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gerrit.osmocom.org/settings
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I6f2991125882bff948708bbb4ae218f9f3d1e50c
Gerrit-Change-Number: 35206
Gerrit-PatchSet: 1
Gerrit-Owner: neels <nhofmeyr(a)sysmocom.de>
Gerrit-MessageType: newchange