diff options
author | mrb0nk500 <b0nk@b0nk.xyz> | 2022-07-26 17:51:29 -0300 |
---|---|---|
committer | mrb0nk500 <b0nk@b0nk.xyz> | 2022-07-26 17:51:29 -0300 |
commit | 4fc5243fa6c249c1f1e118f95f42870b7ccf8c6e (patch) | |
tree | 1b13957f98849bb152e77bbdc1ae429b1c04c0cf /index.c | |
parent | 45b91d6057fc67db1491d301cf44f4f93764c783 (diff) |
index: Add a function for converting an index into a string.
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; + } +} |