mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-08 16:11:18 +00:00
Move mg_a*printf to common
PUBLISHED_FROM=cccd28ef43ec4e08fa52340ce78bda10db5ac27a
This commit is contained in:
1 parent
9dba6c806b
commit
a23daa1547
5 files changed
+73
-99
No files matched your search
+53
-45
@@ -1480,6 +1480,14 @@ void cs_hmac_sha1(const unsigned char *key, size_t keylen,
|
||||
#define C_DISABLE_BUILTIN_SNPRINTF 0
|
||||
#endif
|
||||
|
||||
#ifndef MG_MALLOC
|
||||
#define MG_MALLOC malloc
|
||||
#endif
|
||||
|
||||
#ifndef MG_FREE
|
||||
#define MG_FREE free
|
||||
#endif
|
||||
|
||||
size_t c_strnlen(const char *s, size_t maxlen) {
|
||||
size_t l = 0;
|
||||
for (; l < maxlen && s[l] != '\0'; l++) {
|
||||
@@ -1812,6 +1820,51 @@ int mg_casecmp(const char *s1, const char *s2) {
|
||||
return mg_ncasecmp(s1, s2, (size_t) ~0);
|
||||
}
|
||||
|
||||
int mg_asprintf(char **buf, size_t size, const char *fmt, ...) {
|
||||
int ret;
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
ret = mg_avprintf(buf, size, fmt, ap);
|
||||
va_end(ap);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int mg_avprintf(char **buf, size_t size, const char *fmt, va_list ap) {
|
||||
va_list ap_copy;
|
||||
int len;
|
||||
|
||||
va_copy(ap_copy, ap);
|
||||
len = vsnprintf(*buf, size, fmt, ap_copy);
|
||||
va_end(ap_copy);
|
||||
|
||||
if (len < 0) {
|
||||
/* eCos and Windows are not standard-compliant and return -1 when
|
||||
* the buffer is too small. Keep allocating larger buffers until we
|
||||
* succeed or out of memory. */
|
||||
*buf = NULL; /* LCOV_EXCL_START */
|
||||
while (len < 0) {
|
||||
MG_FREE(*buf);
|
||||
size *= 2;
|
||||
if ((*buf = (char *) MG_MALLOC(size)) == NULL) break;
|
||||
va_copy(ap_copy, ap);
|
||||
len = vsnprintf(*buf, size, fmt, ap_copy);
|
||||
va_end(ap_copy);
|
||||
}
|
||||
/* LCOV_EXCL_STOP */
|
||||
} else if (len >= (int) size) {
|
||||
/* Standard-compliant code path. Allocate a buffer that is large enough. */
|
||||
if ((*buf = (char *) MG_MALLOC(len + 1)) == NULL) {
|
||||
len = -1; /* LCOV_EXCL_LINE */
|
||||
} else { /* LCOV_EXCL_LINE */
|
||||
va_copy(ap_copy, ap);
|
||||
len = vsnprintf(*buf, len + 1, fmt, ap_copy);
|
||||
va_end(ap_copy);
|
||||
}
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
#endif /* EXCLUDE_COMMON */
|
||||
#ifdef MG_MODULE_LINES
|
||||
#line 1 "mongoose/src/tun.h"
|
||||
@@ -9123,51 +9176,6 @@ void mg_hexdump_connection(struct mg_connection *nc, const char *path,
|
||||
}
|
||||
#endif
|
||||
|
||||
int mg_avprintf(char **buf, size_t size, const char *fmt, va_list ap) {
|
||||
va_list ap_copy;
|
||||
int len;
|
||||
|
||||
va_copy(ap_copy, ap);
|
||||
len = vsnprintf(*buf, size, fmt, ap_copy);
|
||||
va_end(ap_copy);
|
||||
|
||||
if (len < 0) {
|
||||
/* eCos and Windows are not standard-compliant and return -1 when
|
||||
* the buffer is too small. Keep allocating larger buffers until we
|
||||
* succeed or out of memory. */
|
||||
*buf = NULL; /* LCOV_EXCL_START */
|
||||
while (len < 0) {
|
||||
MG_FREE(*buf);
|
||||
size *= 2;
|
||||
if ((*buf = (char *) MG_MALLOC(size)) == NULL) break;
|
||||
va_copy(ap_copy, ap);
|
||||
len = vsnprintf(*buf, size, fmt, ap_copy);
|
||||
va_end(ap_copy);
|
||||
}
|
||||
/* LCOV_EXCL_STOP */
|
||||
} else if (len >= (int) size) {
|
||||
/* Standard-compliant code path. Allocate a buffer that is large enough. */
|
||||
if ((*buf = (char *) MG_MALLOC(len + 1)) == NULL) {
|
||||
len = -1; /* LCOV_EXCL_LINE */
|
||||
} else { /* LCOV_EXCL_LINE */
|
||||
va_copy(ap_copy, ap);
|
||||
len = vsnprintf(*buf, len + 1, fmt, ap_copy);
|
||||
va_end(ap_copy);
|
||||
}
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
int mg_asprintf(char **buf, size_t size, const char *fmt, ...) {
|
||||
int ret;
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
ret = mg_avprintf(buf, size, fmt, ap);
|
||||
va_end(ap);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int mg_is_big_endian(void) {
|
||||
static const int n = 1;
|
||||
/* TODO(mkm) use compiletime check with 4-byte char literal */
|
||||
|
||||
Reference in new issue
Block a user