pacemaker  2.0.5-ba59be712
Scalable High-Availability cluster resource manager
strings_internal.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015-2020 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
29 };
30 
31 int pcmk__scan_double(const char *text, double *result,
32  const char *default_text, char **end_text);
33 int pcmk__guint_from_hash(GHashTable *table, const char *key, guint default_val,
34  guint *result);
35 bool pcmk__starts_with(const char *str, const char *prefix);
36 bool pcmk__ends_with(const char *s, const char *match);
37 bool pcmk__ends_with_ext(const char *s, const char *match);
38 void pcmk__add_separated_word(char **list, size_t *len, const char *word,
39  const char *separator);
40 int pcmk__compress(const char *data, unsigned int length, unsigned int max,
41  char **result, unsigned int *result_len);
42 
43 int pcmk__parse_ll_range(const char *srcstring, long long *start, long long *end);
44 gboolean pcmk__str_in_list(GList *lst, const gchar *s);
45 
46 bool pcmk__strcase_any_of(const char *s, ...) G_GNUC_NULL_TERMINATED;
47 bool pcmk__str_any_of(const char *s, ...) G_GNUC_NULL_TERMINATED;
48 bool pcmk__char_in_any_str(int ch, ...) G_GNUC_NULL_TERMINATED;
49 
50 int pcmk__strcmp(const char *s1, const char *s2, uint32_t flags);
51 
52 static inline bool
53 pcmk__str_eq(const char *s1, const char *s2, uint32_t flags)
54 {
55  return pcmk__strcmp(s1, s2, flags) == 0;
56 }
57 
58 // Like pcmk__add_separated_word() but using a space as separator
59 static inline void
60 pcmk__add_word(char **list, size_t *len, const char *word)
61 {
62  return pcmk__add_separated_word(list, len, word, " ");
63 }
64 
65 /* Correctly displaying singular or plural is complicated; consider "1 node has"
66  * vs. "2 nodes have". A flexible solution is to pluralize entire strings, e.g.
67  *
68  * if (a == 1) {
69  * crm_info("singular message"):
70  * } else {
71  * crm_info("plural message");
72  * }
73  *
74  * though even that's not sufficient for all languages besides English (if we
75  * ever desire to do translations of output and log messages). But the following
76  * convenience macros are "good enough" and more concise for many cases.
77  */
78 
79 /* Example:
80  * crm_info("Found %d %s", nentries,
81  * pcmk__plural_alt(nentries, "entry", "entries"));
82  */
83 #define pcmk__plural_alt(i, s1, s2) (((i) == 1)? (s1) : (s2))
84 
85 // Example: crm_info("Found %d node%s", nnodes, pcmk__plural_s(nnodes));
86 #define pcmk__plural_s(i) pcmk__plural_alt(i, "", "s")
87 
88 static inline int
89 pcmk__str_empty(const char *s)
90 {
91  return (s == NULL) || (s[0] == '\0');
92 }
93 
94 // note this returns const not allocated
95 static inline const char *
96 pcmk__btoa(bool condition)
97 {
98  return condition? "true" : "false";
99 }
100 
101 #endif /* PCMK__STRINGS_INTERNAL__H */
void pcmk__add_separated_word(char **list, size_t *len, const char *word, const char *separator)
Definition: strings.c:617
bool pcmk__char_in_any_str(int ch,...) G_GNUC_NULL_TERMINATED
Definition: strings.c:889
bool pcmk__strcase_any_of(const char *s,...) G_GNUC_NULL_TERMINATED
Definition: strings.c:842
gboolean pcmk__str_in_list(GList *lst, const gchar *s)
Definition: strings.c:794
bool pcmk__ends_with(const char *s, const char *match)
Definition: strings.c:504
int pcmk__guint_from_hash(GHashTable *table, const char *key, guint default_val, guint *result)
Definition: strings.c:290
int pcmk__compress(const char *data, unsigned int length, unsigned int max, char **result, unsigned int *result_len)
Definition: strings.c:663
bool pcmk__str_any_of(const char *s,...) G_GNUC_NULL_TERMINATED
Definition: strings.c:866
int pcmk__scan_double(const char *text, double *result, const char *default_text, char **end_text)
Definition: strings.c:178
pcmk__str_flags
int pcmk__strcmp(const char *s1, const char *s2, uint32_t flags)
Definition: strings.c:1009
char data[0]
Definition: internal.h:90
int pcmk__parse_ll_range(const char *srcstring, long long *start, long long *end)
Definition: strings.c:727
bool pcmk__starts_with(const char *str, const char *prefix)
Check whether a string starts with a certain sequence.
Definition: strings.c:452
bool pcmk__ends_with_ext(const char *s, const char *match)
Definition: strings.c:531
uint64_t flags
Definition: remote.c:149