| Sohaib Mohamed | 53bdcab | 2023-08-22 16:46:59 +1000 | [diff] [blame] | 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 | |
| Daniel Lange | 179aeb0 | 2023-11-29 17:44:20 +0100 | [diff] [blame] | 9 | #include "config.h" // IWYU pragma: keep |
| 10 | |
| Sohaib Mohamed | 53bdcab | 2023-08-22 16:46:59 +1000 | [diff] [blame] | 11 | #include "DynamicScreen.h" |
| 12 | |
| 13 | #include <stdbool.h> |
| 14 | #include <stddef.h> |
| Benny Baumann | e56089e | 2023-11-28 15:15:03 +0100 | [diff] [blame] | 15 | #include <stdlib.h> |
| Sohaib Mohamed | 53bdcab | 2023-08-22 16:46:59 +1000 | [diff] [blame] | 16 | |
| 17 | #include "Hashtable.h" |
| 18 | #include "Platform.h" |
| 19 | #include "XUtils.h" |
| 20 | |
| 21 | |
| 22 | Hashtable* DynamicScreens_new(void) { |
| 23 | return Platform_dynamicScreens(); |
| 24 | } |
| 25 | |
| 26 | void DynamicScreens_delete(Hashtable* screens) { |
| 27 | if (screens) { |
| 28 | Platform_dynamicScreensDone(screens); |
| 29 | Hashtable_delete(screens); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | void DynamicScreen_done(DynamicScreen* this) { |
| 34 | free(this->caption); |
| 35 | free(this->fields); |
| 36 | free(this->heading); |
| 37 | free(this->sortKey); |
| 38 | free(this->columnKeys); |
| 39 | } |
| 40 | |
| 41 | typedef struct { |
| 42 | ht_key_t key; |
| 43 | const char* name; |
| 44 | bool found; |
| 45 | } DynamicIterator; |
| 46 | |
| 47 | static void DynamicScreen_compare(ht_key_t key, void* value, void* data) { |
| 48 | const DynamicScreen* screen = (const DynamicScreen*)value; |
| 49 | DynamicIterator* iter = (DynamicIterator*)data; |
| 50 | if (String_eq(iter->name, screen->name)) { |
| 51 | iter->found = true; |
| 52 | iter->key = key; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | bool DynamicScreen_search(Hashtable* screens, const char* name, ht_key_t* key) { |
| 57 | DynamicIterator iter = { .key = 0, .name = name, .found = false }; |
| 58 | if (screens) |
| 59 | Hashtable_foreach(screens, DynamicScreen_compare, &iter); |
| 60 | if (key) |
| 61 | *key = iter.key; |
| 62 | return iter.found; |
| 63 | } |
| 64 | |
| 65 | const char* DynamicScreen_lookup(Hashtable* screens, ht_key_t key) { |
| 66 | const DynamicScreen* screen = Hashtable_get(screens, key); |
| 67 | return screen ? screen->name : NULL; |
| 68 | } |