Skip to contentSkip to conversation

convert _count_hyphens to generalised _count_chars and export

PATCH New Dave Wysochanski 1 participant 4 messages Apr 20, 2007 – Apr 23, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 convert _count_hyphens to generalised _count_chars and export

Should not change functional behavior and be simple to review. Adds newly created function to lvm-string.h exports.

lib/misc/lvm-string.c +20 -18
Index: LVM2/lib/misc/lvm-string.c
===================================================================
@@ -36,21 +36,6 @@ int emit_to_buffer(char **buffer, size_t
 }
 
 /*
- * Device layer names are all of the form <vg>-<lv>-<layer>, 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.
- */
-static void _count_hyphens(const char *str, size_t *len, int *hyphens)
-{
-	const char *ptr;
-
-	for (ptr = str; *ptr; ptr++, (*len)++)
-		if (*ptr == '-')
-			(*hyphens)++;
-}
-
-/*
  * Copies a string, quoting hyphens with hyphens.
  */
 static void _quote_hyphens(char **out, const char *src)
@@ -63,6 +48,17 @@ static void _quote_hyphens(char **out, c
 	}
 }
 
+
+void _count_chars(const char *str, size_t *len, int *count,
+		  char c)
+{
+	const char *ptr;
+
+	for (ptr = str; *ptr; ptr++, (*len)++)
+		if (*ptr == c)
+			(*count)++;
+}
+
 /*
  * <vg>-<lv>-<layer> or if !layer just <vg>-<lv>.
  */
@@ -73,11 +69,11 @@ char *build_dm_name(struct dm_pool *mem,
 	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 +101,12 @@ char *build_dm_name(struct dm_pool *mem,
 	return r;
 }
 
+/*
+ * Device layer names are all of the form <vg>-<lv>-<layer>, 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;
lib/misc/lvm-string.h +3 -0
Index: LVM2/lib/misc/lvm-string.h
===================================================================
@@ -30,4 +30,7 @@ char *build_dm_name(struct dm_pool *mem,
 
 int validate_name(const char *n);
 
+void _count_chars(const char *str, size_t *len, int *count,
+		  char c);
+
 #endif
WHATS_NEW +1 -0
Index: LVM2/WHATS_NEW
===================================================================
@@ -1,5 +1,6 @@
 Version 2.02.25 -
 =================================
+  Convert _count_hyphens to generalized _count_chars.
   Add dev_read_circular.
   Add pvck command stub.
   Update lists of attribute characters in man pages.
#1 lore raw

PATCH 1/1 convert _count_hyphens to generalised count_chars and export

(NOTE: only change from previous patch - remove '_' from '_count_chars') Should not change functional behavior and be simple to review. Adds newly created function to lvm-string.h exports.

lib/misc/lvm-string.c +20 -18
Index: LVM2/lib/misc/lvm-string.c
===================================================================
@@ -36,21 +36,6 @@ int emit_to_buffer(char **buffer, size_t
 }
 
 /*
- * Device layer names are all of the form <vg>-<lv>-<layer>, 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.
- */
-static void _count_hyphens(const char *str, size_t *len, int *hyphens)
-{
-	const char *ptr;
-
-	for (ptr = str; *ptr; ptr++, (*len)++)
-		if (*ptr == '-')
-			(*hyphens)++;
-}
-
-/*
  * Copies a string, quoting hyphens with hyphens.
  */
 static void _quote_hyphens(char **out, const char *src)
@@ -63,6 +48,17 @@ static void _quote_hyphens(char **out, c
 	}
 }
 
+
+void count_chars(const char *str, size_t *len, int *count,
+		  char c)
+{
+	const char *ptr;
+
+	for (ptr = str; *ptr; ptr++, (*len)++)
+		if (*ptr == c)
+			(*count)++;
+}
+
 /*
  * <vg>-<lv>-<layer> or if !layer just <vg>-<lv>.
  */
@@ -73,11 +69,11 @@ char *build_dm_name(struct dm_pool *mem,
 	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 +101,12 @@ char *build_dm_name(struct dm_pool *mem,
 	return r;
 }
 
+/*
+ * Device layer names are all of the form <vg>-<lv>-<layer>, 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;
lib/misc/lvm-string.h +3 -0
Index: LVM2/lib/misc/lvm-string.h
===================================================================
@@ -30,4 +30,7 @@ char *build_dm_name(struct dm_pool *mem,
 
 int validate_name(const char *n);
 
+void count_chars(const char *str, size_t *len, int *count,
+		 char c);
+
 #endif
WHATS_NEW +1 -0
Index: LVM2/WHATS_NEW
===================================================================
@@ -1,5 +1,6 @@
 Version 2.02.25 -
 =================================
+  Convert _count_hyphens to generalized count_chars.
   Add dev_read_circular.
   Add pvck command stub.
   Update lists of attribute characters in man pages.
#2 lore raw

PATCH 1/1 convert _count_hyphens to generalised count_chars and export

Yet another version (similar) of this patch is coming so don't bother review at this point.

Quoted 106 lines · Dave Wysochanski

(NOTE: only change from previous patch - remove '_' from '_count_chars') Should not change functional behavior and be simple to review. Adds newly created function to lvm-string.h exports.

Index: LVM2/lib/misc/lvm-string.c
===================================================================

lib/misc/lvm-string.c +20 -18
@@ -36,21 +36,6 @@ int emit_to_buffer(char **buffer, size_t
 }
 
 /*
- * Device layer names are all of the form <vg>-<lv>-<layer>, 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.
- */
-static void _count_hyphens(const char *str, size_t *len, int *hyphens)
-{
-	const char *ptr;
-
-	for (ptr = str; *ptr; ptr++, (*len)++)
-		if (*ptr == '-')
-			(*hyphens)++;
-}
-
-/*
  * Copies a string, quoting hyphens with hyphens.
  */
 static void _quote_hyphens(char **out, const char *src)
@@ -63,6 +48,17 @@ static void _quote_hyphens(char **out, c
 	}
 }
 
+
+void count_chars(const char *str, size_t *len, int *count,
+		  char c)
+{
+	const char *ptr;
+
+	for (ptr = str; *ptr; ptr++, (*len)++)
+		if (*ptr == c)
+			(*count)++;
+}
+
 /*
  * <vg>-<lv>-<layer> or if !layer just <vg>-<lv>.
  */
@@ -73,11 +69,11 @@ char *build_dm_name(struct dm_pool *mem,
 	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 +101,12 @@ char *build_dm_name(struct dm_pool *mem,
 	return r;
 }
 
+/*
+ * Device layer names are all of the form <vg>-<lv>-<layer>, 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;

Index: LVM2/lib/misc/lvm-string.h
===================================================================

lib/misc/lvm-string.h +3 -0
@@ -30,4 +30,7 @@ char *build_dm_name(struct dm_pool *mem,
 
 int validate_name(const char *n);
 
+void count_chars(const char *str, size_t *len, int *count,
+		 char c);
+
 #endif

Index: LVM2/WHATS_NEW
===================================================================

WHATS_NEW +1 -1
@@ -1,5 +1,6 @@
 Version 2.02.25 -
 =================================
+  Convert _count_hyphens to generalized count_chars.
   Add dev_read_circular.
   Add pvck command stub.
   Update lists of attribute characters in man pages.
 
 
--

lvm-devel mailing list
lvm-devel at redhat.com
https://www.redhat.com/mailman/listinfo/lvm-devel

#3 lore raw

PATCH 1/1 convert _count_hyphens to generalised count_chars_null and export

Add count_chars_null and count_chars_len functions, two generic string utility functions.

lib/misc/lvm-string.c +36 -10
Index: LVM2-working2/lib/misc/lvm-string.c
===================================================================
@@ -36,18 +36,38 @@ int emit_to_buffer(char **buffer, size_t
 }
 
 /*
- * Device layer names are all of the form <vg>-<lv>-<layer>, 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_null(const char *str, size_t *len, int *count,
+		      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, 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,
 	int hyphens = 1;
 	char *r, *out;
 
-	_count_hyphens(vgname, &len, &hyphens);
-	_count_hyphens(lvname, &len, &hyphens);
+	count_chars_null(vgname, &len, &hyphens, '-');
+	count_chars_null(lvname, &len, &hyphens, '-');
 
 	if (layer && *layer) {
-		_count_hyphens(layer, &len, &hyphens);
+		count_chars_null(layer, &len, &hyphens, '-');
 		hyphens++;
 	}
 
@@ -105,6 +125,12 @@ char *build_dm_name(struct dm_pool *mem,
 	return r;
 }
 
+/*
+ * Device layer names are all of the form <vg>-<lv>-<layer>, 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;
lib/misc/lvm-string.h +4 -0
Index: LVM2-working2/lib/misc/lvm-string.h
===================================================================
@@ -30,4 +30,8 @@ char *build_dm_name(struct dm_pool *mem,
 
 int validate_name(const char *n);
 
+void count_chars_null(const char *str, size_t *len, int *count,
+		      char c);
+unsigned count_chars_len(const char *str, size_t size, char c);
+
 #endif
WHATS_NEW +1 -0
Index: LVM2-working2/WHATS_NEW
===================================================================
@@ -1,5 +1,6 @@
 Version 2.02.25 -
 =================================
+  Add count_chars_null and count_chars_len functions.
   Add scan_sector param to label_read and _find_labeller.
   Make clvmd cope with quorum devices on RHEL5
   Add dev_read_circular.