From 348f6405071bde87b6c914d105591f5892d5419e Mon Sep 17 00:00:00 2001 From: ac-stef Date: Wed, 7 Jan 2015 20:29:10 +0100 Subject: [PATCH] new tool functions --- source/src/tools.cpp | 13 +++++++++++++ source/src/tools.h | 12 ++++++++++++ 2 files changed, 25 insertions(+) diff --git a/source/src/tools.cpp b/source/src/tools.cpp index f7be9af9..7c301968 100644 --- a/source/src/tools.cpp +++ b/source/src/tools.cpp @@ -422,6 +422,19 @@ const char *hiddenpwd(const char *pwd, int showchars) for(int i = (int)strlen(text) - 1; i >= sc; i--) text[i] = '*'; return text; } + +int getlistindex(const char *key, const char *list[], bool acceptnumeric, int deflt) +{ + int max = 0; + while(list[max][0]) if(!strcasecmp(key, list[max])) return max; else max++; + if(acceptnumeric && isdigit(key[0])) + { + int i = (int)strtol(key, NULL, 0); + if(i >= 0 && i < max) return i; + } + return deflt; +} + //////////////// geometry utils //////////////// static inline float det2x2(float a, float b, float c, float d) { return a*d - b*c; } diff --git a/source/src/tools.h b/source/src/tools.h index c87246ab..71f47379 100644 --- a/source/src/tools.h +++ b/source/src/tools.h @@ -586,6 +586,16 @@ static inline bool htcmp(const char *x, const char *y) return !strcmp(x, y); } +static inline uint hthash(const uchar *key) +{ + return *((uint *)key); +} + +static inline bool htcmp(const uchar *x, const uchar *y) // assume "uchar *" points to 32byte public keys +{ + return !memcmp(x, y, 32); +} + static inline uint hthash(int key) { return key; @@ -832,6 +842,7 @@ inline char *newstring(const char *s) { return newstring(s, strlen(s)) inline char *newstringbuf() { return newstring(MAXSTRLEN-1); } inline char *newstringbuf(const char *s) { return newstring(s, MAXSTRLEN-1); } inline void delstring(const char *s) { delete[] (char *)s; } +#define DELSTRING(s) if(s) { delstring(s); s = NULL; } #ifndef STANDALONE inline const char *_gettext(const char *msgid) @@ -976,6 +987,7 @@ extern int cmpiprange(const struct iprange *a, const struct iprange *b); extern int cmpipmatch(const struct iprange *a, const struct iprange *b); extern int cvecprintf(vector &v, const char *s, ...); extern const char *hiddenpwd(const char *pwd, int showchars = 0); +extern int getlistindex(const char *key, const char *list[], bool acceptnumeric = true, int deflt = -1); #if defined(WIN32) && !defined(_DEBUG) && !defined(__GNUC__) extern void stackdumper(unsigned int type, EXCEPTION_POINTERS *ep);