ListItem.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. htop - ListItem.c
  3. (C) 2004-2011 Hisham H. Muhammad
  4. Released under the GNU GPLv2+, see the COPYING file
  5. in the source distribution for its full text.
  6. */
  7. #include "config.h" // IWYU pragma: keep
  8. #include "ListItem.h"
  9. #include <assert.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "CRT.h"
  13. #include "RichString.h"
  14. #include "XUtils.h"
  15. void ListItem_delete(Object* cast) {
  16. ListItem* this = (ListItem*)cast;
  17. free(this->value);
  18. free(this);
  19. }
  20. void ListItem_display(const Object* cast, RichString* out) {
  21. const ListItem* const this = (const ListItem*)cast;
  22. assert (this != NULL);
  23. if (this->moving) {
  24. RichString_writeWide(out, CRT_colors[DEFAULT_COLOR],
  25. #ifdef HAVE_LIBNCURSESW
  26. CRT_utf8 ? "↕ " :
  27. #endif
  28. "+ ");
  29. }
  30. RichString_appendWide(out, CRT_colors[DEFAULT_COLOR], this->value);
  31. }
  32. void ListItem_init(ListItem* this, const char* value, int key) {
  33. this->value = xStrdup(value);
  34. this->key = key;
  35. this->moving = false;
  36. }
  37. ListItem* ListItem_new(const char* value, int key) {
  38. ListItem* this = AllocThis(ListItem);
  39. ListItem_init(this, value, key);
  40. return this;
  41. }
  42. void ListItem_append(ListItem* this, const char* text) {
  43. size_t oldLen = strlen(this->value);
  44. size_t textLen = strlen(text);
  45. size_t newLen = oldLen + textLen;
  46. this->value = xRealloc(this->value, newLen + 1);
  47. memcpy(this->value + oldLen, text, textLen);
  48. this->value[newLen] = '\0';
  49. }
  50. int ListItem_compare(const void* cast1, const void* cast2) {
  51. const ListItem* obj1 = (const ListItem*) cast1;
  52. const ListItem* obj2 = (const ListItem*) cast2;
  53. return strcmp(obj1->value, obj2->value);
  54. }
  55. const ObjectClass ListItem_class = {
  56. .display = ListItem_display,
  57. .delete = ListItem_delete,
  58. .compare = ListItem_compare
  59. };