1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
/*
* MOC - music on console
* Copyright (C) 2009 Damian Pietras <daper@daper.net> and John Fitzgerald
*
* 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.
*
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include "common.h"
#include "lists.h"
struct lists_strs {
int size; /* Number of strings on the list */
int capacity; /* Number of allocated strings */
char **strs;
};
/* Allocate a new list of strings and return its address. */
lists_t_strs *lists_strs_new (int reserve)
{
lists_t_strs *result;
assert (reserve >= 0);
result = (lists_t_strs *) xmalloc (sizeof (lists_t_strs));
result->size = 0;
result->capacity = (reserve ? reserve : 64);
result->strs = (char **) xcalloc (sizeof (char *), result->capacity);
return result;
}
/* Clear a list to an empty state. */
void lists_strs_clear (lists_t_strs *list)
{
int ix;
assert (list);
for (ix = 0; ix < list->size; ix += 1)
free ((void *) list->strs[ix]);
list->size = 0;
}
/* Free all storage associated with a list of strings. */
void lists_strs_free (lists_t_strs *list)
{
assert (list);
lists_strs_clear (list);
free (list->strs);
free (list);
}
/* Return the number of strings in a list. */
int lists_strs_size (const lists_t_strs *list)
{
assert (list);
return list->size;
}
/* Return the total number of strings which could be held without growing. */
int lists_strs_capacity (const lists_t_strs *list)
{
assert (list);
return list->capacity;
}
/* Return true iff the list has no members. */
bool lists_strs_empty (const lists_t_strs *list)
{
assert (list);
return list->size == 0 ? true : false;
}
/* Given an index, return the string at that position in a list. */
char *lists_strs_at (const lists_t_strs *list, int index)
{
assert (list);
assert (LIMIT(index, list->size));
return list->strs[index];
}
/* Sort string list into an order determined by caller's comparitor. */
void lists_strs_sort (lists_t_strs *list, lists_t_compare *compare)
{
assert (list);
assert (compare);
qsort (list->strs, list->size, sizeof (char *), compare);
}
/* Reverse the order of entries in a list. */
void lists_strs_reverse (lists_t_strs *list)
{
int ix, iy;
assert (list);
for (ix = 0, iy = list->size - 1; ix < iy; ix += 1, iy -= 1) {
char *str;
str = list->strs[ix];
list->strs[ix] = list->strs[iy];
list->strs[iy] = str;
}
}
/* Take a string and push it onto the end of a list
* (expanding the list if necessary). */
void lists_strs_push (lists_t_strs *list, char *s)
{
assert (list);
assert (s);
if (list->size == list->capacity) {
list->capacity *= 2;
list->strs = (char **) xrealloc (list->strs, list->capacity * sizeof (char *));
}
list->strs[list->size] = s;
list->size += 1;
}
/* Remove the last string on the list and return it, or NULL if the list
* is empty. */
char *lists_strs_pop (lists_t_strs *list)
{
char *result;
assert (list);
result = NULL;
if (list->size > 0) {
list->size -= 1;
result = list->strs[list->size];
}
return result;
}
/* Replace the nominated string with a new one and return the old one. */
char *lists_strs_swap (lists_t_strs *list, int index, char *s)
{
char *result;
assert (list);
assert (LIMIT(index, list->size));
assert (s);
result = list->strs[index];
list->strs[index] = s;
return result;
}
/* Copy a string and append it to the end of a list. */
void lists_strs_append (lists_t_strs *list, const char *s)
{
char *str;
assert (list);
assert (s);
str = xstrdup (s);
lists_strs_push (list, str);
}
/* Remove a string from the end of the list and free it. */
void lists_strs_remove (lists_t_strs *list)
{
char *str;
assert (list);
str = lists_strs_pop (list);
if (str)
free (str);
}
/* Replace the nominated string with a copy of the new one
* and free the old one. */
void lists_strs_replace (lists_t_strs *list, int index, char *s)
{
char *str;
assert (list);
assert (LIMIT(index, list->size));
str = xstrdup (s);
str = lists_strs_swap (list, index, str);
free (str);
}
/* Split a string at any delimiter in given string. The resulting segments
* are appended to the given string list. Returns the number of tokens
* appended. */
int lists_strs_split (lists_t_strs *list, const char *s, const char *delim)
{
int result;
char *str, *token, *saveptr;
assert (list);
assert (s);
assert (delim);
result = 0;
str = xstrdup (s);
token = strtok_r (str, delim, &saveptr);
while (token) {
result += 1;
lists_strs_append (list, token);
token = strtok_r (NULL, delim, &saveptr);
}
free (str);
return result;
}
/* Tokenise a string and append the tokens to the list.
* Returns the number of tokens appended. */
int lists_strs_tokenise (lists_t_strs *list, const char *s)
{
int result;
assert (list);
assert (s);
result = lists_strs_split (list, s, " \t");
return result;
}
/* Return the concatenation of all the strings in a list using the
* given format for each, or NULL if the list is empty. */
GCC_DIAG_OFF(format-nonliteral)
char *lists_strs_fmt (const lists_t_strs *list, const char *fmt)
{
int len, ix, rc;
char *result, *ptr;
assert (list);
assert (strstr (fmt, "%s"));
result = NULL;
if (!lists_strs_empty (list)) {
len = 0;
for (ix = 0; ix < lists_strs_size (list); ix += 1)
len += strlen (lists_strs_at (list, ix));
len += ix * (strlen (fmt) - 2);
ptr = result = xmalloc (len + 1);
for (ix = 0; ix < lists_strs_size (list); ix += 1) {
rc = snprintf (ptr, len + 1, fmt, lists_strs_at (list, ix));
if (rc > len)
fatal ("Allocated string area was too small!");
len -= rc;
ptr += rc;
}
}
return result;
}
GCC_DIAG_ON(format-nonliteral)
/* Return the concatenation of all the strings in a list, or NULL
* if the list is empty. */
char *lists_strs_cat (const lists_t_strs *list)
{
char *result;
assert (list);
result = lists_strs_fmt (list, "%s");
return result;
}
/* Return a "snapshot" of the given string list. The returned memory is a
* null-terminated list of pointers to the given list's strings copied into
* memory allocated after the pointer list. This list is suitable for passing
* to functions which take such a list as an argument (e.g., execv()).
* Invoking free() on the returned pointer also frees the strings. */
char **lists_strs_save (const lists_t_strs *list)
{
int ix, size;
char *ptr, **result;
assert (list);
size = 0;
for (ix = 0; ix < lists_strs_size (list); ix += 1)
size += strlen (lists_strs_at (list, ix)) + 1;
size += sizeof (char *) * (lists_strs_size (list) + 1);
result = (char **) xmalloc (size);
ptr = (char *) (result + lists_strs_size (list) + 1);
for (ix = 0; ix < lists_strs_size (list); ix += 1) {
strcpy (ptr, lists_strs_at (list, ix));
result[ix] = ptr;
ptr += strlen (ptr) + 1;
}
result[ix] = NULL;
return result;
}
/* Reload saved strings into a list. The reloaded strings are appended
* to the list. The number of items reloaded is returned. */
int lists_strs_load (lists_t_strs *list, const char **saved)
{
int size;
assert (list);
assert (saved);
size = lists_strs_size (list);
while (*saved)
lists_strs_append (list, *saved++);
return lists_strs_size (list) - size;
}
/* Given a string, return the index of the first list entry which matches
* it. If not found, return the total number of entries.
* The comparison is case-insensitive. */
int lists_strs_find (lists_t_strs *list, const char *sought)
{
int result;
assert (list);
assert (sought);
for (result = 0; result < lists_strs_size (list); result += 1) {
if (!strcasecmp (lists_strs_at (list, result), sought))
break;
}
return result;
}
/* Given a string, return true iff it exists in the list. */
bool lists_strs_exists (lists_t_strs *list, const char *sought)
{
bool result = false;
assert (list);
assert (sought);
if (lists_strs_find (list, sought) < lists_strs_size (list))
result = true;
return result;
}
|