| 1 |
/// @file |
| 2 |
/// @brief generic manipulations with @ref Agobj_t and derived objects graphs, nodes and edges |
| 3 |
/// @ingroup cgraph_object |
| 4 |
|
| 5 |
/************************************************************************* |
| 6 |
* Copyright (c) 2011 AT&T Intellectual Property |
| 7 |
* All rights reserved. This program and the accompanying materials |
| 8 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 9 |
* which accompanies this distribution, and is available at |
| 10 |
* https://www.eclipse.org/legal/epl-v10.html |
| 11 |
* |
| 12 |
* Contributors: Details at https://graphviz.org |
| 13 |
*************************************************************************/ |
| 14 |
|
| 15 |
#include "config.h" |
| 16 |
|
| 17 |
#include <cgraph/cghdr.h> |
| 18 |
#include <stdlib.h> |
| 19 |
#include <util/alloc.h> |
| 20 |
#include <util/unreachable.h> |
| 21 |
|
| 22 |
int agdelete(Agraph_t * g, void *obj) |
| 23 |
{ |
| 24 |
if (AGTYPE(obj) == AGRAPH && g != agparent(obj)) { |
| 25 |
agerrorf("agdelete on wrong graph"); |
| 26 |
return FAILURE; |
| 27 |
} |
| 28 |
|
| 29 |
switch (AGTYPE(obj)) { |
| 30 |
case AGNODE: |
| 31 |
return agdelnode(g, obj); |
| 32 |
case AGINEDGE: |
| 33 |
case AGOUTEDGE: |
| 34 |
return agdeledge(g, obj); |
| 35 |
case AGRAPH: |
| 36 |
return agclose(obj); |
| 37 |
default: |
| 38 |
agerrorf("agdelete on bad object"); |
| 39 |
} |
| 40 |
return SUCCESS; /* not reached */ |
| 41 |
} |
| 42 |
|
| 43 |
int agrename(Agobj_t * obj, char *newname) |
| 44 |
{ |
| 45 |
Agraph_t *g; |
| 46 |
IDTYPE old_id, new_id; |
| 47 |
|
| 48 |
switch (AGTYPE(obj)) { |
| 49 |
case AGRAPH: |
| 50 |
old_id = AGID(obj); |
| 51 |
g = agraphof(obj); |
| 52 |
/* can we reserve the id corresponding to newname? */ |
| 53 |
if (agmapnametoid(agroot(g), AGTYPE(obj), newname, &new_id, false) == 0) |
| 54 |
return FAILURE; |
| 55 |
if (new_id == old_id) |
| 56 |
return SUCCESS; |
| 57 |
if (agmapnametoid(agroot(g), AGTYPE(obj), newname, &new_id, true) == 0) |
| 58 |
return FAILURE; |
| 59 |
/* obj* is unchanged, so no need to re agregister() */ |
| 60 |
if (agparent(g) && agidsubg(agparent(g), new_id)) |
| 61 |
return FAILURE; |
| 62 |
agfreeid(g, AGRAPH, old_id); |
| 63 |
AGID(g) = new_id; |
| 64 |
break; |
| 65 |
case AGNODE: |
| 66 |
return agrelabel_node((Agnode_t *) obj, newname); |
| 67 |
case AGINEDGE: |
| 68 |
case AGOUTEDGE: |
| 69 |
return FAILURE; |
| 70 |
default: |
| 71 |
UNREACHABLE(); |
| 72 |
} |
| 73 |
return SUCCESS; |
| 74 |
} |
| 75 |
|
| 76 |
/* perform initialization/update/finalization method invocation. |
| 77 |
* skip over nil pointers to next method below. |
| 78 |
*/ |
| 79 |
|
| 80 |
void agmethod_init(Agraph_t * g, void *obj) |
| 81 |
{ |
| 82 |
aginitcb(g, obj, g->clos->cb); |
| 83 |
} |
| 84 |
|
| 85 |
void aginitcb(Agraph_t * g, void *obj, Agcbstack_t * cbstack) |
| 86 |
{ |
| 87 |
agobjfn_t fn; |
| 88 |
|
| 89 |
if (cbstack == NULL) |
| 90 |
return; |
| 91 |
aginitcb(g, obj, cbstack->prev); |
| 92 |
fn = NULL; |
| 93 |
switch (AGTYPE(obj)) { |
| 94 |
case AGRAPH: |
| 95 |
fn = cbstack->f->graph.ins; |
| 96 |
break; |
| 97 |
case AGNODE: |
| 98 |
fn = cbstack->f->node.ins; |
| 99 |
break; |
| 100 |
case AGEDGE: |
| 101 |
fn = cbstack->f->edge.ins; |
| 102 |
break; |
| 103 |
default: // ignore |
| 104 |
break; |
| 105 |
} |
| 106 |
if (fn) |
| 107 |
fn(g, obj, cbstack->state); |
| 108 |
} |
| 109 |
|
| 110 |
void agmethod_upd(Agraph_t * g, void *obj, Agsym_t * sym) |
| 111 |
{ |
| 112 |
agupdcb(g, obj, sym, g->clos->cb); |
| 113 |
} |
| 114 |
|
| 115 |
void agupdcb(Agraph_t * g, void *obj, Agsym_t * sym, Agcbstack_t * cbstack) |
| 116 |
{ |
| 117 |
agobjupdfn_t fn; |
| 118 |
|
| 119 |
if (cbstack == NULL) |
| 120 |
return; |
| 121 |
agupdcb(g, obj, sym, cbstack->prev); |
| 122 |
fn = NULL; |
| 123 |
switch (AGTYPE(obj)) { |
| 124 |
case AGRAPH: |
| 125 |
fn = cbstack->f->graph.mod; |
| 126 |
break; |
| 127 |
case AGNODE: |
| 128 |
fn = cbstack->f->node.mod; |
| 129 |
break; |
| 130 |
case AGEDGE: |
| 131 |
fn = cbstack->f->edge.mod; |
| 132 |
break; |
| 133 |
default: // ignore |
| 134 |
break; |
| 135 |
} |
| 136 |
if (fn) |
| 137 |
fn(g, obj, cbstack->state, sym); |
| 138 |
} |
| 139 |
|
| 140 |
void agmethod_delete(Agraph_t * g, void *obj) |
| 141 |
{ |
| 142 |
agdelcb(g, obj, g->clos->cb); |
| 143 |
} |
| 144 |
|
| 145 |
void agdelcb(Agraph_t * g, void *obj, Agcbstack_t * cbstack) |
| 146 |
{ |
| 147 |
agobjfn_t fn; |
| 148 |
|
| 149 |
if (cbstack == NULL) |
| 150 |
return; |
| 151 |
agdelcb(g, obj, cbstack->prev); |
| 152 |
fn = NULL; |
| 153 |
switch (AGTYPE(obj)) { |
| 154 |
case AGRAPH: |
| 155 |
fn = cbstack->f->graph.del; |
| 156 |
break; |
| 157 |
case AGNODE: |
| 158 |
fn = cbstack->f->node.del; |
| 159 |
break; |
| 160 |
case AGEDGE: |
| 161 |
fn = cbstack->f->edge.del; |
| 162 |
break; |
| 163 |
default: // ignore |
| 164 |
break; |
| 165 |
} |
| 166 |
if (fn) |
| 167 |
fn(g, obj, cbstack->state); |
| 168 |
} |
| 169 |
|
| 170 |
Agraph_t *agroot(void* obj) |
| 171 |
{ |
| 172 |
if (obj == 0) return NULL; |
| 173 |
switch (AGTYPE(obj)) { |
| 174 |
case AGINEDGE: |
| 175 |
case AGOUTEDGE: |
| 176 |
return ((Agedge_t *) obj)->node->root; |
| 177 |
case AGNODE: |
| 178 |
return ((Agnode_t *) obj)->root; |
| 179 |
case AGRAPH: |
| 180 |
return ((Agraph_t *) obj)->root; |
| 181 |
default: /* actually can't occur if only 2 bit tags */ |
| 182 |
agerrorf("agroot of a bad object"); |
| 183 |
return NULL; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
Agraph_t *agraphof(void *obj) |
| 188 |
{ |
| 189 |
switch (AGTYPE(obj)) { |
| 190 |
case AGINEDGE: |
| 191 |
case AGOUTEDGE: |
| 192 |
return ((Agedge_t *) obj)->node->root; |
| 193 |
case AGNODE: |
| 194 |
return ((Agnode_t *) obj)->root; |
| 195 |
case AGRAPH: |
| 196 |
return obj; |
| 197 |
default: /* actually can't occur if only 2 bit tags */ |
| 198 |
agerrorf("agraphof a bad object"); |
| 199 |
return NULL; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
/* to manage disciplines */ |
| 204 |
void agpushdisc(Agraph_t * g, Agcbdisc_t * cbd, void *state) |
| 205 |
{ |
| 206 |
Agcbstack_t *stack_ent = gv_alloc(sizeof(Agcbstack_t)); |
| 207 |
stack_ent->f = cbd; |
| 208 |
stack_ent->state = state; |
| 209 |
stack_ent->prev = g->clos->cb; |
| 210 |
g->clos->cb = stack_ent; |
| 211 |
} |
| 212 |
|
| 213 |
int agpopdisc(Agraph_t * g, Agcbdisc_t * cbd) |
| 214 |
{ |
| 215 |
Agcbstack_t *stack_ent; |
| 216 |
|
| 217 |
stack_ent = g->clos->cb; |
| 218 |
if (stack_ent) { |
| 219 |
if (stack_ent->f == cbd) |
| 220 |
g->clos->cb = stack_ent->prev; |
| 221 |
else { |
| 222 |
while (stack_ent && stack_ent->prev->f != cbd) |
| 223 |
stack_ent = stack_ent->prev; |
| 224 |
if (stack_ent && stack_ent->prev) |
| 225 |
stack_ent->prev = stack_ent->prev->prev; |
| 226 |
} |
| 227 |
if (stack_ent) { |
| 228 |
free(stack_ent); |
| 229 |
return SUCCESS; |
| 230 |
} |
| 231 |
} |
| 232 |
return FAILURE; |
| 233 |
} |
| 234 |
|
| 235 |
int agcontains(Agraph_t* g, void* obj) |
| 236 |
{ |
| 237 |
Agraph_t* subg; |
| 238 |
|
| 239 |
if (agroot (g) != agroot (obj)) return 0; |
| 240 |
switch (AGTYPE(obj)) { |
| 241 |
case AGRAPH: |
| 242 |
subg = obj; |
| 243 |
do { |
| 244 |
if (subg == g) return 1; |
| 245 |
} while ((subg = agparent (subg))); |
| 246 |
return 0; |
| 247 |
case AGNODE: |
| 248 |
return agidnode(g, AGID(obj), 0) != 0; |
| 249 |
default: |
| 250 |
return agsubedge(g, obj, 0) != 0; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
int agobjkind(void *arg) |
| 255 |
{ |
| 256 |
return AGTYPE(arg); |
| 257 |
} |