master
c 60 lines 2 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "contexts_alert_statuses.h"
4
5 static struct {
6 const char *name;
7 uint32_t hash;
8 CONTEXTS_ALERT_STATUS value;
9 } contexts_alert_status[] = {
10 {"uninitialized" , 0 , CONTEXT_ALERT_UNINITIALIZED}
11 , {"undefined" , 0 , CONTEXT_ALERT_UNDEFINED}
12 , {"clear" , 0 , CONTEXT_ALERT_CLEAR}
13 , {"raised" , 0 , CONTEXT_ALERT_RAISED}
14 , {"active" , 0 , CONTEXT_ALERT_RAISED}
15 , {"warning" , 0 , CONTEXT_ALERT_WARNING}
16 , {"critical" , 0 , CONTEXT_ALERT_CRITICAL}
17 , {NULL , 0 , 0}
18 };
19
20 CONTEXTS_ALERT_STATUS contexts_alert_status_str_to_id(char *o) {
21 CONTEXTS_ALERT_STATUS ret = 0;
22 char *tok;
23
24 while(o && *o && (tok = strsep_skip_consecutive_separators(&o, ", |"))) {
25 if(!*tok) continue;
26
27 uint32_t hash = simple_hash(tok);
28 int i;
29 for(i = 0; contexts_alert_status[i].name ; i++) {
30 if (unlikely(hash == contexts_alert_status[i].hash && !strcmp(tok, contexts_alert_status[i].name))) {
31 ret |= contexts_alert_status[i].value;
32 break;
33 }
34 }
35 }
36
37 return ret;
38 }
39
40 void contexts_alerts_status_to_buffer_json_array(BUFFER *wb, const char *key,
41 CONTEXTS_ALERT_STATUS options) {
42 buffer_json_member_add_array(wb, key);
43
44 CONTEXTS_ALERT_STATUS used = 0; // to prevent adding duplicates
45 for(int i = 0; contexts_alert_status[i].name ; i++) {
46 if (unlikely((contexts_alert_status[i].value & options) && !(contexts_alert_status[i].value & used))) {
47 const char *name = contexts_alert_status[i].name;
48 used |= contexts_alert_status[i].value;
49
50 buffer_json_add_array_item_string(wb, name);
51 }
52 }
53
54 buffer_json_array_close(wb);
55 }
56
57 void contexts_alert_statuses_init(void) {
58 for(size_t i = 0; contexts_alert_status[i].name ; i++)
59 contexts_alert_status[i].hash = simple_hash(contexts_alert_status[i].name);
60 }