pacemaker  2.1.7-0f7f88312f
Scalable High-Availability cluster resource manager
strings_internal.h
Go to the documentation of this file.
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 
26  pcmk__str_casei = 1 << 0,
28  pcmk__str_regex = 1 << 2,
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(GString **list, size_t init_size,
41  const char *word, 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 
65 static inline const char *
66 pcmk__s(const char *s, const char *default_value)
67 {
68  return (s == NULL)? default_value : s;
69 }
70 
81 static inline GHashTable *
82 pcmk__intkey_table(GDestroyNotify value_destroy_func)
83 {
84  return g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
85  value_destroy_func);
86 }
87 
100 static inline gboolean
101 pcmk__intkey_table_insert(GHashTable *hash_table, int key, gpointer value)
102 {
103  return g_hash_table_insert(hash_table, GINT_TO_POINTER(key), value);
104 }
105 
115 static inline gpointer
116 pcmk__intkey_table_lookup(GHashTable *hash_table, int key)
117 {
118  return g_hash_table_lookup(hash_table, GINT_TO_POINTER(key));
119 }
120 
130 static inline gboolean
131 pcmk__intkey_table_remove(GHashTable *hash_table, int key)
132 {
133  return g_hash_table_remove(hash_table, GINT_TO_POINTER(key));
134 }
135 
136 gboolean pcmk__str_in_list(const gchar *s, const GList *lst, uint32_t flags);
137 
138 bool pcmk__strcase_any_of(const char *s, ...) G_GNUC_NULL_TERMINATED;
139 bool pcmk__str_any_of(const char *s, ...) G_GNUC_NULL_TERMINATED;
140 bool pcmk__char_in_any_str(int ch, ...) G_GNUC_NULL_TERMINATED;
141 
142 int pcmk__strcmp(const char *s1, const char *s2, uint32_t flags);
143 int pcmk__numeric_strcasecmp(const char *s1, const char *s2);
144 void pcmk__str_update(char **str, const char *value);
145 void pcmk__g_strcat(GString *buffer, ...) G_GNUC_NULL_TERMINATED;
146 
147 static inline bool
148 pcmk__str_eq(const char *s1, const char *s2, uint32_t flags)
149 {
150  return pcmk__strcmp(s1, s2, flags) == 0;
151 }
152 
153 // Like pcmk__add_separated_word() but using a space as separator
154 static inline void
155 pcmk__add_word(GString **list, size_t init_size, const char *word)
156 {
157  return pcmk__add_separated_word(list, init_size, word, " ");
158 }
159 
160 /* Correctly displaying singular or plural is complicated; consider "1 node has"
161  * vs. "2 nodes have". A flexible solution is to pluralize entire strings, e.g.
162  *
163  * if (a == 1) {
164  * crm_info("singular message"):
165  * } else {
166  * crm_info("plural message");
167  * }
168  *
169  * though even that's not sufficient for all languages besides English (if we
170  * ever desire to do translations of output and log messages). But the following
171  * convenience macros are "good enough" and more concise for many cases.
172  */
173 
174 /* Example:
175  * crm_info("Found %d %s", nentries,
176  * pcmk__plural_alt(nentries, "entry", "entries"));
177  */
178 #define pcmk__plural_alt(i, s1, s2) (((i) == 1)? (s1) : (s2))
179 
180 // Example: crm_info("Found %d node%s", nnodes, pcmk__plural_s(nnodes));
181 #define pcmk__plural_s(i) pcmk__plural_alt(i, "", "s")
182 
183 static inline int
184 pcmk__str_empty(const char *s)
185 {
186  return (s == NULL) || (s[0] == '\0');
187 }
188 
189 static inline char *
190 pcmk__itoa(int an_int)
191 {
192  return crm_strdup_printf("%d", an_int);
193 }
194 
195 static inline char *
196 pcmk__ftoa(double a_float)
197 {
198  return crm_strdup_printf("%f", a_float);
199 }
200 
201 static inline char *
202 pcmk__ttoa(time_t epoch_time)
203 {
204  return crm_strdup_printf("%lld", (long long) epoch_time);
205 }
206 
207 // note this returns const not allocated
208 static inline const char *
209 pcmk__btoa(bool condition)
210 {
211  return condition? "true" : "false";
212 }
213 
214 #endif /* PCMK__STRINGS_INTERNAL__H */
char data[0]
Definition: cpg.c:55
bool pcmk__char_in_any_str(int ch,...) G_GNUC_NULL_TERMINATED
Definition: strings.c:980
int pcmk__scan_min_int(const char *text, int *result, int minimum)
Definition: strings.c:127
bool pcmk__strcase_any_of(const char *s,...) G_GNUC_NULL_TERMINATED
Definition: strings.c:933
void pcmk__add_separated_word(GString **list, size_t init_size, const char *word, const char *separator)
Definition: strings.c:700
bool pcmk__ends_with(const char *s, const char *match)
Definition: strings.c:533
int pcmk__scan_ll(const char *text, long long *result, long long default_value)
Definition: strings.c:97
int pcmk__scan_port(const char *text, int *port)
Definition: strings.c:161
int pcmk__guint_from_hash(GHashTable *table, const char *key, guint default_val, guint *result)
Definition: strings.c:311
void pcmk__g_strcat(GString *buffer,...) G_GNUC_NULL_TERMINATED
Definition: strings.c:1217
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1
void pcmk__str_update(char **str, const char *value)
Definition: strings.c:1193
int pcmk__compress(const char *data, unsigned int length, unsigned int max, char **result, unsigned int *result_len)
Definition: strings.c:743
bool pcmk__str_any_of(const char *s,...) G_GNUC_NULL_TERMINATED
Definition: strings.c:957
int pcmk__scan_double(const char *text, double *result, const char *default_text, char **end_text)
Definition: strings.c:199
GHashTable * pcmk__strkey_table(GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func)
Definition: strings.c:608
pcmk__action_result_t result
Definition: pcmk_fence.c:35
GHashTable * pcmk__str_table_dup(GHashTable *old_table)
Definition: strings.c:672
pcmk__str_flags
int pcmk__strcmp(const char *s1, const char *s2, uint32_t flags)
Definition: strings.c:1108
int pcmk__numeric_strcasecmp(const char *s1, const char *s2)
Definition: strings.c:1023
int pcmk__parse_ll_range(const char *srcstring, long long *start, long long *end)
Definition: strings.c:810
bool pcmk__starts_with(const char *str, const char *prefix)
Check whether a string starts with a certain sequence.
Definition: strings.c:481
char * pcmk__trim(char *str)
Definition: strings.c:453
bool pcmk__ends_with_ext(const char *s, const char *match)
Definition: strings.c:560
uint64_t flags
Definition: remote.c:215
GHashTable * pcmk__strikey_table(GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func)
Definition: strings.c:646
gboolean pcmk__str_in_list(const gchar *s, const GList *lst, uint32_t flags)
Definition: strings.c:888