diff options
author | Nikias Bassen | 2014-11-11 15:52:36 +0100 |
---|---|---|
committer | Nikias Bassen | 2014-12-02 19:22:25 +0100 |
commit | 34e6eeef656a205d4f65f128cb17bc81cca3b4d3 (patch) | |
tree | 3cbf63bc707654ffea3a28fd088e969e2edf8f68 /common/utils.h | |
parent | 816b587e32d894083bf884ca647e6c734b242e3f (diff) | |
download | usbmuxd-34e6eeef656a205d4f65f128cb17bc81cca3b4d3.tar.gz usbmuxd-34e6eeef656a205d4f65f128cb17bc81cca3b4d3.tar.bz2 |
Move utils to convenience library
Diffstat (limited to 'common/utils.h')
-rw-r--r-- | common/utils.h | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/common/utils.h b/common/utils.h new file mode 100644 index 0000000..1137a93 --- /dev/null +++ b/common/utils.h @@ -0,0 +1,92 @@ +/* + * utils.h + * + * Copyright (C) 2009 Hector Martin <hector@marcansoft.com> + * Copyright (C) 2009 Nikias Bassen <nikias@gmx.li> + * + * This library is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 2.1 of the + * License, or (at your option) any later version. + * + * This library 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 Lesser 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 UTILS_H +#define UTILS_H + +#include <poll.h> +#include <plist/plist.h> + +enum fdowner { + FD_LISTEN, + FD_CLIENT, + FD_USB +}; + +struct fdlist { + int count; + int capacity; + enum fdowner *owners; + struct pollfd *fds; +}; + +void fdlist_create(struct fdlist *list); +void fdlist_add(struct fdlist *list, enum fdowner owner, int fd, short events); +void fdlist_free(struct fdlist *list); +void fdlist_reset(struct fdlist *list); + +struct collection { + void **list; + int capacity; +}; + +void collection_init(struct collection *col); +void collection_add(struct collection *col, void *element); +void collection_remove(struct collection *col, void *element); +int collection_count(struct collection *col); +void collection_free(struct collection *col); +void collection_copy(struct collection *dest, struct collection *src); + +#define MERGE_(a,b) a ## _ ## b +#define LABEL_(a,b) MERGE_(a, b) +#define UNIQUE_VAR(a) LABEL_(a, __LINE__) + +#define FOREACH(var, col) \ + do { \ + int UNIQUE_VAR(_iter); \ + for(UNIQUE_VAR(_iter)=0; UNIQUE_VAR(_iter)<(col)->capacity; UNIQUE_VAR(_iter)++) { \ + if(!(col)->list[UNIQUE_VAR(_iter)]) continue; \ + var = (col)->list[UNIQUE_VAR(_iter)]; + +#define ENDFOREACH \ + } \ + } while(0); + +#ifndef HAVE_STPCPY +char *stpcpy(char * s1, const char * s2); +#endif +char *string_concat(const char *str, ...); + +void buffer_read_from_filename(const char *filename, char **buffer, uint64_t *length); +void buffer_write_to_filename(const char *filename, const char *buffer, uint64_t length); + +enum plist_format_t { + PLIST_FORMAT_XML, + PLIST_FORMAT_BINARY +}; + +int plist_read_from_filename(plist_t *plist, const char *filename); +int plist_write_to_filename(plist_t plist, const char *filename, enum plist_format_t format); + +uint64_t mstime64(void); +void get_tick_count(struct timeval * tv); + +#endif |