mirror of
https://github.com/Genymobile/scrcpy.git
synced 2025-04-20 03:25:03 +00:00
Add asprintf() compat function
Add sc_asprintf() similar to asprintf() which is not available everywhere.
This commit is contained in:
parent
74ece9b45b
commit
a0d98fe4ae
2 changed files with 43 additions and 0 deletions
|
@ -1,7 +1,9 @@
|
|||
#include "str_util.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
|
@ -195,3 +197,33 @@ utf8_from_wide_char(const wchar_t *ws) {
|
|||
}
|
||||
|
||||
#endif
|
||||
|
||||
char *
|
||||
sc_asprintf(const char *fmt, ...) {
|
||||
va_list va;
|
||||
va_start(va, fmt);
|
||||
char *s = sc_vasprintf(fmt, va);
|
||||
va_end(va);
|
||||
return s;
|
||||
}
|
||||
|
||||
char *
|
||||
sc_vasprintf(const char *fmt, va_list ap) {
|
||||
va_list va;
|
||||
va_copy(va, ap);
|
||||
int len = vsnprintf(NULL, 0, fmt, va);
|
||||
va_end(va);
|
||||
|
||||
char *str = malloc(len + 1);
|
||||
if (!str) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
va_copy(va, ap);
|
||||
int len2 = vsprintf(str, fmt, va);
|
||||
(void) len2;
|
||||
assert(len == len2);
|
||||
va_end(va);
|
||||
|
||||
return str;
|
||||
}
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
#ifndef STRUTIL_H
|
||||
#define STRUTIL_H
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "config.h"
|
||||
|
||||
|
@ -57,4 +59,13 @@ char *
|
|||
utf8_from_wide_char(const wchar_t *s);
|
||||
#endif
|
||||
|
||||
// compatibility function similar to asprintf()
|
||||
// (but returning the resulting string for convenience)
|
||||
char *
|
||||
sc_asprintf(const char *fmt, ...)
|
||||
__attribute__((format(printf, 1, 2)));
|
||||
|
||||
char *
|
||||
sc_vasprintf(const char *fmt, va_list ap);
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Reference in a new issue