forked from htop-dev/htop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynamicScreen.c
68 lines (56 loc) · 1.59 KB
/
DynamicScreen.c
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
/*
htop - DynamicScreen.c
(C) 2022 Sohaib Mohammed
(C) 2022-2023 htop dev team
Released under the GNU GPLv2+, see the COPYING file
in the source distribution for its full text.
*/
#include "config.h" // IWYU pragma: keep
#include "DynamicScreen.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include "Hashtable.h"
#include "Platform.h"
#include "XUtils.h"
Hashtable* DynamicScreens_new(void) {
return Platform_dynamicScreens();
}
void DynamicScreens_delete(Hashtable* screens) {
if (screens) {
Platform_dynamicScreensDone(screens);
Hashtable_delete(screens);
}
}
void DynamicScreen_done(DynamicScreen* this) {
free(this->caption);
free(this->fields);
free(this->heading);
free(this->sortKey);
free(this->columnKeys);
}
typedef struct {
ht_key_t key;
const char* name;
bool found;
} DynamicIterator;
static void DynamicScreen_compare(ht_key_t key, void* value, void* data) {
const DynamicScreen* screen = (const DynamicScreen*)value;
DynamicIterator* iter = (DynamicIterator*)data;
if (String_eq(iter->name, screen->name)) {
iter->found = true;
iter->key = key;
}
}
bool DynamicScreen_search(Hashtable* screens, const char* name, ht_key_t* key) {
DynamicIterator iter = { .key = 0, .name = name, .found = false };
if (screens)
Hashtable_foreach(screens, DynamicScreen_compare, &iter);
if (key)
*key = iter.key;
return iter.found;
}
const char* DynamicScreen_lookup(Hashtable* screens, ht_key_t key) {
const DynamicScreen* screen = Hashtable_get(screens, key);
return screen ? screen->name : NULL;
}