2015-07-01 08:02:44 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 Andrew Kelley
|
|
|
|
*
|
|
|
|
* This file is part of libsoundio, which is MIT licensed.
|
|
|
|
* See http://opensource.org/licenses/MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2015-11-01 22:18:37 +00:00
|
|
|
#include "util.h"
|
2015-07-01 08:02:44 +00:00
|
|
|
|
2015-07-01 20:24:47 +00:00
|
|
|
void soundio_panic(const char *format, ...) {
|
2015-07-01 08:02:44 +00:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
vfprintf(stderr, format, ap);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
va_end(ap);
|
|
|
|
abort();
|
|
|
|
}
|
2015-07-07 09:55:32 +00:00
|
|
|
|
|
|
|
char *soundio_alloc_sprintf(int *len, const char *format, ...) {
|
|
|
|
va_list ap, ap2;
|
|
|
|
va_start(ap, format);
|
|
|
|
va_copy(ap2, ap);
|
|
|
|
|
2015-11-01 22:18:37 +00:00
|
|
|
int len1 = vsnprintf(NULL, 0, format, ap);
|
2015-07-07 09:55:32 +00:00
|
|
|
assert(len1 >= 0);
|
|
|
|
|
|
|
|
size_t required_size = len1 + 1;
|
2015-11-01 22:18:37 +00:00
|
|
|
char *mem = ALLOCATE(char, required_size);
|
2015-07-07 09:55:32 +00:00
|
|
|
if (!mem)
|
2015-11-01 22:18:37 +00:00
|
|
|
return NULL;
|
2015-07-07 09:55:32 +00:00
|
|
|
|
|
|
|
int len2 = vsnprintf(mem, required_size, format, ap2);
|
|
|
|
assert(len2 == len1);
|
|
|
|
|
|
|
|
va_end(ap2);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if (len)
|
|
|
|
*len = len1;
|
|
|
|
return mem;
|
|
|
|
}
|