25 #include <sys/types.h> 31 snprintf(buffer, len,
"%d", an_int);
51 scan_ll(
const char *text,
long long *result,
char **end_text)
54 char *local_end_text = NULL;
60 local_result = (
long long) strtol(text, &local_end_text, 10);
62 local_result = strtoll(text, &local_end_text, 10);
64 if (errno == ERANGE) {
66 crm_warn(
"Integer parsed from %s was clipped to %lld",
69 }
else if (errno != 0) {
72 crm_warn(
"Could not parse integer from %s (using %d instead): %s",
75 }
else if (local_end_text == text) {
78 crm_warn(
"Could not parse integer from %s (using %d instead): " 82 if ((end_text == NULL) && !pcmk__str_empty(local_end_text)) {
83 crm_warn(
"Characters left over after parsing '%s': '%s'",
84 text, local_end_text);
88 if (end_text != NULL) {
89 *end_text = local_end_text;
92 *result = local_result;
114 crm_err(
"No default conversion value supplied");
119 scan_ll(text, &result, NULL);
138 if (result < INT_MIN) {
140 if (errno != ERANGE) {
141 crm_err(
"Conversion of %s was clipped: %lld", text, result);
146 }
else if (result > INT_MAX) {
148 if (errno != ERANGE) {
149 crm_err(
"Conversion of %s was clipped: %lld", text, result);
182 char *local_end_text = NULL;
187 text = (text != NULL) ? text : default_text;
191 crm_debug(
"No text and no default conversion value supplied");
195 *result = strtod(text, &local_end_text);
197 if (errno == ERANGE) {
207 const char *over_under;
209 if (fabs(*result) > DBL_MIN) {
214 over_under =
"under";
217 crm_debug(
"Floating-point value parsed from '%s' would %sflow " 218 "(using %g instead)", text, over_under, *result);
220 }
else if (errno != 0) {
225 crm_debug(
"Could not parse floating-point value from '%s' (using " 229 }
else if (local_end_text == text) {
234 crm_debug(
"Could not parse floating-point value from '%s' (using " 235 "%.1f instead): No digits found", text,
238 }
else if (fabs(*result) <= DBL_MIN) {
251 for (
const char *p = text; p != local_end_text; p++) {
252 if (strchr(
"0.eE", *p) == NULL) {
254 crm_debug(
"Floating-point value parsed from '%s' would " 255 "underflow (using %g instead)", text, *result);
261 crm_trace(
"Floating-point value parsed successfully from " 262 "'%s': %g", text, *result);
265 if ((end_text == NULL) && !pcmk__str_empty(local_end_text)) {
266 crm_debug(
"Characters left over after parsing '%s': '%s'",
267 text, local_end_text);
271 if (end_text != NULL) {
272 *end_text = local_end_text;
296 CRM_CHECK((table != NULL) && (key != NULL),
return EINVAL);
298 value = g_hash_table_lookup(table, key);
300 if (result != NULL) {
301 *result = default_val;
311 if ((value_ll < 0) || (value_ll > G_MAXUINT)) {
312 crm_warn(
"Could not parse non-negative integer from %s", value);
316 if (result != NULL) {
317 *result = (guint) value_ll;
323 # define NUMCHARS "0123456789." 327 # define WHITESPACE " \t\n\r\f" 342 const char *num_start = NULL;
344 long long multiplier = 1000;
345 long long divisor = 1;
348 char *end_text = NULL;
354 num_start = input + strspn(input,
WHITESPACE);
355 num_len = strspn(num_start,
NUMCHARS);
359 units = num_start + num_len;
362 if (!strncasecmp(units,
"ms", 2) || !strncasecmp(units,
"msec", 4)) {
365 }
else if (!strncasecmp(units,
"us", 2) || !strncasecmp(units,
"usec", 4)) {
368 }
else if (!strncasecmp(units,
"s", 1) || !strncasecmp(units,
"sec", 3)) {
371 }
else if (!strncasecmp(units,
"m", 1) || !strncasecmp(units,
"min", 3)) {
372 multiplier = 60 * 1000;
374 }
else if (!strncasecmp(units,
"h", 1) || !strncasecmp(units,
"hr", 2)) {
375 multiplier = 60 * 60 * 1000;
377 }
else if ((*units !=
EOS) && (*units !=
'\n') && (*units !=
'\r')) {
381 scan_ll(num_start, &msec, &end_text);
382 if (msec > (LLONG_MAX / multiplier)) {
394 gboolean ret = FALSE;
408 }
else if (strcasecmp(s,
"true") == 0
409 || strcasecmp(s,
"on") == 0
410 || strcasecmp(s,
"yes") == 0 || strcasecmp(s,
"y") == 0 || strcasecmp(s,
"1") == 0) {
414 }
else if (strcasecmp(s,
"false") == 0
415 || strcasecmp(s,
"off") == 0
416 || strcasecmp(s,
"no") == 0 || strcasecmp(s,
"n") == 0 || strcasecmp(s,
"0") == 0) {
432 for (len = strlen(str) - 1; len >= 0 && str[len] ==
'\n'; len--) {
455 const char *p = prefix;
469 ends_with(
const char *s,
const char *match,
bool as_extension)
471 if (pcmk__str_empty(match)) {
473 }
else if (s == NULL) {
482 s = strrchr(s, match[0]);
483 return (s == NULL)? false : !strcmp(s, match);
486 mlen = strlen(match);
488 return ((slen >= mlen) && !strcmp(s + slen - mlen, match));
506 return ends_with(s, match,
false);
533 return ends_with(s, match,
true);
552 const signed char *p;
555 for (p = v; *p !=
'\0'; p++)
556 h = (h << 5) - h + *p;
565 return pcmk__str_eq((
const char *)a, (
const char *)b,
pcmk__str_casei);
571 const signed char *p;
574 for (p = v; *p !=
'\0'; p++)
575 h = (h << 5) - h + g_ascii_tolower(*p);
581 copy_str_table_entry(gpointer key, gpointer value, gpointer user_data)
583 if (key && value && user_data) {
584 g_hash_table_insert((GHashTable*)user_data, strdup(key), strdup(value));
591 GHashTable *new_table = NULL;
594 new_table = crm_str_table_new();
595 g_hash_table_foreach(old_table, copy_str_table_entry, new_table);
618 const char *separator)
620 size_t orig_len, new_len;
624 if (pcmk__str_empty(word)) {
629 orig_len = (len != NULL)? *len : ((*list == NULL)? 0 : strlen(*list));
636 }
else if (separator == NULL) {
640 new_len = orig_len + strlen(separator) + strlen(word);
646 *list = pcmk__realloc(*list, new_len + 1);
647 sprintf(*list + orig_len,
"%s%s", separator, word);
664 char **result,
unsigned int *result_len)
667 char *compressed = NULL;
668 char *uncompressed = strdup(
data);
669 #ifdef CLOCK_MONOTONIC 670 struct timespec after_t;
671 struct timespec before_t;
675 max = (length * 1.01) + 601;
678 #ifdef CLOCK_MONOTONIC 679 clock_gettime(CLOCK_MONOTONIC, &before_t);
682 compressed = calloc((
size_t) max,
sizeof(
char));
686 rc = BZ2_bzBuffToBuffCompress(compressed, result_len, uncompressed, length,
690 crm_err(
"Compression of %d bytes failed: %s " CRM_XS " bzerror=%d",
696 #ifdef CLOCK_MONOTONIC 697 clock_gettime(CLOCK_MONOTONIC, &after_t);
699 crm_trace(
"Compressed %d bytes into %d (ratio %d:1) in %.0fms",
700 length, *result_len, length / (*result_len),
701 (after_t.tv_sec - before_t.tv_sec) * 1000 +
702 (after_t.tv_nsec - before_t.tv_nsec) / 1e6);
704 crm_trace(
"Compressed %d bytes into %d (ratio %d:1)",
705 length, *result_len, length / (*result_len));
708 *result = compressed;
719 va_start(ap, format);
720 len = vasprintf (&
string, format, ap);
729 char *remainder = NULL;
736 crm_trace(
"Attempting to decode: [%s]", srcstring);
737 if (pcmk__str_empty(srcstring) || !strcmp(srcstring,
"-")) {
744 if (*srcstring ==
'-') {
745 int rc = scan_ll(srcstring+1, end, &remainder);
754 if (scan_ll(srcstring, start, &remainder) !=
pcmk_rc_ok) {
758 if (*remainder && *remainder ==
'-') {
759 if (*(remainder+1)) {
760 char *more_remainder = NULL;
761 int rc = scan_ll(remainder+1, end, &more_remainder);
767 }
else if (*remainder && *remainder !=
'-') {
800 if (strcmp(lst->data,
"*") == 0 && lst->next == NULL) {
804 return g_list_find_custom(lst, s, (GCompareFunc) strcmp) != NULL;
808 str_any_of(
bool casei,
const char *s, va_list args)
814 const char *ele = va_arg(args,
const char *);
818 }
else if (pcmk__str_eq(s, ele,
848 rc = str_any_of(
true, s, ap);
872 rc = str_any_of(
false, s, ap);
901 const char *ele = va_arg(ap,
const char *);
905 }
else if (strchr(ele, ch) != NULL) {
934 if (isdigit(*s1) && isdigit(*s2)) {
939 long num1 = strtol(s1, &end1, 10);
940 long num2 = strtol(s2, &end2, 10);
943 size_t len1 = end1 - s1;
944 size_t len2 = end2 - s2;
948 }
else if (num1 > num2) {
950 }
else if (len1 < len2) {
952 }
else if (len1 > len2) {
959 int lower1 = tolower(*s1);
960 int lower2 = tolower(*s2);
962 if (lower1 < lower2) {
964 }
else if (lower1 > lower2) {
973 }
else if (*s1 && !*s2) {
1013 regex_t *r_patt = calloc(1,
sizeof(regex_t));
1014 int reg_flags = REG_EXTENDED | REG_NOSUB;
1018 if (s1 == NULL || s2 == NULL) {
1024 reg_flags |= REG_ICASE;
1026 regcomp_rc = regcomp(r_patt, s2, reg_flags);
1027 if (regcomp_rc != 0) {
1031 rc = regexec(r_patt, s1, 0, NULL, 0);
1053 if (s1 == NULL || s2 == NULL) {
1063 }
else if (s2 == NULL) {
1068 return strcasecmp(s1, s2);
1070 return strcmp(s1, s2);
1078 gboolean
crm_str_eq(
const char *a,
const char *b, gboolean use_case);
1087 }
else if (a == NULL || b == NULL) {
1090 }
else if (strcasecmp(a, b) == 0) {
1101 return g_strcmp0(a, b) == 0;
1104 }
else if (a == b) {
1107 }
else if (a == NULL || b == NULL) {
1111 }
else if (strcasecmp(a, b) == 0) {
#define CRM_CHECK(expr, failure_action)
bool pcmk__starts_with(const char *str, const char *prefix)
Check whether a string starts with a certain sequence.
const char * bz2_strerror(int rc)
char * crm_strdup_printf(char const *format,...)
int pcmk_numeric_strcasecmp(const char *s1, const char *s2)
Sort strings, with numeric portions sorted numerically.
guint g_str_hash_traditional(gconstpointer v)
#define PCMK__PARSE_DBL_DEFAULT
gboolean pcmk__str_in_list(GList *lst, const gchar *s)
bool pcmk__str_any_of(const char *s,...)
int pcmk__scan_double(const char *text, double *result, const char *default_text, char **end_text)
const char * pcmk_rc_str(int rc)
Get a user-friendly description of a return code.
char * strerror(int errnum)
#define PCMK__PARSE_INT_DEFAULT
#define crm_warn(fmt, args...)
int pcmk__guint_from_hash(GHashTable *table, const char *key, guint default_val, guint *result)
#define crm_debug(fmt, args...)
int pcmk__parse_ll_range(const char *srcstring, long long *start, long long *end)
int pcmk__compress(const char *data, unsigned int length, unsigned int max, char **result, unsigned int *result_len)
#define crm_trace(fmt, args...)
#define pcmk_is_set(g, f)
Convenience alias for pcmk_all_flags_set(), to check single flag.
long long crm_get_msec(const char *input)
Parse a time+units string and return milliseconds equivalent.
char * crm_itoa_stack(int an_int, char *buffer, size_t len)
int crm_str_to_boolean(const char *s, int *ret)
void pcmk__add_separated_word(char **list, size_t *len, const char *word, const char *separator)
char * crm_strip_trailing_newline(char *str)
int pcmk__strcmp(const char *s1, const char *s2, uint32_t flags)
gboolean crm_is_true(const char *s)
bool pcmk__ends_with_ext(const char *s, const char *match)
gboolean crm_str_eq(const char *a, const char *b, gboolean use_case)
int crm_parse_int(const char *text, const char *default_text)
Parse an integer value from a string.
long long crm_parse_ll(const char *text, const char *default_text)
Parse a long long integer value from a string.
#define crm_err(fmt, args...)
gboolean crm_strcase_equal(gconstpointer a, gconstpointer b)
gboolean safe_str_neq(const char *a, const char *b)
guint crm_strcase_hash(gconstpointer v)
GHashTable * crm_str_table_dup(GHashTable *old_table)
bool pcmk__char_in_any_str(int ch,...)
bool pcmk__ends_with(const char *s, const char *match)
bool pcmk__strcase_any_of(const char *s,...)