]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
utils: Add helper routines for indent handling
authorParav Pandit <parav@nvidia.com>
Wed, 10 Feb 2021 18:34:42 +0000 (20:34 +0200)
committerDavid Ahern <dsahern@kernel.org>
Thu, 11 Feb 2021 16:08:13 +0000 (09:08 -0700)
Subsequent patch needs to use 2 char indentation for nested objects.
Hence introduce a generic helpers to allocate, deallocate, increment,
decrement and to print indent block.

Signed-off-by: Parav Pandit <parav@nvidia.com>
Signed-off-by: David Ahern <dsahern@kernel.org>
include/utils.h
lib/utils.c

index e66090ae5169cd886901c8091cdcece110516816..9b76c92a948390eecd45fc835971f4e2a380473b 100644 (file)
@@ -349,4 +349,20 @@ int str_map_lookup_str(const struct str_num_map *map, const char *needle);
 const char *str_map_lookup_u16(const struct str_num_map *map, uint16_t val);
 const char *str_map_lookup_u8(const struct str_num_map *map, uint8_t val);
 
+unsigned int get_str_char_count(const char *str, int match);
+int str_split_by_char(char *str, char **before, char **after, int match);
+
+#define INDENT_STR_MAXLEN 32
+
+struct indent_mem {
+       int indent_level;
+       char indent_str[INDENT_STR_MAXLEN + 1];
+};
+
+struct indent_mem *alloc_indent_mem(void);
+void free_indent_mem(struct indent_mem *mem);
+void inc_indent(struct indent_mem *mem);
+void dec_indent(struct indent_mem *mem);
+void print_indent(struct indent_mem *mem);
+
 #endif /* __UTILS_H__ */
index af1b553cd67f3a5fb65544863fc7b57822f5b294..cc6d0e34896c66f543686f937cdcf736442ae9a2 100644 (file)
@@ -1978,3 +1978,69 @@ const char *str_map_lookup_u8(const struct str_num_map *map, uint8_t val)
        }
        return NULL;
 }
+
+unsigned int get_str_char_count(const char *str, int match)
+{
+       unsigned int count = 0;
+       const char *pos = str;
+
+       while ((pos = strchr(pos, match))) {
+               count++;
+               pos++;
+       }
+       return count;
+}
+
+int str_split_by_char(char *str, char **before, char **after, int match)
+{
+       char *slash;
+
+       slash = strrchr(str, match);
+       if (!slash)
+               return -EINVAL;
+       *slash = '\0';
+       *before = str;
+       *after = slash + 1;
+       return 0;
+}
+
+struct indent_mem *alloc_indent_mem(void)
+{
+       struct indent_mem *mem = malloc(sizeof(*mem));
+
+       if (!mem)
+               return NULL;
+       strcpy(mem->indent_str, "");
+       mem->indent_level = 0;
+       return mem;
+}
+
+void free_indent_mem(struct indent_mem *mem)
+{
+       free(mem);
+}
+
+#define INDENT_STR_STEP 2
+
+void inc_indent(struct indent_mem *mem)
+{
+       if (mem->indent_level + INDENT_STR_STEP > INDENT_STR_MAXLEN)
+               return;
+       mem->indent_level += INDENT_STR_STEP;
+       memset(mem->indent_str, ' ', sizeof(mem->indent_str));
+       mem->indent_str[mem->indent_level] = '\0';
+}
+
+void dec_indent(struct indent_mem *mem)
+{
+       if (mem->indent_level - INDENT_STR_STEP < 0)
+               return;
+       mem->indent_level -= INDENT_STR_STEP;
+       mem->indent_str[mem->indent_level] = '\0';
+}
+
+void print_indent(struct indent_mem *mem)
+{
+       if (mem->indent_level)
+               printf("%s", mem->indent_str);
+}