DynamicScreen.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. htop - DynamicScreen.c
  3. (C) 2022 Sohaib Mohammed
  4. (C) 2022-2023 htop dev team
  5. Released under the GNU GPLv2+, see the COPYING file
  6. in the source distribution for its full text.
  7. */
  8. #include "config.h" // IWYU pragma: keep
  9. #include "DynamicScreen.h"
  10. #include <stdbool.h>
  11. #include <stddef.h>
  12. #include <stdlib.h>
  13. #include "Hashtable.h"
  14. #include "Platform.h"
  15. #include "XUtils.h"
  16. Hashtable* DynamicScreens_new(void) {
  17. return Platform_dynamicScreens();
  18. }
  19. void DynamicScreens_delete(Hashtable* screens) {
  20. if (screens) {
  21. Platform_dynamicScreensDone(screens);
  22. Hashtable_delete(screens);
  23. }
  24. }
  25. void DynamicScreen_done(DynamicScreen* this) {
  26. free(this->caption);
  27. free(this->fields);
  28. free(this->heading);
  29. free(this->sortKey);
  30. free(this->columnKeys);
  31. }
  32. typedef struct {
  33. ht_key_t key;
  34. const char* name;
  35. bool found;
  36. } DynamicIterator;
  37. static void DynamicScreen_compare(ht_key_t key, void* value, void* data) {
  38. const DynamicScreen* screen = (const DynamicScreen*)value;
  39. DynamicIterator* iter = (DynamicIterator*)data;
  40. if (String_eq(iter->name, screen->name)) {
  41. iter->found = true;
  42. iter->key = key;
  43. }
  44. }
  45. bool DynamicScreen_search(Hashtable* screens, const char* name, ht_key_t* key) {
  46. DynamicIterator iter = { .key = 0, .name = name, .found = false };
  47. if (screens)
  48. Hashtable_foreach(screens, DynamicScreen_compare, &iter);
  49. if (key)
  50. *key = iter.key;
  51. return iter.found;
  52. }
  53. const char* DynamicScreen_lookup(Hashtable* screens, ht_key_t key) {
  54. const DynamicScreen* screen = Hashtable_get(screens, key);
  55. return screen ? screen->name : NULL;
  56. }