-
Notifications
You must be signed in to change notification settings - Fork 14
/
path.c
132 lines (98 loc) · 2.22 KB
/
path.c
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "path.h"
#include "strlcpy.h"
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#define dbpath_invalidate_component(obj, comp) (((obj).comp)[0] = '\0')
int
split_path(const char *in, struct dbpath *out)
{
const char *pos1;
const char *pos2 = in;
if (in[0] != '/')
return -EINVAL;
dbpath_invalidate_component(*out, database);
dbpath_invalidate_component(*out, schema);
dbpath_invalidate_component(*out, table);
dbpath_invalidate_component(*out, row);
dbpath_invalidate_component(*out, column);
/* root */
if (in[1] == '\0')
return 0;
/* database */
pos1 = pos2 + 1;
pos2 = strchr(pos1, '/');
if (pos2 == NULL)
{
strcpy(out->database, pos1);
return 0;
}
strlcpy(out->database, pos1, pos2 - pos1);
/* schema */
pos1 = pos2 + 1;
pos2 = strchr(pos1, '/');
if (pos2 == NULL)
{
strcpy(out->schema, pos1);
return 0;
}
strlcpy(out->schema, pos1, pos2 - pos1);
/* table */
pos1 = pos2 + 1;
pos2 = strchr(pos1, '/');
if (pos2 == NULL)
{
strcpy(out->table, pos1);
return 0;
}
strlcpy(out->table, pos1, pos2 - pos1);
/* row */
pos1 = pos2 + 1;
pos2 = strchr(pos1, '/');
if (pos2 == NULL)
{
strcpy(out->row, pos1);
return 0;
}
strlcpy(out->row, pos1, pos2 - pos1);
/* column */
pos1 = pos2 + 1;
pos2 = strchr(pos1, '/');
if (pos2 == NULL)
{
strcpy(out->column, pos1);
return 0;
}
strlcpy(out->column, pos1, pos2 - pos1);
/* rest */
pos1 = pos2 + 1;
pos2 = strchr(pos1, '/');
if (pos2 == NULL)
return 0;
else
return -EINVAL;
}
const char *
dbpath_to_string(const struct dbpath *in)
{
static char out[PATH_MAX * 5 + 50];
snprintf(out, sizeof(out),
"database=%s schema=%s table=%s row=%s column=%s",
in->database,
in->schema,
in->table,
in->row,
in->column);
return out;
}
bool
dbpaths_are_same_level(struct dbpath *path1, struct dbpath *path2)
{
return ((dbpath_is_root(*path1) && dbpath_is_root(*path2))
|| (dbpath_is_database(*path1) && dbpath_is_database(*path2))
|| (dbpath_is_schema(*path1) && dbpath_is_schema(*path2))
|| (dbpath_is_table(*path1) && dbpath_is_table(*path2))
|| (dbpath_is_row(*path1) && dbpath_is_row(*path2))
|| (dbpath_is_column(*path1) && dbpath_is_column(*path2)));
}