1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 17:38:00 +00:00

A few new minor utility functions.

A function to compare two strings _both_ in ptrlen form (I've had
ptrlen_eq_string for ages, but for some reason, never quite needed
ptrlen_eq_ptrlen). A function to ask whether one ptrlen starts with
another (and, optionally, return a ptrlen giving the remaining part of
the longer string). And the va_list version of logeventf, which I
really ought to have written in the first place by sheer habit, even
if it was only needed by logeventf itself.
This commit is contained in:
Simon Tatham 2018-10-13 17:03:27 +01:00
parent 3229d468b9
commit 14f797305a
4 changed files with 28 additions and 3 deletions

View File

@ -256,15 +256,18 @@ void logevent_and_free(LogContext *ctx, char *event)
sfree(event);
}
void logeventvf(LogContext *ctx, const char *fmt, va_list ap)
{
logevent_and_free(ctx, dupvprintf(fmt, ap));
}
void logeventf(LogContext *ctx, const char *fmt, ...)
{
va_list ap;
char *buf;
va_start(ap, fmt);
buf = dupvprintf(fmt, ap);
logeventvf(ctx, fmt, ap);
va_end(ap);
logevent_and_free(ctx, buf);
}
/*

18
misc.c
View File

@ -1259,6 +1259,24 @@ int ptrlen_eq_string(ptrlen pl, const char *str)
return (pl.len == len && !memcmp(pl.ptr, str, len));
}
int ptrlen_eq_ptrlen(ptrlen pl1, ptrlen pl2)
{
return (pl1.len == pl2.len && !memcmp(pl1.ptr, pl2.ptr, pl1.len));
}
int ptrlen_startswith(ptrlen whole, ptrlen prefix, ptrlen *tail)
{
if (whole.len >= prefix.len &&
!memcmp(whole.ptr, prefix.ptr, prefix.len)) {
if (tail) {
tail->ptr = (const char *)whole.ptr + prefix.len;
tail->len = whole.len - prefix.len;
}
return TRUE;
}
return FALSE;
}
char *mkstr(ptrlen pl)
{
char *p = snewn(pl.len + 1, char);

2
misc.h
View File

@ -100,6 +100,8 @@ ptrlen make_ptrlen(const void *ptr, size_t len);
ptrlen ptrlen_from_asciz(const char *str);
ptrlen ptrlen_from_strbuf(strbuf *sb);
int ptrlen_eq_string(ptrlen pl, const char *str);
int ptrlen_eq_ptrlen(ptrlen pl1, ptrlen pl2);
int ptrlen_startswith(ptrlen whole, ptrlen prefix, ptrlen *tail);
char *mkstr(ptrlen pl);
int string_length_for_printf(size_t);
/* Derive two printf arguments from a ptrlen, suitable for "%.*s" */

View File

@ -1484,6 +1484,8 @@ void logtraffic(LogContext *logctx, unsigned char c, int logmode);
void logflush(LogContext *logctx);
void logevent(LogContext *logctx, const char *event);
void logeventf(LogContext *logctx, const char *fmt, ...);
void logeventvf(LogContext *logctx, const char *fmt, va_list ap);
/*
* Pass a dynamically allocated string to logevent and immediately
* free it. Intended for use by wrapper macros which pass the return