1 |
commit: f6d31778569b3bb53a743515a8993aa64cdf0bfe |
2 |
Author: Fabian Groffen <grobian <AT> gentoo <DOT> org> |
3 |
AuthorDate: Mon May 25 10:28:35 2020 +0000 |
4 |
Commit: Fabian Groffen <grobian <AT> gentoo <DOT> org> |
5 |
CommitDate: Mon May 25 10:28:35 2020 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=f6d31778 |
7 |
|
8 |
libq/atom: add atom_clone function |
9 |
|
10 |
atom_clone() does a deep copy of the given atom, such that it lives |
11 |
completely on its own and needs its own atom_implode() call. |
12 |
|
13 |
Signed-off-by: Fabian Groffen <grobian <AT> gentoo.org> |
14 |
|
15 |
libq/atom.c | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
16 |
libq/atom.h | 1 + |
17 |
2 files changed, 107 insertions(+) |
18 |
|
19 |
diff --git a/libq/atom.c b/libq/atom.c |
20 |
index e79c30a..7e72bf3 100644 |
21 |
--- a/libq/atom.c |
22 |
+++ b/libq/atom.c |
23 |
@@ -355,6 +355,112 @@ atom_explode(const char *atom) |
24 |
return ret; |
25 |
} |
26 |
|
27 |
+depend_atom * |
28 |
+atom_clone(depend_atom *atom) |
29 |
+{ |
30 |
+ depend_atom *ret; |
31 |
+ char *p; |
32 |
+ size_t alen; |
33 |
+ size_t clen = 0; |
34 |
+ size_t flen = 0; |
35 |
+ size_t plen = 0; |
36 |
+ size_t nlen = 0; |
37 |
+ size_t slen = 0; |
38 |
+ size_t rlen = 0; |
39 |
+ |
40 |
+ if (atom->REPO != NULL) |
41 |
+ rlen = strlen(atom->REPO) + 1; |
42 |
+ if (atom->SLOT != NULL) |
43 |
+ slen = strlen(atom->SLOT) + 1; |
44 |
+ if (atom->CATEGORY != NULL) |
45 |
+ clen = strlen(atom->CATEGORY) + 1; |
46 |
+ if (atom->PF != NULL) |
47 |
+ flen = strlen(atom->PF) + 1; /* should include PVR */ |
48 |
+ if (atom->P != NULL) |
49 |
+ plen = strlen(atom->P) + 1; /* should include PV */ |
50 |
+ if (atom->PN != NULL) |
51 |
+ nlen = strlen(atom->PN) + 1; |
52 |
+ |
53 |
+ alen = sizeof(*ret) + clen + flen + plen + nlen + rlen + slen; |
54 |
+ ret = xmalloc(alen); |
55 |
+ memset(ret, '\0', sizeof(*ret)); |
56 |
+ |
57 |
+ /* build up main storage pointers, see explode */ |
58 |
+ p = (char *)ret + sizeof(*ret); |
59 |
+ if (atom->CATEGORY != NULL) { |
60 |
+ ret->CATEGORY = p; |
61 |
+ memcpy(ret->CATEGORY, atom->CATEGORY, clen); |
62 |
+ p += clen; |
63 |
+ } |
64 |
+ if (atom->PF != NULL) { |
65 |
+ ret->PF = p; |
66 |
+ memcpy(ret->PF, atom->PF, flen); |
67 |
+ p += flen; |
68 |
+ } |
69 |
+ if (atom->PVR > atom->PF && atom->PVR < (atom->PF + flen)) |
70 |
+ ret->PVR = ret->PF + (atom->PVR - atom->PF); |
71 |
+ if (atom->P != NULL) { |
72 |
+ ret->P = p; |
73 |
+ memcpy(ret->P, atom->P, plen); |
74 |
+ p += plen; |
75 |
+ } |
76 |
+ if (atom->PV > atom->P && atom->PV < (atom->P + plen)) |
77 |
+ ret->PV = ret->P + (atom->PV - atom->P); |
78 |
+ if (atom->PN != NULL) { |
79 |
+ ret->PN = p; |
80 |
+ memcpy(ret->PN, atom->PN, nlen); |
81 |
+ p += nlen; |
82 |
+ } |
83 |
+ if (atom->SLOT != NULL) { |
84 |
+ ret->SLOT = p; |
85 |
+ memcpy(ret->SLOT, atom->SLOT, slen); |
86 |
+ p += slen; |
87 |
+ } |
88 |
+ if (atom->SUBSLOT > atom->SLOT && atom->SUBSLOT < (atom->SLOT + slen)) |
89 |
+ ret->SUBSLOT = ret->SLOT + (atom->SUBSLOT - atom->SLOT); |
90 |
+ if (atom->REPO != NULL) { |
91 |
+ ret->REPO = p; |
92 |
+ memcpy(ret->REPO, atom->REPO, rlen); |
93 |
+ p += rlen; |
94 |
+ } |
95 |
+ |
96 |
+ ret->blocker = atom->blocker; |
97 |
+ ret->pfx_op = atom->pfx_op; |
98 |
+ ret->sfx_op = atom->pfx_op; |
99 |
+ ret->PR_int = atom->PR_int; |
100 |
+ ret->letter = atom->letter; |
101 |
+ ret->slotdep = atom->slotdep; |
102 |
+ |
103 |
+ if (atom->suffixes != NULL) { |
104 |
+ for (slen = 0; atom->suffixes[slen].suffix != VER_NORM; slen++) |
105 |
+ ; |
106 |
+ slen++; |
107 |
+ ret->suffixes = xmalloc(sizeof(ret->suffixes[0]) * slen); |
108 |
+ memcpy(ret->suffixes, atom->suffixes, sizeof(ret->suffixes[0]) * slen); |
109 |
+ } |
110 |
+ |
111 |
+ if (atom->usedeps) { |
112 |
+ atom_usedep *w; |
113 |
+ atom_usedep *n = NULL; |
114 |
+ |
115 |
+ for (w = atom->usedeps; w != NULL; w = w->next) { |
116 |
+ nlen = w->use != NULL ? strlen(w->use) + 1 : 0; |
117 |
+ if (n == NULL) { |
118 |
+ atom->usedeps = n = xmalloc(sizeof(*n) + nlen); |
119 |
+ } else { |
120 |
+ n = n->next = xmalloc(sizeof(*n) + nlen); |
121 |
+ } |
122 |
+ n->next = NULL; |
123 |
+ n->pfx_cond = w->pfx_cond; |
124 |
+ n->sfx_cond = w->sfx_cond; |
125 |
+ n->use = (char *)n + sizeof(*n); |
126 |
+ memcpy(n->use, w->use, nlen); |
127 |
+ } |
128 |
+ } |
129 |
+ |
130 |
+ return ret; |
131 |
+} |
132 |
+ |
133 |
void |
134 |
atom_implode(depend_atom *atom) |
135 |
{ |
136 |
|
137 |
diff --git a/libq/atom.h b/libq/atom.h |
138 |
index 1548dd9..aff4548 100644 |
139 |
--- a/libq/atom.h |
140 |
+++ b/libq/atom.h |
141 |
@@ -97,6 +97,7 @@ typedef enum { |
142 |
} atom_equality; |
143 |
|
144 |
depend_atom *atom_explode(const char *atom); |
145 |
+depend_atom *atom_clone(depend_atom *atom); |
146 |
void atom_implode(depend_atom *atom); |
147 |
atom_equality atom_compare(const depend_atom *a1, const depend_atom *a2); |
148 |
atom_equality atom_compare_str(const char * const s1, const char * const s2); |