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 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
|
/* Copyright (C) 2010-2020 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (string_list.c).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <lists/string_list.h>
#include <compat/strl.h>
#include <compat/posix_string.h>
#include <string/stdstring.h>
static bool string_list_deinitialize_internal(struct string_list *list)
{
if (!list)
return false;
if (list->elems)
{
unsigned i;
for (i = 0; i < (unsigned)list->size; i++)
{
if (list->elems[i].data)
free(list->elems[i].data);
if (list->elems[i].userdata)
free(list->elems[i].userdata);
list->elems[i].data = NULL;
list->elems[i].userdata = NULL;
}
free(list->elems);
}
list->elems = NULL;
return true;
}
/**
* string_list_capacity:
* @list : pointer to string list
* @cap : new capacity for string list.
*
* Change maximum capacity of string list's size.
*
* @return true if successful, otherwise false.
**/
static bool string_list_capacity(struct string_list *list, size_t cap)
{
struct string_list_elem *new_data = (struct string_list_elem*)
realloc(list->elems, cap * sizeof(*new_data));
if (!new_data)
return false;
if (cap > list->cap)
memset(&new_data[list->cap], 0, sizeof(*new_data) * (cap - list->cap));
list->elems = new_data;
list->cap = cap;
return true;
}
/**
* string_list_free
* @list : pointer to string list object
*
* Frees a string list.
**/
void string_list_free(struct string_list *list)
{
if (!list)
return;
string_list_deinitialize_internal(list);
free(list);
}
bool string_list_deinitialize(struct string_list *list)
{
if (!list)
return false;
if (!string_list_deinitialize_internal(list))
return false;
list->elems = NULL;
list->size = 0;
list->cap = 0;
return true;
}
/**
* string_list_new:
*
* Creates a new string list. Has to be freed manually.
*
* @return New string list if successful, otherwise NULL.
**/
struct string_list *string_list_new(void)
{
struct string_list_elem *
elems = NULL;
struct string_list *list = (struct string_list*)
malloc(sizeof(*list));
if (!list)
return NULL;
if (!(elems = (struct string_list_elem*)
calloc(32, sizeof(*elems))))
{
string_list_free(list);
return NULL;
}
list->elems = elems;
list->size = 0;
list->cap = 32;
return list;
}
bool string_list_initialize(struct string_list *list)
{
struct string_list_elem *
elems = NULL;
if (!list)
return false;
if (!(elems = (struct string_list_elem*)
calloc(32, sizeof(*elems))))
{
string_list_deinitialize(list);
return false;
}
list->elems = elems;
list->size = 0;
list->cap = 32;
return true;
}
/**
* string_list_append:
* @list : pointer to string list
* @elem : element to add to the string list
* @attr : attributes of new element.
*
* Appends a new element to the string list.
*
* @return true if successful, otherwise false.
**/
bool string_list_append(struct string_list *list, const char *elem,
union string_list_elem_attr attr)
{
char *data_dup = NULL;
/* Note: If 'list' is incorrectly initialised
* (i.e. if struct is zero initialised and
* string_list_initialize() is not called on
* it) capacity will be zero. This will cause
* a segfault. Handle this case by forcing the new
* capacity to a fixed size of 32 */
if (list->size >= list->cap &&
!string_list_capacity(list,
(list->cap > 0) ? (list->cap * 2) : 32))
return false;
if (!(data_dup = strdup(elem)))
return false;
list->elems[list->size].data = data_dup;
list->elems[list->size].attr = attr;
list->size++;
return true;
}
/**
* string_list_append_n:
* @list : pointer to string list
* @elem : element to add to the string list
* @length : read at most this many bytes from elem
* @attr : attributes of new element.
*
* Appends a new element to the string list.
*
* @return true if successful, otherwise false.
**/
bool string_list_append_n(struct string_list *list, const char *elem,
unsigned length, union string_list_elem_attr attr)
{
char *data_dup = NULL;
if (list->size >= list->cap &&
!string_list_capacity(list, list->cap * 2))
return false;
if (!(data_dup = (char*)malloc(length + 1)))
return false;
strlcpy(data_dup, elem, length + 1);
list->elems[list->size].data = data_dup;
list->elems[list->size].attr = attr;
list->size++;
return true;
}
/**
* string_list_set:
* @list : pointer to string list
* @idx : index of element in string list
* @str : value for the element.
*
* Set value of element inside string list.
**/
void string_list_set(struct string_list *list,
unsigned idx, const char *str)
{
free(list->elems[idx].data);
list->elems[idx].data = strdup(str);
}
/**
* string_list_join_concat:
* @s : buffer that @list will be joined to.
* @len : length of @s.
* @list : pointer to string list.
* @delim : delimiter character for @list.
*
* A string list will be joined/concatenated as a
* string to @buffer, delimited by @delim.
**/
void string_list_join_concat(char *s, size_t len,
const struct string_list *list, const char *delim)
{
size_t i;
size_t _len = strlen_size(s, len);
/* If @s is already 'full', nothing
* further can be added
* > This condition will also be triggered
* if @s is not NULL-terminated,
* in which case any attempt to increment
* @s or decrement @len would lead to
* undefined behaviour */
if (_len >= len)
return;
s += _len;
len -= _len;
for (i = 0; i < list->size; i++)
{
size_t __len = strlcat(s, list->elems[i].data, len);
if ((i + 1) < list->size)
strlcpy(s + __len, delim, len - __len);
}
}
/**
* string_list_join_concat:
* @s : buffer that @list will be joined to.
* @len : length of @s.
* @list : pointer to string list.
* @delim : delimiter character for @list.
*
* Specialized version of string_list_join_concat
* without the bounds check.
*
* A string list will be joined/concatenated as a
* string to @s, delimited by @delim.
*
* TODO/FIXME - eliminate the strlcat
**/
void string_list_join_concat_special(char *s, size_t len,
const struct string_list *list, const char *delim)
{
size_t i;
for (i = 0; i < list->size; i++)
{
size_t __len = strlcat(s, list->elems[i].data, len);
if ((i + 1) < list->size)
strlcpy(s + __len, delim, len - __len);
}
}
/**
* string_split:
* @str : string to turn into a string list
* @delim : delimiter character to use for splitting the string.
*
* Creates a new string list based on string @str, delimited by @delim.
*
* Returns: new string list if successful, otherwise NULL.
*/
struct string_list *string_split(const char *str, const char *delim)
{
char *save = NULL;
char *copy = NULL;
const char *tmp = NULL;
struct string_list *list = string_list_new();
if (!list)
return NULL;
if (!(copy = strdup(str)))
goto error;
tmp = strtok_r(copy, delim, &save);
while (tmp)
{
union string_list_elem_attr attr;
attr.i = 0;
if (!string_list_append(list, tmp, attr))
goto error;
tmp = strtok_r(NULL, delim, &save);
}
free(copy);
return list;
error:
string_list_free(list);
free(copy);
return NULL;
}
bool string_split_noalloc(struct string_list *list,
const char *str, const char *delim)
{
char *save = NULL;
char *copy = NULL;
const char *tmp = NULL;
if (!list)
return false;
if (!(copy = strdup(str)))
return false;
tmp = strtok_r(copy, delim, &save);
while (tmp)
{
union string_list_elem_attr attr;
attr.i = 0;
if (!string_list_append(list, tmp, attr))
{
free(copy);
return false;
}
tmp = strtok_r(NULL, delim, &save);
}
free(copy);
return true;
}
/**
* string_separate:
* @str : string to turn into a string list
* @delim : delimiter character to use for separating the string.
*
* Creates a new string list based on string @str, delimited by @delim.
* Includes empty strings - i.e. two adjacent delimiters will resolve
* to a string list element of "".
*
* @return New string list if successful, otherwise NULL.
**/
struct string_list *string_separate(char *str, const char *delim)
{
char *token = NULL;
char **str_ptr = NULL;
struct string_list *list = NULL;
/* Sanity check */
if (!str || string_is_empty(delim))
return NULL;
if (!(list = string_list_new()))
return NULL;
str_ptr = &str;
token = string_tokenize(str_ptr, delim);
while (token)
{
union string_list_elem_attr attr;
attr.i = 0;
if (!string_list_append(list, token, attr))
{
free(token);
string_list_free(list);
return NULL;
}
free(token);
token = string_tokenize(str_ptr, delim);
}
return list;
}
bool string_separate_noalloc(
struct string_list *list,
char *str, const char *delim)
{
char *token = NULL;
char **str_ptr = NULL;
/* Sanity check */
if (!str || string_is_empty(delim) || !list)
return false;
str_ptr = &str;
token = string_tokenize(str_ptr, delim);
while (token)
{
union string_list_elem_attr attr;
attr.i = 0;
if (!string_list_append(list, token, attr))
{
free(token);
return false;
}
free(token);
token = string_tokenize(str_ptr, delim);
}
return true;
}
/**
* string_list_find_elem:
*
* @param list
* Pointer to string list
* @param elem
* Element to find inside the string list.
*
* Searches for an element (@elem) inside the string list.
*
* @return Number of elements found, otherwise 0.
*/
int string_list_find_elem(const struct string_list *list, const char *elem)
{
if (list)
{
size_t i;
for (i = 0; i < list->size; i++)
{
if (string_is_equal_noncase(list->elems[i].data, elem))
return (int)(i + 1);
}
}
return 0;
}
/**
* string_list_find_elem_prefix:
*
* @param list
* Pointer to string list
* @param prefix
* Prefix to append to @elem
* @param elem
* Element to find inside the string list.
*
* Searches for an element (@elem) inside the string list. Will
* also search for the same element prefixed by @prefix.
*
* @return true if element could be found, otherwise false.
*/
bool string_list_find_elem_prefix(const struct string_list *list,
const char *prefix, const char *elem)
{
if (list)
{
size_t i;
char prefixed[255];
size_t _len = strlcpy(prefixed, prefix, sizeof(prefixed));
strlcpy(prefixed + _len, elem, sizeof(prefixed) - _len);
for (i = 0; i < list->size; i++)
{
if ( string_is_equal_noncase(list->elems[i].data, elem)
|| string_is_equal_noncase(list->elems[i].data, prefixed))
return true;
}
}
return false;
}
struct string_list *string_list_clone(const struct string_list *src)
{
size_t i;
struct string_list_elem *elems = NULL;
struct string_list *dest = (struct string_list*)
malloc(sizeof(struct string_list));
if (!dest)
return NULL;
dest->elems = NULL;
dest->size = src->size;
if (src->cap < dest->size)
dest->cap = dest->size;
else
dest->cap = src->cap;
if (!(elems = (struct string_list_elem*)
calloc(dest->cap, sizeof(struct string_list_elem))))
{
free(dest);
return NULL;
}
dest->elems = elems;
for (i = 0; i < src->size; i++)
{
const char *_src = src->elems[i].data;
size_t len = _src ? strlen(_src) : 0;
dest->elems[i].data = NULL;
dest->elems[i].attr = src->elems[i].attr;
if (len != 0)
{
char *result = (char*)malloc(len + 1);
if (result)
{
strcpy(result, _src);
dest->elems[i].data = result;
}
}
}
return dest;
}
|