pacemaker 3.0.1-16e74fc4da
Scalable High-Availability cluster resource manager
Loading...
Searching...
No Matches
strings_internal.h
Go to the documentation of this file.
1/*
2 * Copyright 2015-2024 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__CRM_COMMON_STRINGS_INTERNAL__H
11#define PCMK__CRM_COMMON_STRINGS_INTERNAL__H
12
13#include <stdbool.h> // bool
14#include <stdint.h> // uint32_t, etc.
15
16#include <glib.h> // guint, GList, GHashTable
17
18#include <crm/common/options.h> // PCMK_VALUE_TRUE, PCMK_VALUE_FALSE
19#include <crm/common/strings.h> // crm_strdup_printf()
20
21#ifdef __cplusplus
22extern "C" {
23#endif
24
25/* internal constants for generic string functions (from strings.c) */
26
27#define PCMK__PARSE_INT_DEFAULT -1
28#define PCMK__PARSE_DBL_DEFAULT -1.0
29
30/* internal generic string functions (from strings.c) */
31
39
40int pcmk__scan_double(const char *text, double *result,
41 const char *default_text, char **end_text);
42int pcmk__guint_from_hash(GHashTable *table, const char *key, guint default_val,
43 guint *result);
44bool pcmk__starts_with(const char *str, const char *prefix);
45bool pcmk__ends_with(const char *s, const char *match);
46bool pcmk__ends_with_ext(const char *s, const char *match);
47char *pcmk__trim(char *str);
48void pcmk__add_separated_word(GString **list, size_t init_size,
49 const char *word, const char *separator);
50int pcmk__compress(const char *data, unsigned int length, unsigned int max,
51 char **result, unsigned int *result_len);
52
53int pcmk__scan_ll(const char *text, long long *result, long long default_value);
54int pcmk__scan_min_int(const char *text, int *result, int minimum);
55int pcmk__scan_port(const char *text, int *port);
56int pcmk__parse_ll_range(const char *srcstring, long long *start, long long *end);
57
58GHashTable *pcmk__strkey_table(GDestroyNotify key_destroy_func,
59 GDestroyNotify value_destroy_func);
60GHashTable *pcmk__strikey_table(GDestroyNotify key_destroy_func,
61 GDestroyNotify value_destroy_func);
62GHashTable *pcmk__str_table_dup(GHashTable *old_table);
63void pcmk__insert_dup(GHashTable *table, const char *name, const char *value);
64
74static inline const char *
75pcmk__s(const char *s, const char *default_value)
76{
77 return (s == NULL)? default_value : s;
78}
79
90static inline GHashTable *
91pcmk__intkey_table(GDestroyNotify value_destroy_func)
92{
93 return g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
94 value_destroy_func);
95}
96
109static inline gboolean
110pcmk__intkey_table_insert(GHashTable *hash_table, int key, gpointer value)
111{
112 return g_hash_table_insert(hash_table, GINT_TO_POINTER(key), value);
113}
114
124static inline gpointer
125pcmk__intkey_table_lookup(GHashTable *hash_table, int key)
126{
127 return g_hash_table_lookup(hash_table, GINT_TO_POINTER(key));
128}
129
139static inline gboolean
140pcmk__intkey_table_remove(GHashTable *hash_table, int key)
141{
142 return g_hash_table_remove(hash_table, GINT_TO_POINTER(key));
143}
144
145gboolean pcmk__str_in_list(const gchar *s, const GList *lst, uint32_t flags);
146
147bool pcmk__strcase_any_of(const char *s, ...) G_GNUC_NULL_TERMINATED;
148bool pcmk__str_any_of(const char *s, ...) G_GNUC_NULL_TERMINATED;
149bool pcmk__char_in_any_str(int ch, ...) G_GNUC_NULL_TERMINATED;
150
151int pcmk__strcmp(const char *s1, const char *s2, uint32_t flags);
152int pcmk__numeric_strcasecmp(const char *s1, const char *s2);
153
154char *pcmk__str_copy_as(const char *file, const char *function, uint32_t line,
155 const char *str);
156
167#define pcmk__str_copy(str) pcmk__str_copy_as(__FILE__, __func__, __LINE__, str)
168
169void pcmk__str_update(char **str, const char *value);
170
171void pcmk__g_strcat(GString *buffer, ...) G_GNUC_NULL_TERMINATED;
172
173static inline bool
174pcmk__str_eq(const char *s1, const char *s2, uint32_t flags)
175{
176 return pcmk__strcmp(s1, s2, flags) == 0;
177}
178
179// Like pcmk__add_separated_word() but using a space as separator
180static inline void
181pcmk__add_word(GString **list, size_t init_size, const char *word)
182{
183 return pcmk__add_separated_word(list, init_size, word, " ");
184}
185
186/* Correctly displaying singular or plural is complicated; consider "1 node has"
187 * vs. "2 nodes have". A flexible solution is to pluralize entire strings, e.g.
188 *
189 * if (a == 1) {
190 * crm_info("singular message"):
191 * } else {
192 * crm_info("plural message");
193 * }
194 *
195 * though even that's not sufficient for all languages besides English (if we
196 * ever desire to do translations of output and log messages). But the following
197 * convenience macros are "good enough" and more concise for many cases.
198 */
199
200/* Example:
201 * crm_info("Found %d %s", nentries,
202 * pcmk__plural_alt(nentries, "entry", "entries"));
203 */
204#define pcmk__plural_alt(i, s1, s2) (((i) == 1)? (s1) : (s2))
205
206// Example: crm_info("Found %d node%s", nnodes, pcmk__plural_s(nnodes));
207#define pcmk__plural_s(i) pcmk__plural_alt(i, "", "s")
208
209static inline int
210pcmk__str_empty(const char *s)
211{
212 return (s == NULL) || (s[0] == '\0');
213}
214
215static inline char *
216pcmk__itoa(int an_int)
217{
218 return crm_strdup_printf("%d", an_int);
219}
220
221static inline char *
222pcmk__ftoa(double a_float)
223{
224 return crm_strdup_printf("%f", a_float);
225}
226
227static inline char *
228pcmk__ttoa(time_t epoch_time)
229{
230 return crm_strdup_printf("%lld", (long long) epoch_time);
231}
232
233// note this returns const not allocated
234static inline const char *
235pcmk__btoa(bool condition)
236{
237 return condition? PCMK_VALUE_TRUE : PCMK_VALUE_FALSE;
238}
239
240#ifdef __cplusplus
241}
242#endif
243
244#endif // PCMK__CRM_COMMON_STRINGS_INTERNAL__H
const char * name
Definition cib.c:26
uint64_t flags
Definition remote.c:3
char data[0]
Definition cpg.c:10
API related to options.
#define PCMK_VALUE_TRUE
Definition options.h:219
#define PCMK_VALUE_FALSE
Definition options.h:154
pcmk__action_result_t result
Definition pcmk_fence.c:37
API for strings.
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1
bool pcmk__char_in_any_str(int ch,...) G_GNUC_NULL_TERMINATED
GHashTable * pcmk__str_table_dup(GHashTable *old_table)
Definition strings.c:768
void pcmk__insert_dup(GHashTable *table, const char *name, const char *value)
Definition strings.c:703
int pcmk__numeric_strcasecmp(const char *s1, const char *s2)
Definition strings.c:1081
int pcmk__scan_min_int(const char *text, int *result, int minimum)
Definition strings.c:116
gboolean pcmk__str_in_list(const gchar *s, const GList *lst, uint32_t flags)
Definition strings.c:984
int pcmk__scan_port(const char *text, int *port)
Definition strings.c:150
GHashTable * pcmk__strkey_table(GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func)
Definition strings.c:685
char * pcmk__str_copy_as(const char *file, const char *function, uint32_t line, const char *str)
Definition strings.c:1251
bool pcmk__ends_with_ext(const char *s, const char *match)
Definition strings.c:637
char * pcmk__trim(char *str)
Definition strings.c:530
int pcmk__scan_double(const char *text, double *result, const char *default_text, char **end_text)
Definition strings.c:191
bool pcmk__starts_with(const char *str, const char *prefix)
Check whether a string starts with a certain sequence.
Definition strings.c:558
int pcmk__compress(const char *data, unsigned int length, unsigned int max, char **result, unsigned int *result_len)
Definition strings.c:839
int pcmk__scan_ll(const char *text, long long *result, long long default_value)
Definition strings.c:92
void pcmk__str_update(char **str, const char *value)
Definition strings.c:1280
bool pcmk__strcase_any_of(const char *s,...) G_GNUC_NULL_TERMINATED
Definition strings.c:1029
pcmk__str_flags
@ pcmk__str_regex
@ pcmk__str_none
@ pcmk__str_null_matches
@ pcmk__str_star_matches
@ pcmk__str_casei
bool pcmk__ends_with(const char *s, const char *match)
Definition strings.c:610
int pcmk__guint_from_hash(GHashTable *table, const char *key, guint default_val, guint *result)
Definition strings.c:303
bool pcmk__str_any_of(const char *s,...) G_GNUC_NULL_TERMINATED
Definition strings.c:1053
int pcmk__strcmp(const char *s1, const char *s2, uint32_t flags)
Definition strings.c:1166
int pcmk__parse_ll_range(const char *srcstring, long long *start, long long *end)
Definition strings.c:905
void pcmk__g_strcat(GString *buffer,...) G_GNUC_NULL_TERMINATED
Definition strings.c:1299
void pcmk__add_separated_word(GString **list, size_t init_size, const char *word, const char *separator)
Definition strings.c:796
GHashTable * pcmk__strikey_table(GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func)
Definition strings.c:741