XMMS2

src/lib/xmmsutils/strlist.c

Go to the documentation of this file.
00001 /*  XMMS2 - X Music Multiplexer System
00002  *  Copyright (C) 2003-2009 XMMS2 Team
00003  *
00004  *  PLUGINS ARE NOT CONSIDERED TO BE DERIVED WORK !!!
00005  *
00006  *  This library is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU Lesser General Public
00008  *  License as published by the Free Software Foundation; either
00009  *  version 2.1 of the License, or (at your option) any later version.
00010  *
00011  *  This library is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *  Lesser General Public License for more details.
00015  */
00016 
00017 /** @file
00018  * Functions for working with lists/arrays of strings.
00019  * All such lists are assumed to be NULL-terminated.
00020  */
00021 
00022 #include <stdlib.h>
00023 #include <stdarg.h>
00024 #include <unistd.h>
00025 #include <string.h>
00026 
00027 #include "xmmsc/xmmsc_strlist.h"
00028 
00029 /**
00030  * Convert a list of variable arguments into a list of strings.
00031  * Note: Assumes that the arguments provided are all strings.
00032  * @param first The first string. Cannot be NULL.
00033  * @param ap List of variable arguments.
00034  * @return A newly allocated list of strings.
00035  */
00036 char **
00037 xmms_valist_to_strlist (const char *first, va_list ap)
00038 {
00039     const char *cur = first;
00040     char **ret = NULL;
00041     int i, size = sizeof (char *);
00042 
00043     if (first == NULL)
00044         abort ();
00045 
00046     for (i = 0; cur != NULL; i++) {
00047         size += sizeof (char *);
00048         ret = realloc (ret, size);
00049         ret[i] = strdup (cur);
00050         cur = va_arg (ap, char *);
00051     }
00052     ret[i] = NULL;
00053 
00054     return ret;
00055 }
00056 
00057 /**
00058  * Convert a variable number of arguments into a list of strings.
00059  * Note: Assumes that the arguments provided are all strings.
00060  * @param first The first string. Cannot be NULL.
00061  * @param ... Variable number of strings.
00062  * @return A newly allocated list of strings.
00063  */
00064 char **
00065 xmms_vargs_to_strlist (const char *first, ...)
00066 {
00067     va_list ap;
00068     char **ret = NULL;
00069 
00070     if (first == NULL)
00071         abort ();
00072 
00073     va_start (ap, first);
00074     ret = xmms_valist_to_strlist (first, ap);
00075     va_end (ap);
00076 
00077     return ret;
00078 }
00079 
00080 /**
00081  * Get the number of strings in a list. Note that the real length of the
00082  * (char **) array will be larger by 1 element (containing the terminating NULL).
00083  * @param data The list of strings
00084  * @return Number of strings
00085  */
00086 int
00087 xmms_strlist_len (char **data)
00088 {
00089     int i;
00090     if (data == NULL)
00091         abort ();
00092     for (i = 0; data[i] != NULL; i++);
00093     return i;
00094 }
00095 
00096 /**
00097  * Destroy a list of strings. Equivalent to g_strfreev().
00098  * @param data The list to destroy.
00099  */
00100 void
00101 xmms_strlist_destroy (char **data)
00102 {
00103     int i;
00104     if (data == NULL)
00105         abort ();
00106     for (i = 0; data[i] != NULL; i++) {
00107         free (data[i]);
00108     }
00109     free (data);
00110 }
00111 
00112 /**
00113  * Return a copy of a list with newstr prepended.
00114  * @param data The original list.
00115  * @param newstr The string to prepend.
00116  * @return A newly allocated list of strings.
00117  */
00118 char **
00119 xmms_strlist_prepend_copy (char **data, char *newstr) {
00120     int i;
00121     char **ret;
00122 
00123     ret = malloc ((xmms_strlist_len (data) + 2) * sizeof (char *));
00124     ret[0] = strdup (newstr);
00125 
00126     for (i = 0; data[i] != NULL; i++)
00127         ret[i+1] = strdup (data[i]);
00128     ret[i+1] = NULL;
00129 
00130     return ret;
00131 }
00132 
00133 /**
00134  * Return a deep copy of a list.
00135  * @param strlist The original list.
00136  * @return A newly allocated list of strings.
00137  */
00138 char **
00139 xmms_strlist_copy (char **strlist)
00140 {
00141     char **ret;
00142     int i;
00143 
00144     ret = malloc ((xmms_strlist_len (strlist) + 1) * sizeof (char *));
00145 
00146     for (i = 0; strlist[i] != NULL; i++) {
00147         ret[i] = strdup (strlist[i]);
00148     }
00149 
00150     ret[i] = NULL;
00151 
00152     return ret;
00153 }