btrfs-progs: Add count_digits() function to help calculate filename len.
authorQu Wenruo <quwenruo@cn.fujitsu.com>
Tue, 9 Dec 2014 08:27:27 +0000 (16:27 +0800)
committerDavid Sterba <dsterba@suse.cz>
Wed, 10 Dec 2014 12:44:52 +0000 (13:44 +0100)
Add count_digits() function in utils.h to help calculate filename with
ino suffix.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
utils.h

diff --git a/utils.h b/utils.h
index 8950fca..6cbb7b8 100644 (file)
--- a/utils.h
+++ b/utils.h
@@ -169,4 +169,22 @@ int find_next_key(struct btrfs_path *path, struct btrfs_key *key);
 char* btrfs_group_type_str(u64 flag);
 char* btrfs_group_profile_str(u64 flag);
 
+/*
+ * Get the length of the string converted from a u64 number.
+ *
+ * Result is equal to log10(num) + 1, but without the use of math library.
+ */
+static inline int count_digits(u64 num)
+{
+       int ret = 0;
+
+       if (num == 0)
+               return 1;
+       while (num > 0) {
+               ret++;
+               num /= 10;
+       }
+       return ret;
+}
+
 #endif