root/include/crm/common/strings_internal.h

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

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. pcmk__intkey_table
  2. pcmk__intkey_table_insert
  3. pcmk__intkey_table_lookup
  4. pcmk__intkey_table_remove
  5. pcmk__strcase_any_of
  6. pcmk__add_word
  7. pcmk__str_empty
  8. pcmk__itoa
  9. pcmk__ftoa
  10. pcmk__ttoa
  11. pcmk__btoa

   1 /*
   2  * Copyright 2015-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 #ifndef PCMK__STRINGS_INTERNAL__H
  11 #define PCMK__STRINGS_INTERNAL__H
  12 
  13 #include <stdbool.h>            // bool
  14 
  15 #include <glib.h>               // guint, GList, GHashTable
  16 
  17 /* internal constants for generic string functions (from strings.c) */
  18 
  19 #define PCMK__PARSE_INT_DEFAULT -1
  20 #define PCMK__PARSE_DBL_DEFAULT -1.0
  21 
  22 /* internal generic string functions (from strings.c) */
  23 
  24 enum pcmk__str_flags {
  25     pcmk__str_none          = 0,
  26     pcmk__str_casei         = 1 << 0,
  27     pcmk__str_null_matches  = 1 << 1,
  28     pcmk__str_regex         = 1 << 2,
  29     pcmk__str_star_matches  = 1 << 3,
  30 };
  31 
  32 int pcmk__scan_double(const char *text, double *result,
  33                       const char *default_text, char **end_text);
  34 int pcmk__guint_from_hash(GHashTable *table, const char *key, guint default_val,
  35                           guint *result);
  36 bool pcmk__starts_with(const char *str, const char *prefix);
  37 bool pcmk__ends_with(const char *s, const char *match);
  38 bool pcmk__ends_with_ext(const char *s, const char *match);
  39 char *pcmk__trim(char *str);
  40 void pcmk__add_separated_word(char **list, size_t *len, const char *word,
  41                               const char *separator);
  42 int pcmk__compress(const char *data, unsigned int length, unsigned int max,
  43                    char **result, unsigned int *result_len);
  44 
  45 int pcmk__scan_ll(const char *text, long long *result, long long default_value);
  46 int pcmk__scan_min_int(const char *text, int *result, int minimum);
  47 int pcmk__scan_port(const char *text, int *port);
  48 int pcmk__parse_ll_range(const char *srcstring, long long *start, long long *end);
  49 
  50 GHashTable *pcmk__strkey_table(GDestroyNotify key_destroy_func,
  51                                GDestroyNotify value_destroy_func);
  52 GHashTable *pcmk__strikey_table(GDestroyNotify key_destroy_func,
  53                                 GDestroyNotify value_destroy_func);
  54 GHashTable *pcmk__str_table_dup(GHashTable *old_table);
  55 
  56 /*!
  57  * \internal
  58  * \brief Create a hash table with integer keys
  59  *
  60  * \param[in] value_destroy_func  Function to free a value
  61  *
  62  * \return Newly allocated hash table
  63  * \note It is the caller's responsibility to free the result, using
  64  *       g_hash_table_destroy().
  65  */
  66 static inline GHashTable *
  67 pcmk__intkey_table(GDestroyNotify value_destroy_func)
     /* [previous][next][first][last][top][bottom][index][help] */
  68 {
  69     return g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
  70                                  value_destroy_func);
  71 }
  72 
  73 /*!
  74  * \internal
  75  * \brief Insert a value into a hash table with integer keys
  76  *
  77  * \param[in,out] hash_table  Table to insert into
  78  * \param[in]     key         Integer key to insert
  79  * \param[in]     value       Value to insert
  80  *
  81  * \return Whether the key/value was already in the table
  82  * \note This has the same semantics as g_hash_table_insert(). If the key
  83  *       already exists in the table, the old value is freed and replaced.
  84  */
  85 static inline gboolean
  86 pcmk__intkey_table_insert(GHashTable *hash_table, int key, gpointer value)
     /* [previous][next][first][last][top][bottom][index][help] */
  87 {
  88     return g_hash_table_insert(hash_table, GINT_TO_POINTER(key), value);
  89 }
  90 
  91 /*!
  92  * \internal
  93  * \brief Look up a value in a hash table with integer keys
  94  *
  95  * \param[in] hash_table  Table to check
  96  * \param[in] key         Integer key to look for
  97  *
  98  * \return Value in table for \key (or NULL if not found)
  99  */
 100 static inline gpointer
 101 pcmk__intkey_table_lookup(GHashTable *hash_table, int key)
     /* [previous][next][first][last][top][bottom][index][help] */
 102 {
 103     return g_hash_table_lookup(hash_table, GINT_TO_POINTER(key));
 104 }
 105 
 106 /*!
 107  * \internal
 108  * \brief Remove a key/value from a hash table with integer keys
 109  *
 110  * \param[in] hash_table  Table to modify
 111  * \param[in] key         Integer key of entry to remove
 112  *
 113  * \return Whether \p key was found and removed from \p hash_table
 114  */
 115 static inline gboolean
 116 pcmk__intkey_table_remove(GHashTable *hash_table, int key)
     /* [previous][next][first][last][top][bottom][index][help] */
 117 {
 118     return g_hash_table_remove(hash_table, GINT_TO_POINTER(key));
 119 }
 120 
 121 gboolean pcmk__str_in_list(const gchar *s, GList *lst, uint32_t flags);
 122 
 123 bool pcmk__strcase_any_of(const char *s, ...) G_GNUC_NULL_TERMINATED;
     /* [previous][next][first][last][top][bottom][index][help] */
 124 bool pcmk__str_any_of(const char *s, ...) G_GNUC_NULL_TERMINATED;
 125 bool pcmk__char_in_any_str(int ch, ...) G_GNUC_NULL_TERMINATED;
 126 
 127 int pcmk__strcmp(const char *s1, const char *s2, uint32_t flags);
 128 int pcmk__numeric_strcasecmp(const char *s1, const char *s2);
 129 void pcmk__str_update(char **str, const char *value);
 130 
 131 static inline bool
 132 pcmk__str_eq(const char *s1, const char *s2, uint32_t flags)
 133 {
 134     return pcmk__strcmp(s1, s2, flags) == 0;
 135 }
 136 
 137 // Like pcmk__add_separated_word() but using a space as separator
 138 static inline void
 139 pcmk__add_word(char **list, size_t *len, const char *word)
     /* [previous][next][first][last][top][bottom][index][help] */
 140 {
 141     return pcmk__add_separated_word(list, len, word, " ");
 142 }
 143 
 144 /* Correctly displaying singular or plural is complicated; consider "1 node has"
 145  * vs. "2 nodes have". A flexible solution is to pluralize entire strings, e.g.
 146  *
 147  * if (a == 1) {
 148  *     crm_info("singular message"):
 149  * } else {
 150  *     crm_info("plural message");
 151  * }
 152  *
 153  * though even that's not sufficient for all languages besides English (if we
 154  * ever desire to do translations of output and log messages). But the following
 155  * convenience macros are "good enough" and more concise for many cases.
 156  */
 157 
 158 /* Example:
 159  * crm_info("Found %d %s", nentries,
 160  *          pcmk__plural_alt(nentries, "entry", "entries"));
 161  */
 162 #define pcmk__plural_alt(i, s1, s2) (((i) == 1)? (s1) : (s2))
 163 
 164 // Example: crm_info("Found %d node%s", nnodes, pcmk__plural_s(nnodes));
 165 #define pcmk__plural_s(i) pcmk__plural_alt(i, "", "s")
 166 
 167 static inline int
 168 pcmk__str_empty(const char *s)
     /* [previous][next][first][last][top][bottom][index][help] */
 169 {
 170     return (s == NULL) || (s[0] == '\0');
 171 }
 172 
 173 static inline char *
 174 pcmk__itoa(int an_int)
     /* [previous][next][first][last][top][bottom][index][help] */
 175 {
 176     return crm_strdup_printf("%d", an_int);
 177 }
 178 
 179 static inline char *
 180 pcmk__ftoa(double a_float)
     /* [previous][next][first][last][top][bottom][index][help] */
 181 {
 182     return crm_strdup_printf("%f", a_float);
 183 }
 184 
 185 static inline char *
 186 pcmk__ttoa(time_t epoch_time)
     /* [previous][next][first][last][top][bottom][index][help] */
 187 {
 188     return crm_strdup_printf("%lld", (long long) epoch_time);
 189 }
 190 
 191 // note this returns const not allocated
 192 static inline const char *
 193 pcmk__btoa(bool condition)
     /* [previous][next][first][last][top][bottom][index][help] */
 194 {
 195     return condition? "true" : "false";
 196 }
 197 
 198 #endif /* PCMK__STRINGS_INTERNAL__H */

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