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 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
|
/* Copyright (C) 2010-2020 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (nested_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 <string/stdstring.h>
#include <lists/string_list.h>
#include <array/rbuf.h>
#include <array/rhmap.h>
#include <lists/nested_list.h>
struct nested_list_item
{
nested_list_item_t *parent_item;
nested_list_t *parent_list;
nested_list_t *children;
char *id;
const void *value;
};
struct nested_list
{
nested_list_item_t **items;
nested_list_item_t **item_map;
};
/**************************************/
/* Initialisation / De-Initialisation */
/**************************************/
/* Forward declaration - required since
* nested_list_free_list() is recursive */
static void nested_list_free_list(nested_list_t *list);
/* Frees contents of a nested list item */
static void nested_list_free_item(nested_list_item_t *item)
{
if (!item)
return;
item->parent_item = NULL;
item->parent_list = NULL;
if (item->children)
{
nested_list_free_list(item->children);
item->children = NULL;
}
if (item->id)
{
free(item->id);
item->id = NULL;
}
item->value = NULL;
free(item);
}
/* Frees contents of a nested list */
static void nested_list_free_list(nested_list_t *list)
{
size_t i;
if (!list)
return;
for (i = 0; i < RBUF_LEN(list->items); i++)
nested_list_free_item(list->items[i]);
RBUF_FREE(list->items);
RHMAP_FREE(list->item_map);
free(list);
}
/**
* nested_list_init:
*
* Creates a new empty nested list. Returned pointer
* must be freed using nested_list_free.
*
* Returns: Valid nested_list_t pointer if successful,
* otherwise NULL.
*/
nested_list_t *nested_list_init(void)
{
/* Create nested list */
nested_list_t *list = (nested_list_t*)malloc(sizeof(*list));
if (!list)
return NULL;
/* Initialise members */
list->items = NULL;
list->item_map = NULL;
return list;
}
/**
* nested_list_free:
* @list : pointer to nested_list_t object
*
* Frees specified nested list.
*/
void nested_list_free(nested_list_t *list)
{
nested_list_free_list(list);
}
/***********/
/* Setters */
/***********/
/* Creates and adds a new item to the specified
* nested list. Returns NULL if item matching 'id'
* already exists */
static nested_list_item_t *nested_list_add_item_to_list(nested_list_t *list,
nested_list_item_t *parent_item, const char *id, const void *value)
{
size_t num_items = 0;
nested_list_item_t *new_item = NULL;
nested_list_t *child_list = NULL;
if (!list || string_is_empty(id))
goto end;
num_items = RBUF_LEN(list->items);
/* Ensure that item does not already exist */
if (RHMAP_HAS_STR(list->item_map, id))
goto end;
/* Attempt to allocate a buffer slot for the
* new item */
if (!RBUF_TRYFIT(list->items, num_items + 1))
goto end;
/* Create new empty child list */
child_list = nested_list_init();
if (!child_list)
goto end;
/* Create new list item */
new_item = (nested_list_item_t*)malloc(sizeof(*new_item));
if (!new_item)
{
nested_list_free(child_list);
goto end;
}
/* Assign members */
new_item->parent_item = parent_item;
new_item->parent_list = list;
new_item->children = child_list;
new_item->id = strdup(id);
new_item->value = value;
/* Increment item buffer size */
RBUF_RESIZE(list->items, num_items + 1);
/* Add new item to buffer */
list->items[num_items] = new_item;
/* Update map */
RHMAP_SET_STR(list->item_map, id, new_item);
end:
return new_item;
}
/**
* nested_list_add_item:
*
* @list : pointer to nested_list_t object
* @address : a delimited list of item identifiers,
* corresponding to item 'levels'
* @delim : delimiter to use when splitting @address
* into individual ids
* @value : optional value (user data) associated with
* new list item. This is added to the last
* item specified by @address
*
* Appends a new item to the specified nested list.
* If @delim is NULL, item is added to the top level
* list (@list itself) with id equal to @address.
* Otherwise, @address is split by @delim and each
* id is added as new 'layer'. For example:
*
* > @address = "one:two:three", @delim = ":" will
* produce:
* top_level_list:one
* `- "one" list:two
* `- "two" list:three
* where @value is assigned to the "two" list:three
* item.
*
* Returns: true if successful, otherwise false. Will
* always return false if item specified by @address
* already exists in the nested list.
*/
bool nested_list_add_item(nested_list_t *list,
const char *address, const char *delim, const void *value)
{
struct string_list id_list = {0};
const char *top_id = NULL;
bool success = false;
if (!list || string_is_empty(address))
goto end;
/* If delim is NULL or address contains a single
* token, then we are adding an item to the top
* level list */
if (string_is_empty(delim))
top_id = address;
else
{
string_list_initialize(&id_list);
if (!string_split_noalloc(&id_list, address, delim) ||
(id_list.size < 1))
goto end;
if (id_list.size == 1)
top_id = id_list.elems[0].data;
}
if (!string_is_empty(top_id))
{
if (nested_list_add_item_to_list(list, NULL, top_id, value))
success = true;
}
else
{
nested_list_t *current_list = list;
nested_list_item_t *parent_item = NULL;
nested_list_item_t *next_item = NULL;
size_t i;
/* Loop over list item ids */
for (i = 0; i < id_list.size; i++)
{
const char *id = id_list.elems[i].data;
if (string_is_empty(id))
goto end;
/* If this is the last entry in the id list,
* then we are adding the item itself */
if (i == (id_list.size - 1))
{
if (nested_list_add_item_to_list(current_list,
parent_item, id, value))
success = true;
break;
}
/* Otherwise, id corresponds to a 'category' */
else
{
/* Check whether category item already exists */
next_item = RHMAP_GET_STR(current_list->item_map, id);
/* Create it, if required */
if (!next_item)
next_item = nested_list_add_item_to_list(current_list,
parent_item, id, NULL);
if (!next_item)
break;
/* Update pointers */
parent_item = next_item;
current_list = next_item->children;
}
}
}
end:
string_list_deinitialize(&id_list);
return success;
}
/***********/
/* Getters */
/***********/
/**
* nested_list_get_size:
*
* @list : pointer to nested_list_t object
*
* Fetches the current size (number of items) in
* the specified list.
*
* Returns: list size.
*/
size_t nested_list_get_size(nested_list_t *list)
{
if (!list)
return 0;
return RBUF_LEN(list->items);
}
/**
* nested_list_get_item:
*
* @list : pointer to nested_list_t object
* @address : a delimited list of item identifiers,
* corresponding to item 'levels'
* @delim : delimiter to use when splitting @address
* into individual ids
*
* Searches for (and returns) the list item corresponding
* to @address. If @delim is NULL, the top level list
* (@list itself) is searched for an item with an id
* equal to @address. Otherwise, @address is split by
* @delim and each id is searched for in a subsequent
* list level.
*
* Returns: valid nested_list_item_t pointer if item
* is found, otherwise NULL.
*/
nested_list_item_t *nested_list_get_item(nested_list_t *list,
const char *address, const char *delim)
{
nested_list_item_t *search_item = NULL;
struct string_list id_list = {0};
const char *top_id = NULL;
if (!list || string_is_empty(address))
goto end;
/* If delim is NULL or address contains a single
* token, then we are fetching an item from the
* top level list */
if (string_is_empty(delim))
top_id = address;
else
{
string_list_initialize(&id_list);
if (!string_split_noalloc(&id_list, address, delim) ||
(id_list.size < 1))
goto end;
if (id_list.size == 1)
top_id = id_list.elems[0].data;
}
if (!string_is_empty(top_id))
search_item = RHMAP_GET_STR(list->item_map, top_id);
else
{
/* Otherwise, search 'category' levels */
nested_list_t *current_list = list;
nested_list_item_t *next_item = NULL;
size_t i;
/* Loop over list item ids */
for (i = 0; i < id_list.size; i++)
{
const char *id = id_list.elems[i].data;
if (string_is_empty(id))
goto end;
/* If this is the last entry in the id list,
* then we are searching for the item itself */
if (i == (id_list.size - 1))
{
search_item = RHMAP_GET_STR(current_list->item_map, id);
break;
}
/* Otherwise, id corresponds to a 'category' */
else
{
next_item = RHMAP_GET_STR(current_list->item_map, id);
if (!next_item)
break;
/* Update pointer */
current_list = next_item->children;
}
}
}
end:
string_list_deinitialize(&id_list);
return search_item;
}
/**
* nested_list_get_item_idx:
*
* @list : pointer to nested_list_t object
* @idx : item index
*
* Fetches the item corresponding to index @idx in
* the top level list (@list itself) of the specified
* nested list.
*
* Returns: valid nested_list_item_t pointer if item
* exists, otherwise NULL.
*/
nested_list_item_t *nested_list_get_item_idx(nested_list_t *list,
size_t idx)
{
if (!list || (idx >= RBUF_LEN(list->items)))
return NULL;
return list->items[idx];
}
/**
* nested_list_item_get_parent:
*
* @list_item : pointer to nested_list_item_t object
*
* Fetches the parent item of the specified nested
* list item. If returned value is NULL, specified
* nested list item belongs to a top level list.
*
* Returns: valid nested_list_item_t pointer if item
* has a parent, otherwise NULL.
*/
nested_list_item_t *nested_list_item_get_parent(nested_list_item_t *list_item)
{
if (!list_item)
return NULL;
return list_item->parent_item;
}
/**
* nested_list_item_get_parent_list:
*
* @list_item : pointer to nested_list_item_t object
*
* Fetches a pointer to the nested list of which the
* specified list item is a direct member.
*
* Returns: valid nested_list_t pointer if successful,
* otherwise NULL.
*/
nested_list_t *nested_list_item_get_parent_list(nested_list_item_t *list_item)
{
if (!list_item)
return NULL;
return list_item->parent_list;
}
/**
* nested_list_item_get_children:
*
* @list_item : pointer to nested_list_item_t object
*
* Fetches a pointer to the nested list of child items
* belonging to the specified list item.
*
* Returns: valid nested_list_t pointer if item has
* children, otherwise NULL.
*/
nested_list_t *nested_list_item_get_children(nested_list_item_t *list_item)
{
if (!list_item ||
!list_item->children ||
(RBUF_LEN(list_item->children->items) < 1))
return NULL;
return list_item->children;
}
/**
* nested_list_item_get_id:
*
* @list_item : pointer to nested_list_item_t object
*
* Fetches the id string of the specified list item,
* as set by nested_list_add_item().
*
* Returns: item id if successful, otherwise NULL.
*/
const char *nested_list_item_get_id(nested_list_item_t *list_item)
{
if (!list_item)
return NULL;
return list_item->id;
}
/**
* nested_list_item_get_address:
*
* @list_item : pointer to nested_list_item_t object
* @delim : delimiter to use when concatenating
* individual item ids into a an @address
* string
* @address : a delimited list of item identifiers,
* corresponding to item 'levels'
* @len : length of supplied @address char array
* Fetches a compound @address string corresponding to
* the specified item's 'position' in the top level
* nested list of which it is a member. The resultant
* @address may be used to find the item when calling
* nested_list_get_item() on the top level nested list.
*
* Returns: true if successful, otherwise false.
*/
bool nested_list_item_get_address(nested_list_item_t *list_item,
const char *delim, char *address, size_t len)
{
nested_list_item_t *current_item = list_item;
struct string_list id_list = {0};
bool success = false;
union string_list_elem_attr attr;
size_t i;
if (!list_item ||
string_is_empty(delim) ||
!address ||
(len < 1))
goto end;
address[0] = '\0';
attr.i = 0;
/* If this is an item of the top level
* list, just copy the item id directly */
if (!list_item->parent_item)
{
strlcpy(address, list_item->id, len);
success = true;
goto end;
}
/* ...otherwise we have to combine the ids
* of the item and all of its 'ancestors' */
string_list_initialize(&id_list);
/* Fetch all ids */
do
{
const char *id = current_item->id;
if (string_is_empty(id) ||
!string_list_append(&id_list, id, attr))
goto end;
current_item = current_item->parent_item;
}
while (current_item);
if (id_list.size < 1)
goto end;
/* Build address string */
for (i = id_list.size; i > 0; i--)
{
size_t _len;
const char *id = id_list.elems[i - 1].data;
if (string_is_empty(id))
goto end;
_len = strlcat(address, id, len);
if (i > 1)
strlcpy(address + _len, delim, len - _len);
}
success = true;
end:
string_list_deinitialize(&id_list);
return success;
}
/**
* nested_list_item_get_value:
*
* @list_item : pointer to nested_list_item_t object
*
* Fetches the value (user data) associated with the
* specified list item.
*
* Returns: pointer to user data if set, otherwise
* NULL.
*/
const void *nested_list_item_get_value(nested_list_item_t *list_item)
{
if (!list_item)
return NULL;
return list_item->value;
}
|