mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-01-09 01:18:00 +00:00
3396c97da9
Now that the new CMake build system is encouraging us to lay out the code like a set of libraries, it seems like a good idea to make them look more _like_ libraries, by putting things into separate modules as far as possible. This fixes several previous annoyances in which you had to link against some object in order to get a function you needed, but that object also contained other functions you didn't need which included link-time symbol references you didn't want to have to deal with. The usual offender was subsidiary supporting programs including misc.c for some innocuous function and then finding they had to deal with the requirements of buildinfo(). This big reorganisation introduces three new subdirectories called 'utils', one at the top level and one in each platform subdir. In each case, the directory contains basically the same files that were previously placed in the 'utils' build-time library, except that the ones that were extremely miscellaneous (misc.c, utils.c, uxmisc.c, winmisc.c, winmiscs.c, winutils.c) have been split up into much smaller pieces.
96 lines
2.3 KiB
C
96 lines
2.3 KiB
C
/*
|
|
* Functions to deal with ptrlens.
|
|
*/
|
|
|
|
#include "defs.h"
|
|
#include "misc.h"
|
|
#include "ssh.h"
|
|
|
|
bool ptrlen_eq_string(ptrlen pl, const char *str)
|
|
{
|
|
size_t len = strlen(str);
|
|
return (pl.len == len && !memcmp(pl.ptr, str, len));
|
|
}
|
|
|
|
bool ptrlen_eq_ptrlen(ptrlen pl1, ptrlen pl2)
|
|
{
|
|
return (pl1.len == pl2.len && !memcmp(pl1.ptr, pl2.ptr, pl1.len));
|
|
}
|
|
|
|
int ptrlen_strcmp(ptrlen pl1, ptrlen pl2)
|
|
{
|
|
size_t minlen = pl1.len < pl2.len ? pl1.len : pl2.len;
|
|
if (minlen) { /* tolerate plX.ptr==NULL as long as plX.len==0 */
|
|
int cmp = memcmp(pl1.ptr, pl2.ptr, minlen);
|
|
if (cmp)
|
|
return cmp;
|
|
}
|
|
return pl1.len < pl2.len ? -1 : pl1.len > pl2.len ? +1 : 0;
|
|
}
|
|
|
|
bool 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;
|
|
}
|
|
|
|
bool ptrlen_endswith(ptrlen whole, ptrlen suffix, ptrlen *tail)
|
|
{
|
|
if (whole.len >= suffix.len &&
|
|
!memcmp((char *)whole.ptr + (whole.len - suffix.len),
|
|
suffix.ptr, suffix.len)) {
|
|
if (tail) {
|
|
tail->ptr = whole.ptr;
|
|
tail->len = whole.len - suffix.len;
|
|
}
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
ptrlen ptrlen_get_word(ptrlen *input, const char *separators)
|
|
{
|
|
const char *p = input->ptr, *end = p + input->len;
|
|
ptrlen toret;
|
|
|
|
while (p < end && strchr(separators, *p))
|
|
p++;
|
|
toret.ptr = p;
|
|
while (p < end && !strchr(separators, *p))
|
|
p++;
|
|
toret.len = p - (const char *)toret.ptr;
|
|
|
|
size_t to_consume = p - (const char *)input->ptr;
|
|
assert(to_consume <= input->len);
|
|
input->ptr = (const char *)input->ptr + to_consume;
|
|
input->len -= to_consume;
|
|
|
|
return toret;
|
|
}
|
|
|
|
char *mkstr(ptrlen pl)
|
|
{
|
|
char *p = snewn(pl.len + 1, char);
|
|
memcpy(p, pl.ptr, pl.len);
|
|
p[pl.len] = '\0';
|
|
return p;
|
|
}
|
|
|
|
bool strstartswith(const char *s, const char *t)
|
|
{
|
|
return !strncmp(s, t, strlen(t));
|
|
}
|
|
|
|
bool strendswith(const char *s, const char *t)
|
|
{
|
|
size_t slen = strlen(s), tlen = strlen(t);
|
|
return slen >= tlen && !strcmp(s + (slen - tlen), t);
|
|
}
|