diff options
Diffstat (limited to 'src/util.c')
-rw-r--r-- | src/util.c | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..55953b8 --- /dev/null +++ b/src/util.c @@ -0,0 +1,121 @@ +/* LibSLV2 + * Copyright (C) 2006 Dave Robillard <drobilla@connect.carleton.ca> + * + * This library 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 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 General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#define _XOPEN_SOURCE 500 + +#include <util.h> +#include <stdlib.h> +#include <string.h> +#include <assert.h> +#include <stdarg.h> + + +void +ustrappend(uchar** dst, const uchar* suffix) +{ + assert(dst); + assert(*dst); + assert(suffix); + + const size_t new_length = strlen((char*)*dst) + strlen((char*)suffix) + 1; + *dst = realloc(*dst, (new_length * sizeof(char))); + assert(dst); + strcat((char*)*dst, (char*)suffix); +} + + +uchar* +ustrdup(const uchar* src) +{ + assert(src); + return (uchar*)strdup((char*)src); +} + + +uchar* +ustrjoin(const uchar* first, ...) +{ + // FIXME: this is horribly, awfully, disgracefully slow + + va_list args_list; + va_start(args_list, first); + + va_list args_copy; + va_copy(args_copy, args_list); + + uchar* result = vstrjoin(first, args_copy); + + //va_end(args_copy); + va_end(args_list); + + return result; + + /* + va_list args_list; + uchar* arg = NULL; + uchar* result = ustrdup(first); + + va_start(args_list, first); + + while ((arg = va_arg(args_list, uchar*)) != (uchar*)0) + ustrappend(&result, arg); + + va_end(args_list); + + return result;*/ +} + + +uchar* +vstrjoin(const uchar* first, va_list args_list) +{ + // FIXME: this is horribly, awfully, disgracefully slow + + uchar* arg = NULL; + uchar* result = ustrdup(first); + + while ((arg = va_arg(args_list, uchar*)) != NULL) + ustrappend(&result, arg); + + va_end(args_list); + + return result; +} + + + +/** Convert a URL to a local filesystem path (ie by chopping off the + * leading "file://". + * + * Returns NULL if URL is not a valid URL on the local filesystem. + * Result is simply a pointer in to \a url and must not be free()'d. + */ +const char* +url2path(const uchar* const url) +{ + /*assert(strlen((char*)url) > 8); + char* result = calloc(strlen((char*)url)-7+1, sizeof(char)); + strcpy(result, (char*)url+7); + return result;*/ + if (!strncmp((char*)url, "file://", 7)) + return (char*)url + 7; + else + return NULL; +} + + |