XMMS2
strlist.c
Go to the documentation of this file.
1/* XMMS2 - X Music Multiplexer System
2 * Copyright (C) 2003-2011 XMMS2 Team
3 *
4 * PLUGINS ARE NOT CONSIDERED TO BE DERIVED WORK !!!
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 */
16
17/** @file
18 * Functions for working with lists/arrays of strings.
19 * All such lists are assumed to be NULL-terminated.
20 */
21
22#include <stdlib.h>
23#include <stdarg.h>
24#include <unistd.h>
25#include <string.h>
26
27#include "xmmsc/xmmsc_strlist.h"
28
29/**
30 * Convert a list of variable arguments into a list of strings.
31 * Note: Assumes that the arguments provided are all strings.
32 * @param first The first string. Cannot be NULL.
33 * @param ap List of variable arguments.
34 * @return A newly allocated list of strings.
35 */
36char **
37xmms_valist_to_strlist (const char *first, va_list ap)
38{
39 const char *cur = first;
40 char **ret = NULL;
41 int i, size = sizeof (char *);
42
43 if (first == NULL)
44 abort ();
45
46 for (i = 0; cur != NULL; i++) {
47 size += sizeof (char *);
48 ret = realloc (ret, size);
49 ret[i] = strdup (cur);
50 cur = va_arg (ap, char *);
51 }
52 ret[i] = NULL;
53
54 return ret;
55}
56
57/**
58 * Convert a variable number of arguments into a list of strings.
59 * Note: Assumes that the arguments provided are all strings.
60 * @param first The first string. Cannot be NULL.
61 * @param ... Variable number of strings.
62 * @return A newly allocated list of strings.
63 */
64char **
65xmms_vargs_to_strlist (const char *first, ...)
66{
67 va_list ap;
68 char **ret = NULL;
69
70 if (first == NULL)
71 abort ();
72
73 va_start (ap, first);
74 ret = xmms_valist_to_strlist (first, ap);
75 va_end (ap);
76
77 return ret;
78}
79
80/**
81 * Get the number of strings in a list. Note that the real length of the
82 * (char **) array will be larger by 1 element (containing the terminating NULL).
83 * @param data The list of strings
84 * @return Number of strings
85 */
86int
87xmms_strlist_len (char **data)
88{
89 int i;
90 if (data == NULL)
91 abort ();
92 for (i = 0; data[i] != NULL; i++);
93 return i;
94}
95
96/**
97 * Destroy a list of strings. Equivalent to g_strfreev().
98 * @param data The list to destroy.
99 */
100void
102{
103 int i;
104 if (data == NULL)
105 abort ();
106 for (i = 0; data[i] != NULL; i++) {
107 free (data[i]);
108 }
109 free (data);
110}
111
112/**
113 * Return a copy of a list with newstr prepended.
114 * @param data The original list.
115 * @param newstr The string to prepend.
116 * @return A newly allocated list of strings.
117 */
118char **
119xmms_strlist_prepend_copy (char **data, char *newstr) {
120 int i;
121 char **ret;
122
123 ret = malloc ((xmms_strlist_len (data) + 2) * sizeof (char *));
124 ret[0] = strdup (newstr);
125
126 for (i = 0; data[i] != NULL; i++)
127 ret[i+1] = strdup (data[i]);
128 ret[i+1] = NULL;
129
130 return ret;
131}
132
133/**
134 * Return a deep copy of a list.
135 * @param strlist The original list.
136 * @return A newly allocated list of strings.
137 */
138char **
139xmms_strlist_copy (char **strlist)
140{
141 char **ret;
142 int i;
143
144 ret = malloc ((xmms_strlist_len (strlist) + 1) * sizeof (char *));
145
146 for (i = 0; strlist[i] != NULL; i++) {
147 ret[i] = strdup (strlist[i]);
148 }
149
150 ret[i] = NULL;
151
152 return ret;
153}
char ** xmms_valist_to_strlist(const char *first, va_list ap)
Convert a list of variable arguments into a list of strings.
Definition strlist.c:37
char ** xmms_strlist_prepend_copy(char **data, char *newstr)
Return a copy of a list with newstr prepended.
Definition strlist.c:119
int xmms_strlist_len(char **data)
Get the number of strings in a list.
Definition strlist.c:87
void xmms_strlist_destroy(char **data)
Destroy a list of strings.
Definition strlist.c:101
char ** xmms_vargs_to_strlist(const char *first,...)
Convert a variable number of arguments into a list of strings.
Definition strlist.c:65
char ** xmms_strlist_copy(char **strlist)
Return a deep copy of a list.
Definition strlist.c:139