root/lib/common/scores.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. char2score
  2. pcmk_readable_score
  3. pcmk__add_scores
  4. score2char
  5. score2char_stack

   1 /*
   2  * Copyright 2004-2022 the Pacemaker project contributors
   3  *
   4  * The version control history for this file may have further details.
   5  *
   6  * This source code is licensed under the GNU Lesser General Public License
   7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #include <crm_internal.h>
  11 
  12 #ifndef _GNU_SOURCE
  13 #  define _GNU_SOURCE
  14 #endif
  15 
  16 #include <stdio.h>      // snprintf(), NULL
  17 #include <string.h>     // strcpy(), strdup()
  18 #include <sys/types.h>  // size_t
  19 
  20 int pcmk__score_red = 0;
  21 int pcmk__score_green = 0;
  22 int pcmk__score_yellow = 0;
  23 
  24 /*!
  25  * \brief Get the integer value of a score string
  26  *
  27  * Given a string representation of a score, return the integer equivalent.
  28  * This accepts infinity strings as well as red, yellow, and green, and
  29  * bounds the result to +/-INFINITY.
  30  *
  31  * \param[in] score  Score as string
  32  *
  33  * \return Integer value corresponding to \p score
  34  */
  35 int
  36 char2score(const char *score)
     /* [previous][next][first][last][top][bottom][index][help] */
  37 {
  38     if (score == NULL) {
  39         return 0;
  40 
  41     } else if (pcmk_str_is_minus_infinity(score)) {
  42         return -CRM_SCORE_INFINITY;
  43 
  44     } else if (pcmk_str_is_infinity(score)) {
  45         return CRM_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 > CRM_SCORE_INFINITY) {
  61             return CRM_SCORE_INFINITY;
  62 
  63         } else if (score_ll < -CRM_SCORE_INFINITY) {
  64             return -CRM_SCORE_INFINITY;
  65 
  66         } else {
  67             return (int) score_ll;
  68         }
  69     }
  70 }
  71 
  72 /*!
  73  * \brief Return a displayable static string for a score value
  74  *
  75  * Given a score value, return a pointer to a static string representation of
  76  * the score suitable for log messages, output, etc.
  77  *
  78  * \param[in] score  Score to display
  79  *
  80  * \return Pointer to static memory containing string representation of \p score
  81  * \note Subsequent calls to this function will overwrite the returned value, so
  82  *       it should be used only in a local context such as a printf()-style
  83  *       statement.
  84  */
  85 const char *
  86 pcmk_readable_score(int score)
     /* [previous][next][first][last][top][bottom][index][help] */
  87 {
  88     // The longest possible result is "-INFINITY"
  89     static char score_s[sizeof(CRM_MINUS_INFINITY_S)];
  90 
  91     if (score >= CRM_SCORE_INFINITY) {
  92         strcpy(score_s, CRM_INFINITY_S);
  93 
  94     } else if (score <= -CRM_SCORE_INFINITY) {
  95         strcpy(score_s, CRM_MINUS_INFINITY_S);
  96 
  97     } else {
  98         // Range is limited to +/-1000000, so no chance of overflow
  99         snprintf(score_s, sizeof(score_s), "%d", score);
 100     }
 101 
 102     return score_s;
 103 }
 104 
 105 /*!
 106  * \internal
 107  * \brief Add two scores, bounding to +/-INFINITY
 108  *
 109  * \param[in] score1  First score to add
 110  * \param[in] score2  Second score to add
 111  */
 112 int
 113 pcmk__add_scores(int score1, int score2)
     /* [previous][next][first][last][top][bottom][index][help] */
 114 {
 115     int result = score1 + score2;
 116 
 117     // First handle the cases where one or both is infinite
 118 
 119     if (score1 <= -CRM_SCORE_INFINITY) {
 120 
 121         if (score2 <= -CRM_SCORE_INFINITY) {
 122             crm_trace("-INFINITY + -INFINITY = -INFINITY");
 123         } else if (score2 >= CRM_SCORE_INFINITY) {
 124             crm_trace("-INFINITY + +INFINITY = -INFINITY");
 125         } else {
 126             crm_trace("-INFINITY + %d = -INFINITY", score2);
 127         }
 128 
 129         return -CRM_SCORE_INFINITY;
 130 
 131     } else if (score2 <= -CRM_SCORE_INFINITY) {
 132 
 133         if (score1 >= CRM_SCORE_INFINITY) {
 134             crm_trace("+INFINITY + -INFINITY = -INFINITY");
 135         } else {
 136             crm_trace("%d + -INFINITY = -INFINITY", score1);
 137         }
 138 
 139         return -CRM_SCORE_INFINITY;
 140 
 141     } else if (score1 >= CRM_SCORE_INFINITY) {
 142 
 143         if (score2 >= CRM_SCORE_INFINITY) {
 144             crm_trace("+INFINITY + +INFINITY = +INFINITY");
 145         } else {
 146             crm_trace("+INFINITY + %d = +INFINITY", score2);
 147         }
 148 
 149         return CRM_SCORE_INFINITY;
 150 
 151     } else if (score2 >= CRM_SCORE_INFINITY) {
 152         crm_trace("%d + +INFINITY = +INFINITY", score1);
 153         return CRM_SCORE_INFINITY;
 154     }
 155 
 156     /* As long as CRM_SCORE_INFINITY is less than half of the maximum integer,
 157      * we can ignore the possibility of integer overflow
 158      */
 159 
 160     // Bound result to infinity
 161 
 162     if (result >= CRM_SCORE_INFINITY) {
 163         crm_trace("%d + %d = +INFINITY", score1, score2);
 164         return CRM_SCORE_INFINITY;
 165 
 166     } else if (result <= -CRM_SCORE_INFINITY) {
 167         crm_trace("%d + %d = -INFINITY", score1, score2);
 168         return -CRM_SCORE_INFINITY;
 169     }
 170 
 171     crm_trace("%d + %d = %d", score1, score2, result);
 172     return result;
 173 }
 174 
 175 // Deprecated functions kept only for backward API compatibility
 176 // LCOV_EXCL_START
 177 
 178 #include <crm/common/util_compat.h>
 179 
 180 char *
 181 score2char(int score)
     /* [previous][next][first][last][top][bottom][index][help] */
 182 {
 183     char *result = strdup(pcmk_readable_score(score));
 184 
 185     CRM_ASSERT(result != NULL);
 186     return result;
 187 }
 188 
 189 char *
 190 score2char_stack(int score, char *buf, size_t len)
     /* [previous][next][first][last][top][bottom][index][help] */
 191 {
 192     CRM_CHECK((buf != NULL) && (len >= sizeof(CRM_MINUS_INFINITY_S)),
 193               return NULL);
 194     strcpy(buf, pcmk_readable_score(score));
 195     return buf;
 196 }
 197 
 198 // LCOV_EXCL_STOP
 199 // End deprecated API

/* [previous][next][first][last][top][bottom][index][help] */