Browse Source

C: Add sdprintf

master
Markus Koch 2 years ago
parent
commit
25d2b25335
  1. 4
      README.MD
  2. 23
      c/sdprintf.c

4
README.MD

@ -2,5 +2,5 @@
Small code snippets that might prove useful again in the future.
## C
* report: Easy to use report statement with log levels
* report/: Easy to use report statement with log levels
* sdprintf.c: sprintf to a dynamic buffer

23
c/sdprintf.c

@ -0,0 +1,23 @@
/*!
* \brief sdprintf printf to a dynamic buffer
* \param buf The character buffer. Will append if not NULL;
* \param offset Should be set to strlen when continuing a string. Will be incremented automatically.
* \param __format printf format string
* \return the number of bytes written
*/
int sdprintf(char **buf, int *offset, const char *__restrict __format, ...)
{
va_list vargs;
int len;
va_start(vargs, __format);
len = vsnprintf(NULL, 0, __format, vargs);
va_end(vargs);
*buf = realloc(*buf, *offset + len + 1);
va_start(vargs, __format);
len = vsprintf(*buf + *offset, __format, vargs);
va_end(vargs);
*offset += len;
return len;
}
Loading…
Cancel
Save