This source file includes following definitions.
- char2score
- pcmk_readable_score
- pcmk__add_scores
- score2char
- score2char_stack
1
2
3
4
5
6
7
8
9
10 #include <crm_internal.h>
11
12 #ifndef _GNU_SOURCE
13 # define _GNU_SOURCE
14 #endif
15
16 #include <stdio.h>
17 #include <string.h>
18 #include <sys/types.h>
19
20 int pcmk__score_red = 0;
21 int pcmk__score_green = 0;
22 int pcmk__score_yellow = 0;
23
24
25
26
27
28
29
30
31
32
33
34
35 int
36 char2score(const char *score)
37 {
38 if (score == NULL) {
39 return 0;
40
41 } else if (pcmk_str_is_minus_infinity(score)) {
42 return -PCMK_SCORE_INFINITY;
43
44 } else if (pcmk_str_is_infinity(score)) {
45 return PCMK_SCORE_INFINITY;
46
47 } else if (pcmk__str_eq(score, PCMK_VALUE_RED, pcmk__str_casei)) {
48 return pcmk__score_red;
49
50 } else if (pcmk__str_eq(score, PCMK_VALUE_YELLOW, pcmk__str_casei)) {
51 return pcmk__score_yellow;
52
53 } else if (pcmk__str_eq(score, PCMK_VALUE_GREEN, pcmk__str_casei)) {
54 return pcmk__score_green;
55
56 } else {
57 long long score_ll;
58
59 pcmk__scan_ll(score, &score_ll, 0LL);
60 if (score_ll > PCMK_SCORE_INFINITY) {
61 return PCMK_SCORE_INFINITY;
62
63 } else if (score_ll < -PCMK_SCORE_INFINITY) {
64 return -PCMK_SCORE_INFINITY;
65
66 } else {
67 return (int) score_ll;
68 }
69 }
70 }
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 const char *
86 pcmk_readable_score(int score)
87 {
88
89 static char score_s[sizeof(PCMK_VALUE_MINUS_INFINITY)];
90
91 if (score >= PCMK_SCORE_INFINITY) {
92 strcpy(score_s, PCMK_VALUE_INFINITY);
93
94 } else if (score <= -PCMK_SCORE_INFINITY) {
95 strcpy(score_s, PCMK_VALUE_MINUS_INFINITY);
96
97 } else {
98
99 snprintf(score_s, sizeof(score_s), "%d", score);
100 }
101
102 return score_s;
103 }
104
105
106
107
108
109
110
111
112
113
114
115 int
116 pcmk__add_scores(int score1, int score2)
117 {
118
119
120
121 int result = score1 + score2;
122
123
124 if ((score1 <= -PCMK_SCORE_INFINITY) || (score2 <= -PCMK_SCORE_INFINITY)) {
125 return -PCMK_SCORE_INFINITY;
126 }
127 if ((score1 >= PCMK_SCORE_INFINITY) || (score2 >= PCMK_SCORE_INFINITY)) {
128 return PCMK_SCORE_INFINITY;
129 }
130
131
132 if (result >= PCMK_SCORE_INFINITY) {
133 return PCMK_SCORE_INFINITY;
134 }
135 if (result <= -PCMK_SCORE_INFINITY) {
136 return -PCMK_SCORE_INFINITY;
137 }
138
139 return result;
140 }
141
142
143
144
145 #include <crm/common/scores_compat.h>
146
147 char *
148 score2char(int score)
149 {
150 return pcmk__str_copy(pcmk_readable_score(score));
151 }
152
153 char *
154 score2char_stack(int score, char *buf, size_t len)
155 {
156 CRM_CHECK((buf != NULL) && (len >= sizeof(PCMK_VALUE_MINUS_INFINITY)),
157 return NULL);
158 strcpy(buf, pcmk_readable_score(score));
159 return buf;
160 }
161
162
163