diff options
Diffstat (limited to 'index.c')
-rw-r--r-- | index.c | 28 |
1 files changed, 28 insertions, 0 deletions
@@ -4,6 +4,17 @@ #include "index.h" #include "macros.h" +static int index_strlen(index *idx) { + int len = 0; + /* Get the length of the index. */ + for (index *i = idx; i != NULL; i = i->next) { + const int is_last = (i->next == NULL); + const char *fmt = (i->type) "%llu" : "%s"; + len += format_len(fmt, (i->type) ? i->num : i->name) + !is_last; + } + return len; +} + int is_valid_index(index *idx) { int did_alloc = 0; char *err_str = NULL; @@ -68,3 +79,20 @@ char *make_index_path(const char *root, index *idx, int path_type) { return path; } + +char *index_to_str(index *idx) { + if (is_valid_index(idx)) { + char *idx_str = calloc(index_strlen(idx)+1, sizeof(char)); + char *tmp = idx_str; + /* Create the index. */ + for (index *i = idx; i != NULL; i = i->next) { + const int is_last = (i->next == NULL); + const char *fmt = (i->type) "%llu%s" : "%s%s"; + + tmp += sprintf(tmp, fmt, (i->type) ? i->num : i->name, (!is_last) ? "-" : ""); + } + return idx_str; + } else { + return NULL; + } +} |