ucx/printf.c

branch
newapi
changeset 324
ce13a778654a
parent 253
087cc9216f28
--- a/ucx/printf.c	Thu Oct 03 18:54:19 2024 +0200
+++ b/ucx/printf.c	Sun Oct 06 12:00:31 2024 +0200
@@ -39,7 +39,7 @@
 int cx_fprintf(
         void *stream,
         cx_write_func wfc,
-        char const *fmt,
+        const char *fmt,
         ...
 ) {
     int ret;
@@ -53,7 +53,7 @@
 int cx_vfprintf(
         void *stream,
         cx_write_func wfc,
-        char const *fmt,
+        const char *fmt,
         va_list ap
 ) {
     char buf[CX_PRINTF_SBO_SIZE];
@@ -85,8 +85,8 @@
 }
 
 cxmutstr cx_asprintf_a(
-        CxAllocator const *allocator,
-        char const *fmt,
+        const CxAllocator *allocator,
+        const char *fmt,
         ...
 ) {
     va_list ap;
@@ -97,8 +97,8 @@
 }
 
 cxmutstr cx_vasprintf_a(
-        CxAllocator const *a,
-        char const *fmt,
+        const CxAllocator *a,
+        const char *fmt,
         va_list ap
 ) {
     cxmutstr s;
@@ -132,7 +132,7 @@
     return s;
 }
 
-int cx_sprintf_a(CxAllocator *alloc, char **str, size_t len, const char *fmt, ... ) {
+int cx_sprintf_a(CxAllocator *alloc, char **str, size_t *len, const char *fmt, ... ) {
     va_list ap;
     va_start(ap, fmt);
     int ret = cx_vsprintf_a(alloc, str, len, fmt, ap);
@@ -140,11 +140,11 @@
     return ret;
 }
 
-int cx_vsprintf_a(CxAllocator *alloc, char **str, size_t len, const char *fmt, va_list ap) {
+int cx_vsprintf_a(CxAllocator *alloc, char **str, size_t *len, const char *fmt, va_list ap) {
     va_list ap2;
     va_copy(ap2, ap);
-    int ret = vsnprintf(*str, len, fmt, ap);
-    if ((unsigned) ret >= len) {
+    int ret = vsnprintf(*str, *len, fmt, ap);
+    if ((unsigned) ret >= *len) {
         unsigned newlen = ret + 1;
         char *ptr = cxRealloc(alloc, *str, newlen);
         if (ptr) {
@@ -152,6 +152,7 @@
             if (newret < 0) {
                 cxFree(alloc, ptr);
             } else {
+                *len = newlen;
                 *str = ptr;
                 ret = newret;
             }
@@ -161,7 +162,7 @@
     return ret;
 }
 
-int cx_sprintf_sa(CxAllocator *alloc, char *buf, size_t len, char **str, const char *fmt, ... ) {
+int cx_sprintf_sa(CxAllocator *alloc, char *buf, size_t *len, char **str, const char *fmt, ... ) {
     va_list ap;
     va_start(ap, fmt);
     int ret = cx_vsprintf_sa(alloc, buf, len, str, fmt, ap);
@@ -169,12 +170,12 @@
     return ret;
 }
 
-int cx_vsprintf_sa(CxAllocator *alloc, char *buf, size_t len, char **str, const char *fmt, va_list ap) {
+int cx_vsprintf_sa(CxAllocator *alloc, char *buf, size_t *len, char **str, const char *fmt, va_list ap) {
     va_list ap2;
     va_copy(ap2, ap);
-    int ret = vsnprintf(buf, len, fmt, ap);
+    int ret = vsnprintf(buf, *len, fmt, ap);
     *str = buf;
-    if ((unsigned) ret >= len) {
+    if ((unsigned) ret >= *len) {
         unsigned newlen = ret + 1;
         char *ptr = cxMalloc(alloc, newlen);
         if (ptr) {
@@ -182,6 +183,7 @@
             if (newret < 0) {
                 cxFree(alloc, ptr);
             } else {
+                *len = newlen;
                 *str = ptr;
                 ret = newret;
             }

mercurial