| 1 |
/** |
| 2 |
* @file |
| 3 |
* @ingroup cgraph_core |
| 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 <stdbool.h> |
| 19 |
#include <stdlib.h> |
| 20 |
#include <util/alloc.h> |
| 21 |
|
| 22 |
typedef struct IMapEntry_s { |
| 23 |
Dtlink_t namedict_link; |
| 24 |
Dtlink_t iddict_link; |
| 25 |
IDTYPE id; |
| 26 |
char *str; |
| 27 |
} IMapEntry_t; |
| 28 |
|
| 29 |
static int idcmpf(void *arg_p0, void *arg_p1) { |
| 30 |
IMapEntry_t *p0 = arg_p0; |
| 31 |
IMapEntry_t *p1 = arg_p1; |
| 32 |
if (p0->id > p1->id) |
| 33 |
{ |
| 34 |
return 1; |
| 35 |
} |
| 36 |
else if (p0->id < p1->id) |
| 37 |
{ |
| 38 |
return -1; |
| 39 |
} |
| 40 |
else |
| 41 |
{ |
| 42 |
return 0; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
/* note, OK to compare pointers into shared string pool |
| 47 |
* but can't probe with an arbitrary string pointer |
| 48 |
*/ |
| 49 |
static int namecmpf(void *arg_p0, void *arg_p1) { |
| 50 |
IMapEntry_t *p0 = arg_p0; |
| 51 |
IMapEntry_t *p1 = arg_p1; |
| 52 |
if (p0->str > p1->str) |
| 53 |
{ |
| 54 |
return 1; |
| 55 |
} |
| 56 |
else if (p0->str < p1->str) |
| 57 |
{ |
| 58 |
return -1; |
| 59 |
} |
| 60 |
else |
| 61 |
{ |
| 62 |
return 0; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
static Dtdisc_t LookupByName = { |
| 67 |
0, /* object ptr is passed as key */ |
| 68 |
0, /* size (ignored) */ |
| 69 |
offsetof(IMapEntry_t, namedict_link), |
| 70 |
NULL, |
| 71 |
NULL, |
| 72 |
namecmpf, |
| 73 |
}; |
| 74 |
|
| 75 |
static Dtdisc_t LookupById = { |
| 76 |
0, /* object ptr is passed as key */ |
| 77 |
0, /* size (ignored) */ |
| 78 |
offsetof(IMapEntry_t, iddict_link), |
| 79 |
NULL, |
| 80 |
NULL, |
| 81 |
idcmpf, |
| 82 |
}; |
| 83 |
|
| 84 |
bool aginternalmaplookup(Agraph_t *g, int objtype, char *str, IDTYPE *result) { |
| 85 |
Dict_t *d; |
| 86 |
IMapEntry_t *sym, template; |
| 87 |
char *search_str; |
| 88 |
|
| 89 |
if (objtype == AGINEDGE) |
| 90 |
objtype = AGEDGE; |
| 91 |
if ((d = g->clos->lookup_by_name[objtype])) { |
| 92 |
if ((search_str = agstrbind(g, str))) { |
| 93 |
template.str = search_str; |
| 94 |
sym = dtsearch(d, &template); |
| 95 |
if (sym) { |
| 96 |
*result = sym->id; |
| 97 |
return true; |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
/* caller GUARANTEES that this is a new entry */ |
| 105 |
void aginternalmapinsert(Agraph_t * g, int objtype, char *str, |
| 106 |
IDTYPE id) |
| 107 |
{ |
| 108 |
Dict_t *d_name_to_id, *d_id_to_name; |
| 109 |
|
| 110 |
IMapEntry_t *ent = gv_alloc(sizeof(IMapEntry_t)); |
| 111 |
ent->id = id; |
| 112 |
ent->str = agstrdup(g, str); |
| 113 |
|
| 114 |
if (objtype == AGINEDGE) |
| 115 |
objtype = AGEDGE; |
| 116 |
if ((d_name_to_id = g->clos->lookup_by_name[objtype]) == NULL) |
| 117 |
d_name_to_id = g->clos->lookup_by_name[objtype] = |
| 118 |
agdtopen(&LookupByName, Dttree); |
| 119 |
if ((d_id_to_name = g->clos->lookup_by_id[objtype]) == NULL) |
| 120 |
d_id_to_name = g->clos->lookup_by_id[objtype] = |
| 121 |
agdtopen(&LookupById, Dttree); |
| 122 |
dtinsert(d_name_to_id, ent); |
| 123 |
dtinsert(d_id_to_name, ent); |
| 124 |
} |
| 125 |
|
| 126 |
static IMapEntry_t *find_isym(Agraph_t * g, int objtype, IDTYPE id) |
| 127 |
{ |
| 128 |
Dict_t *d; |
| 129 |
IMapEntry_t *isym, itemplate; |
| 130 |
|
| 131 |
if (objtype == AGINEDGE) |
| 132 |
objtype = AGEDGE; |
| 133 |
if ((d = g->clos->lookup_by_id[objtype])) { |
| 134 |
itemplate.id = id; |
| 135 |
isym = dtsearch(d, &itemplate); |
| 136 |
} else |
| 137 |
isym = NULL; |
| 138 |
return isym; |
| 139 |
} |
| 140 |
|
| 141 |
char *aginternalmapprint(Agraph_t * g, int objtype, IDTYPE id) |
| 142 |
{ |
| 143 |
IMapEntry_t *isym; |
| 144 |
|
| 145 |
if ((isym = find_isym(g, objtype, id))) |
| 146 |
return isym->str; |
| 147 |
return NULL; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
int aginternalmapdelete(Agraph_t * g, int objtype, IDTYPE id) |
| 152 |
{ |
| 153 |
IMapEntry_t *isym; |
| 154 |
|
| 155 |
if (objtype == AGINEDGE) |
| 156 |
objtype = AGEDGE; |
| 157 |
if ((isym = find_isym(g, objtype, id))) { |
| 158 |
dtdelete(g->clos->lookup_by_name[objtype], isym); |
| 159 |
dtdelete(g->clos->lookup_by_id[objtype], isym); |
| 160 |
agstrfree(g, isym->str, false); |
| 161 |
free(isym); |
| 162 |
return true; |
| 163 |
} |
| 164 |
return false; |
| 165 |
} |
| 166 |
|
| 167 |
void aginternalmapclearlocalnames(Agraph_t * g) |
| 168 |
{ |
| 169 |
int i; |
| 170 |
IMapEntry_t *sym, *nxt; |
| 171 |
Dict_t **d_name; |
| 172 |
|
| 173 |
d_name = g->clos->lookup_by_name; |
| 174 |
for (i = 0; i < 3; i++) { |
| 175 |
if (d_name[i]) { |
| 176 |
for (sym = dtfirst(d_name[i]); sym; sym = nxt) { |
| 177 |
nxt = dtnext(d_name[i], sym); |
| 178 |
if (sym->str[0] == LOCALNAMEPREFIX) |
| 179 |
aginternalmapdelete(g, i, sym->id); |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
static void closeit(Dict_t ** d) |
| 186 |
{ |
| 187 |
int i; |
| 188 |
|
| 189 |
for (i = 0; i < 3; i++) { |
| 190 |
if (d[i]) { |
| 191 |
dtclose(d[i]); |
| 192 |
d[i] = NULL; |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
void aginternalmapclose(Agraph_t * g) |
| 198 |
{ |
| 199 |
closeit(g->clos->lookup_by_name); |
| 200 |
closeit(g->clos->lookup_by_id); |
| 201 |
} |