mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-21 11:30:01 +00:00
Split mg_*printf* functions into src/printf.c/h
This commit is contained in:
1 parent
b513d40bd6
commit
a4430dd49d
10 files changed
+262
-235
No files matched your search
@@ -2,65 +2,6 @@
|
||||
#include "iobuf.h"
|
||||
#include "util.h"
|
||||
|
||||
static void mg_pfn_iobuf_private(char ch, void *param, bool expand) {
|
||||
struct mg_iobuf *io = (struct mg_iobuf *) param;
|
||||
if (expand && io->len + 2 > io->size) mg_iobuf_resize(io, io->len + 2);
|
||||
if (io->len + 2 <= io->size) {
|
||||
io->buf[io->len++] = (uint8_t) ch;
|
||||
io->buf[io->len] = 0;
|
||||
} else if (io->len < io->size) {
|
||||
io->buf[io->len++] = 0; // Guarantee to 0-terminate
|
||||
}
|
||||
}
|
||||
|
||||
static void mg_putchar_iobuf_static(char ch, void *param) {
|
||||
mg_pfn_iobuf_private(ch, param, false);
|
||||
}
|
||||
|
||||
void mg_pfn_iobuf(char ch, void *param) {
|
||||
mg_pfn_iobuf_private(ch, param, true);
|
||||
}
|
||||
|
||||
size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list *ap) {
|
||||
struct mg_iobuf io = {(uint8_t *) buf, len, 0, 0};
|
||||
size_t n = mg_vxprintf(mg_putchar_iobuf_static, &io, fmt, ap);
|
||||
if (n < len) buf[n] = '\0';
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t mg_snprintf(char *buf, size_t len, const char *fmt, ...) {
|
||||
va_list ap;
|
||||
size_t n;
|
||||
va_start(ap, fmt);
|
||||
n = mg_vsnprintf(buf, len, fmt, &ap);
|
||||
va_end(ap);
|
||||
return n;
|
||||
}
|
||||
|
||||
char *mg_vmprintf(const char *fmt, va_list *ap) {
|
||||
struct mg_iobuf io = {0, 0, 0, 256};
|
||||
mg_vxprintf(mg_pfn_iobuf, &io, fmt, ap);
|
||||
return (char *) io.buf;
|
||||
}
|
||||
|
||||
char *mg_mprintf(const char *fmt, ...) {
|
||||
char *s;
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
s = mg_vmprintf(fmt, &ap);
|
||||
va_end(ap);
|
||||
return s;
|
||||
}
|
||||
|
||||
size_t mg_xprintf(void (*out)(char, void *), void *ptr, const char *fmt, ...) {
|
||||
size_t len = 0;
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
len = mg_vxprintf(out, ptr, fmt, &ap);
|
||||
va_end(ap);
|
||||
return len;
|
||||
}
|
||||
|
||||
static bool is_digit(int c) {
|
||||
return c >= '0' && c <= '9';
|
||||
}
|
||||
@@ -237,6 +178,15 @@ static size_t bcpy(void (*out)(char, void *), void *ptr, uint8_t *buf,
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t mg_xprintf(void (*out)(char, void *), void *ptr, const char *fmt, ...) {
|
||||
size_t len = 0;
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
len = mg_vxprintf(out, ptr, fmt, &ap);
|
||||
va_end(ap);
|
||||
return len;
|
||||
}
|
||||
|
||||
size_t mg_vxprintf(void (*out)(char, void *), void *param, const char *fmt,
|
||||
va_list *ap) {
|
||||
size_t i = 0, n = 0;
|
||||
|
||||
@@ -3,13 +3,26 @@
|
||||
#include "arch.h"
|
||||
#include "iobuf.h"
|
||||
|
||||
typedef void (*mg_pfn_t)(char, void *); // Custom putchar
|
||||
typedef void (*mg_pfn_t)(char, void *); // Output function
|
||||
typedef size_t (*mg_pm_t)(mg_pfn_t, void *, va_list *); // %M printer
|
||||
void mg_pfn_iobuf(char ch, void *param); // iobuf printer
|
||||
|
||||
// The lowest level
|
||||
size_t mg_vxprintf(void (*)(char, void *), void *, const char *fmt, va_list *);
|
||||
size_t mg_xprintf(void (*fn)(char, void *), void *, const char *fmt, ...);
|
||||
|
||||
// Convenience wrappers around mg_xprintf
|
||||
size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list *ap);
|
||||
size_t mg_snprintf(char *, size_t, const char *fmt, ...);
|
||||
char *mg_vmprintf(const char *fmt, va_list *ap);
|
||||
char *mg_mprintf(const char *fmt, ...);
|
||||
|
||||
// %M print helper functions
|
||||
size_t mg_print_ip(void (*out)(char, void *), void *arg, va_list *ap);
|
||||
size_t mg_print_ip_port(void (*out)(char, void *), void *arg, va_list *ap);
|
||||
size_t mg_print_ip4(void (*out)(char, void *), void *arg, va_list *ap);
|
||||
size_t mg_print_ip6(void (*out)(char, void *), void *arg, va_list *ap);
|
||||
size_t mg_print_mac(void (*out)(char, void *), void *arg, va_list *ap);
|
||||
|
||||
// Various output functions
|
||||
void mg_pfn_iobuf(char ch, void *param); // param: struct mg_iobuf *
|
||||
void mg_pfn_stdout(char c, void *param); // param: ignored
|
||||
@@ -1,15 +1,10 @@
|
||||
#include "log.h"
|
||||
#include "fmt.h"
|
||||
#include "printf.h"
|
||||
#include "str.h"
|
||||
#include "util.h"
|
||||
|
||||
static void default_logger(char c, void *param) {
|
||||
putchar(c);
|
||||
(void) c, (void) param;
|
||||
}
|
||||
|
||||
static int s_level = MG_LL_INFO;
|
||||
static mg_pfn_t s_log_func = default_logger;
|
||||
static mg_pfn_t s_log_func = mg_pfn_stdout;
|
||||
static void *s_log_func_param = NULL;
|
||||
|
||||
void mg_log_set_fn(mg_pfn_t fn, void *param) {
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
#include "fmt.h"
|
||||
#include "iobuf.h"
|
||||
#include "util.h"
|
||||
|
||||
static void mg_pfn_iobuf_private(char ch, void *param, bool expand) {
|
||||
struct mg_iobuf *io = (struct mg_iobuf *) param;
|
||||
if (expand && io->len + 2 > io->size) mg_iobuf_resize(io, io->len + 2);
|
||||
if (io->len + 2 <= io->size) {
|
||||
io->buf[io->len++] = (uint8_t) ch;
|
||||
io->buf[io->len] = 0;
|
||||
} else if (io->len < io->size) {
|
||||
io->buf[io->len++] = 0; // Guarantee to 0-terminate
|
||||
}
|
||||
}
|
||||
|
||||
static void mg_putchar_iobuf_static(char ch, void *param) {
|
||||
mg_pfn_iobuf_private(ch, param, false);
|
||||
}
|
||||
|
||||
void mg_pfn_iobuf(char ch, void *param) {
|
||||
mg_pfn_iobuf_private(ch, param, true);
|
||||
}
|
||||
|
||||
size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list *ap) {
|
||||
struct mg_iobuf io = {(uint8_t *) buf, len, 0, 0};
|
||||
size_t n = mg_vxprintf(mg_putchar_iobuf_static, &io, fmt, ap);
|
||||
if (n < len) buf[n] = '\0';
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t mg_snprintf(char *buf, size_t len, const char *fmt, ...) {
|
||||
va_list ap;
|
||||
size_t n;
|
||||
va_start(ap, fmt);
|
||||
n = mg_vsnprintf(buf, len, fmt, &ap);
|
||||
va_end(ap);
|
||||
return n;
|
||||
}
|
||||
|
||||
char *mg_vmprintf(const char *fmt, va_list *ap) {
|
||||
struct mg_iobuf io = {0, 0, 0, 256};
|
||||
mg_vxprintf(mg_pfn_iobuf, &io, fmt, ap);
|
||||
return (char *) io.buf;
|
||||
}
|
||||
|
||||
char *mg_mprintf(const char *fmt, ...) {
|
||||
char *s;
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
s = mg_vmprintf(fmt, &ap);
|
||||
va_end(ap);
|
||||
return s;
|
||||
}
|
||||
|
||||
void mg_pfn_stdout(char c, void *param) {
|
||||
putchar(c);
|
||||
(void) param;
|
||||
}
|
||||
|
||||
static size_t print_ip4(void (*out)(char, void *), void *arg, uint8_t *p) {
|
||||
return mg_xprintf(out, arg, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
|
||||
}
|
||||
|
||||
static size_t print_ip6(void (*out)(char, void *), void *arg, uint16_t *p) {
|
||||
return mg_xprintf(out, arg, "[%x:%x:%x:%x:%x:%x:%x:%x]", mg_ntohs(p[0]),
|
||||
mg_ntohs(p[1]), mg_ntohs(p[2]), mg_ntohs(p[3]),
|
||||
mg_ntohs(p[4]), mg_ntohs(p[5]), mg_ntohs(p[6]),
|
||||
mg_ntohs(p[7]));
|
||||
}
|
||||
|
||||
size_t mg_print_ip4(void (*out)(char, void *), void *arg, va_list *ap) {
|
||||
uint8_t *p = va_arg(*ap, uint8_t *);
|
||||
return print_ip4(out, arg, p);
|
||||
}
|
||||
|
||||
size_t mg_print_ip6(void (*out)(char, void *), void *arg, va_list *ap) {
|
||||
uint16_t *p = va_arg(*ap, uint16_t *);
|
||||
return print_ip6(out, arg, p);
|
||||
}
|
||||
|
||||
size_t mg_print_ip(void (*out)(char, void *), void *arg, va_list *ap) {
|
||||
struct mg_addr *addr = va_arg(*ap, struct mg_addr *);
|
||||
if (addr->is_ip6) return print_ip6(out, arg, (uint16_t *) addr->ip6);
|
||||
return print_ip4(out, arg, (uint8_t *) &addr->ip);
|
||||
}
|
||||
|
||||
size_t mg_print_ip_port(void (*out)(char, void *), void *arg, va_list *ap) {
|
||||
struct mg_addr *a = va_arg(*ap, struct mg_addr *);
|
||||
return mg_xprintf(out, arg, "%M:%hu", mg_print_ip, a, mg_ntohs(a->port));
|
||||
}
|
||||
|
||||
size_t mg_print_mac(void (*out)(char, void *), void *arg, va_list *ap) {
|
||||
uint8_t *p = va_arg(*ap, uint8_t *);
|
||||
return mg_xprintf(out, arg, "%02x:%02x:%02x:%02x:%02x:%02x", p[0], p[1], p[2],
|
||||
p[3], p[4], p[5]);
|
||||
}
|
||||
-38
@@ -87,44 +87,6 @@ int mg_check_ip_acl(struct mg_str acl, uint32_t remote_ip) {
|
||||
return allowed == '+';
|
||||
}
|
||||
|
||||
static size_t print_ip4(void (*out)(char, void *), void *arg, uint8_t *p) {
|
||||
return mg_xprintf(out, arg, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
|
||||
}
|
||||
|
||||
static size_t print_ip6(void (*out)(char, void *), void *arg, uint16_t *p) {
|
||||
return mg_xprintf(out, arg, "[%x:%x:%x:%x:%x:%x:%x:%x]", mg_ntohs(p[0]),
|
||||
mg_ntohs(p[1]), mg_ntohs(p[2]), mg_ntohs(p[3]),
|
||||
mg_ntohs(p[4]), mg_ntohs(p[5]), mg_ntohs(p[6]),
|
||||
mg_ntohs(p[7]));
|
||||
}
|
||||
|
||||
size_t mg_print_ip4(void (*out)(char, void *), void *arg, va_list *ap) {
|
||||
uint8_t *p = va_arg(*ap, uint8_t *);
|
||||
return print_ip4(out, arg, p);
|
||||
}
|
||||
|
||||
size_t mg_print_ip6(void (*out)(char, void *), void *arg, va_list *ap) {
|
||||
uint16_t *p = va_arg(*ap, uint16_t *);
|
||||
return print_ip6(out, arg, p);
|
||||
}
|
||||
|
||||
size_t mg_print_ip(void (*out)(char, void *), void *arg, va_list *ap) {
|
||||
struct mg_addr *addr = va_arg(*ap, struct mg_addr *);
|
||||
if (addr->is_ip6) return print_ip6(out, arg, (uint16_t *) addr->ip6);
|
||||
return print_ip4(out, arg, (uint8_t *) &addr->ip);
|
||||
}
|
||||
|
||||
size_t mg_print_ip_port(void (*out)(char, void *), void *arg, va_list *ap) {
|
||||
struct mg_addr *a = va_arg(*ap, struct mg_addr *);
|
||||
return mg_xprintf(out, arg, "%M:%hu", mg_print_ip, a, mg_ntohs(a->port));
|
||||
}
|
||||
|
||||
size_t mg_print_mac(void (*out)(char, void *), void *arg, va_list *ap) {
|
||||
uint8_t *p = va_arg(*ap, uint8_t *);
|
||||
return mg_xprintf(out, arg, "%02x:%02x:%02x:%02x:%02x:%02x", p[0], p[1], p[2],
|
||||
p[3], p[4], p[5]);
|
||||
}
|
||||
|
||||
#if MG_ENABLE_CUSTOM_MILLIS
|
||||
#else
|
||||
uint64_t mg_millis(void) {
|
||||
|
||||
@@ -25,12 +25,6 @@ uint64_t mg_millis(void);
|
||||
#define MG_IPADDR_PARTS(ADDR) \
|
||||
MG_U8P(ADDR)[0], MG_U8P(ADDR)[1], MG_U8P(ADDR)[2], MG_U8P(ADDR)[3]
|
||||
|
||||
size_t mg_print_ip(void (*out)(char, void *), void *arg, va_list *ap);
|
||||
size_t mg_print_ip_port(void (*out)(char, void *), void *arg, va_list *ap);
|
||||
size_t mg_print_ip4(void (*out)(char, void *), void *arg, va_list *ap);
|
||||
size_t mg_print_ip6(void (*out)(char, void *), void *arg, va_list *ap);
|
||||
size_t mg_print_mac(void (*out)(char, void *), void *arg, va_list *ap);
|
||||
|
||||
// Linked list management macros
|
||||
#define LIST_ADD_HEAD(type_, head_, elem_) \
|
||||
do { \
|
||||
|
||||
Reference in new issue
Block a user