From c7488de510daaad8e2acdc32e3e31b3c9e318bbe Mon Sep 17 00:00:00 2001 From: Andrew Bettison Date: Wed, 11 Jul 2012 14:11:59 +0930 Subject: [PATCH] Add strbuf_helpers.[ch] Functions to help assemble strbuf contents. --- Android.mk | 1 + Makefile.in | 2 ++ strbuf_helpers.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ strbuf_helpers.h | 30 +++++++++++++++++++++++ 4 files changed, 96 insertions(+) create mode 100644 strbuf_helpers.c create mode 100644 strbuf_helpers.h diff --git a/Android.mk b/Android.mk index 0103ad41..8407b2d6 100644 --- a/Android.mk +++ b/Android.mk @@ -21,6 +21,7 @@ SERVALD_SRC_FILES = \ serval-dna/net.c \ serval-dna/mkdir.c \ serval-dna/strbuf.c \ + serval-dna/strbuf_helpers.c \ serval-dna/gateway.c \ serval-dna/overlay.c \ serval-dna/overlay_broadcast.c \ diff --git a/Makefile.in b/Makefile.in index fb92f77d..0bcccd8b 100644 --- a/Makefile.in +++ b/Makefile.in @@ -10,6 +10,7 @@ SRCS= main.c \ net.c \ mkdir.c \ strbuf.c \ + strbuf_helpers.c \ dna_identity.c \ encode.c \ fifo.c \ @@ -70,6 +71,7 @@ HDRS= fifo.h \ rhizome.h \ serval.h \ strbuf.h \ + strbuf_helpers.h \ sha2.h \ sqlite-amalgamation-3070900/sqlite3.h diff --git a/strbuf_helpers.c b/strbuf_helpers.c new file mode 100644 index 00000000..a1661113 --- /dev/null +++ b/strbuf_helpers.c @@ -0,0 +1,63 @@ +/* +Serval string buffer helper functions. +Copyright (C) 2012 The Serval Project + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include "strbuf_helpers.h" +#include + +strbuf strbuf_append_poll_events(strbuf sb, short events) +{ + static struct { short flags; const char *name; } symbols[] = { + { POLLIN, "IN" }, + { POLLPRI, "PRI" }, + { POLLOUT, "OUT" }, + { POLLERR, "ERR" }, + { POLLHUP, "HUP" }, + { POLLNVAL, "NVAL" }, + { POLLRDNORM, "RDNORM" }, + { POLLRDBAND, "RDBAND" }, +#ifdef POLLWRNORM + { POLLWRNORM, "WRNORM" }, +#endif +#ifdef POLLWRBAND + { POLLWRBAND, "WRBAND" }, +#endif +#ifdef POLLMSG + { POLLMSG, "MSG" }, +#endif +#ifdef POLLREMOVE + { POLLREMOVE, "REMOVE" }, +#endif +#ifdef POLLRDHUP + { POLLRDHUP, "RDHUP" }, +#endif + { 0, NULL } + }, *sp; + int n = 0; + for (sp = symbols; sp->name; ++sp) { + if (events & sp->flags) { + if (n) + strbuf_putc(sb, '|'); + strbuf_puts(sb, sp->name); + ++n; + } + } + if (!n) + strbuf_putc(sb, '0'); + return sb; +} diff --git a/strbuf_helpers.h b/strbuf_helpers.h new file mode 100644 index 00000000..26d24ecd --- /dev/null +++ b/strbuf_helpers.h @@ -0,0 +1,30 @@ +/* +Serval string buffer helper functions. +Copyright (C) 2012 The Serval Project + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#ifndef __STRBUF_HELPERS_H__ +#define __STRBUF_HELPERS_H__ + +#include "strbuf.h" + +/* Append a symbolic representation of the poll(2) event flags. + * @author Andrew Bettison + */ +strbuf strbuf_append_poll_events(strbuf sb, short events); + +#endif //__STRBUF_HELPERS_H__