No need to loop to increase buffer size when snprintf() tells me exactly how much space I need.
modified: src/nms.c
This commit is contained in:
parent
9b5471838a
commit
4ccde16721
11
src/nms.c
11
src/nms.c
@ -52,15 +52,14 @@ char getMaskChar(void);
|
|||||||
* const char *format - printf-style format string
|
* const char *format - printf-style format string
|
||||||
*/
|
*/
|
||||||
void nmsprintf(const char *format, ...) {
|
void nmsprintf(const char *format, ...) {
|
||||||
int bufferSize = PRINT_BUFFER;
|
char *nmsprintBuffer = malloc(PRINT_BUFFER);
|
||||||
int bufferIncrementSize = PRINT_BUFFER;
|
int fmtSize;
|
||||||
char *nmsprintBuffer = malloc(bufferSize);
|
|
||||||
|
|
||||||
va_list argp;
|
va_list argp;
|
||||||
va_start(argp, format);
|
va_start(argp, format);
|
||||||
while (vsnprintf(nmsprintBuffer, bufferSize, format, argp) >= bufferSize) {
|
if ((fmtSize = vsnprintf(nmsprintBuffer, PRINT_BUFFER, format, argp)) >= PRINT_BUFFER) {
|
||||||
bufferSize += bufferIncrementSize;
|
nmsprintBuffer = realloc(nmsprintBuffer, fmtSize + 1);
|
||||||
nmsprintBuffer = realloc(nmsprintBuffer, bufferSize);
|
vsnprintf(nmsprintBuffer, fmtSize + 1, format, argp);
|
||||||
}
|
}
|
||||||
va_end(argp);
|
va_end(argp);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user