From 15309c57bd8d0c7d0951eb9b806c614d39050c01 Mon Sep 17 00:00:00 2001 From: Dave Wysochanski Date: Wed, 25 Apr 2007 18:24:19 +0000 Subject: [PATCH] Add count_chars and count_chars_len functions, two generic string utility functions. -- --- WHATS_NEW | 1 + lib/misc/lvm-string.c | 46 +++++++++++++++++++++++++++++++++---------- lib/misc/lvm-string.h | 4 ++++ 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/WHATS_NEW b/WHATS_NEW index 6b7238362..bb6b70867 100644 --- a/WHATS_NEW +++ b/WHATS_NEW @@ -1,5 +1,6 @@ Version 2.02.25 - ================================= + Add count_chars and count_chars_len functions Add /sys/block listings to lvm_dump.sh Make lvm_dump.sh list /dev recursively Fix thread race in clvmd. diff --git a/lib/misc/lvm-string.c b/lib/misc/lvm-string.c index de6cdfca8..2dea2eeff 100644 --- a/lib/misc/lvm-string.c +++ b/lib/misc/lvm-string.c @@ -36,18 +36,38 @@ int emit_to_buffer(char **buffer, size_t *size, const char *fmt, ...) } /* - * Device layer names are all of the form --, any - * other hyphens that appear in these names are quoted with yet - * another hyphen. The top layer of any device has no layer - * name. eg, vg0-lvol0. + * Count occurences of 'c' in 'str' until we reach a null char. + * + * Returns: + * len - incremented for each char we encounter, whether 'c' or not. + * count - number of occurences of 'c' */ -static void _count_hyphens(const char *str, size_t *len, int *hyphens) +void count_chars(const char *str, size_t *len, int *count, + const char c) { const char *ptr; for (ptr = str; *ptr; ptr++, (*len)++) - if (*ptr == '-') - (*hyphens)++; + if (*ptr == c) + (*count)++; +} + +/* + * Count occurences of 'c' in 'str' of length 'size'. + * + * Returns: + * # of occurences of 'c' + */ +unsigned count_chars_len(const char *str, size_t size, const char c) +{ + int i; + unsigned count=0; + + for (i=0; i < size; i++) + if (str[i] == c) + count++; + return count; + } /* @@ -73,11 +93,11 @@ char *build_dm_name(struct dm_pool *mem, const char *vgname, int hyphens = 1; char *r, *out; - _count_hyphens(vgname, &len, &hyphens); - _count_hyphens(lvname, &len, &hyphens); + count_chars(vgname, &len, &hyphens, '-'); + count_chars(lvname, &len, &hyphens, '-'); if (layer && *layer) { - _count_hyphens(layer, &len, &hyphens); + count_chars(layer, &len, &hyphens, '-'); hyphens++; } @@ -105,6 +125,12 @@ char *build_dm_name(struct dm_pool *mem, const char *vgname, return r; } +/* + * Device layer names are all of the form --, any + * other hyphens that appear in these names are quoted with yet + * another hyphen. The top layer of any device has no layer + * name. eg, vg0-lvol0. + */ int validate_name(const char *n) { register char c; diff --git a/lib/misc/lvm-string.h b/lib/misc/lvm-string.h index 9b0f5ddc0..c5a87811d 100644 --- a/lib/misc/lvm-string.h +++ b/lib/misc/lvm-string.h @@ -30,4 +30,8 @@ char *build_dm_name(struct dm_pool *mem, const char *vg, int validate_name(const char *n); +void count_chars(const char *str, size_t *len, int *count, + char c); +unsigned count_chars_len(const char *str, size_t size, char c); + #endif