blob: d996b45a4552fe63506185fe51ceaef123677da8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include "index.h"
#include "macros.h"
static int index_strlen(index *idx) {
int len = 0;
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;
if (idx == NULL) {
err_str = "Index is NULL.";
} else if (idx->type > 1) {
did_alloc = 1;
err_str = calloc(format_len("Invalid index type: %u.", idx->type)+1, sizeof(char));
sprintf(err_str, "Invalid index type: %u.", idx->type);
} else if (idx->type == 0 && is_empty(idx->name)) {
err_str = "Named index is empty.";
}
if (err_str != NULL) {
log_reason(LOG_ERR, "Invalid index.", reason);
if (did_alloc) {
free(err_str);
}
return 0;
} else if (idx->next != NULL) {
return is_valid_index(idx->next);
}
return 1;
}
char *make_index_path(const char *root, index *idx, int path_type) {
char *path;
char *tmp;
int path_len = strlen(root) + 1;
if (!is_valid_index(idx)) {
return NULL;
}
for (index *i = idx; i != NULL; i = i->next) {
const int is_last = (i->next == NULL);
const char *fmt = (i->type) "%llu" : "%s";
path_len += format_len(fmt, (i->type) ? i->num : i->name) + !is_last;
}
path = calloc(path_len+1, sizeof(char));
tmp = path;
tmp += sprintf(tmp, "%s/", root);
for (index *i = idx; i != NULL; i = i->next) {
const int is_last = (i->next == NULL);
const char *delm = (path_type) ? "-" : "/";
const char *fmt = (i->type) "%llu%s" : "%s%s";
tmp += sprintf(tmp, fmt, (i->type) ? i->num : i->name, (!is_last) ? delm : "");
}
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;
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;
}
}
|