Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
imap.c
Go to the documentation of this file.
1
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 <cgraph/cghdr.h>
16#include <stdbool.h>
17#include <stdlib.h>
18#include <util/alloc.h>
19
26
27static int idcmpf(void *arg_p0, void *arg_p1) {
28 IMapEntry_t *p0 = arg_p0;
29 IMapEntry_t *p1 = arg_p1;
30 if (p0->id > p1->id)
31 {
32 return 1;
33 }
34 else if (p0->id < p1->id)
35 {
36 return -1;
37 }
38 else
39 {
40 return 0;
41 }
42}
43
44/* note, OK to compare pointers into shared string pool
45 * but can't probe with an arbitrary string pointer
46 */
47static int namecmpf(void *arg_p0, void *arg_p1) {
48 IMapEntry_t *p0 = arg_p0;
49 IMapEntry_t *p1 = arg_p1;
50 if (p0->str > p1->str)
51 {
52 return 1;
53 }
54 else if (p0->str < p1->str)
55 {
56 return -1;
57 }
58 else
59 {
60 return 0;
61 }
62}
63
65 0, /* object ptr is passed as key */
66 0, /* size (ignored) */
67 offsetof(IMapEntry_t, namedict_link),
68 NULL,
69 NULL,
71};
72
74 0, /* object ptr is passed as key */
75 0, /* size (ignored) */
76 offsetof(IMapEntry_t, iddict_link),
77 NULL,
78 NULL,
79 idcmpf,
80};
81
82bool aginternalmaplookup(Agraph_t *g, int objtype, char *str, IDTYPE *result) {
83 Dict_t *d;
84 IMapEntry_t *sym, template;
85 char *search_str;
86
87 if (objtype == AGINEDGE)
88 objtype = AGEDGE;
89 if ((d = g->clos->lookup_by_name[objtype])) {
90 if ((search_str = agstrbind(g, str))) {
91 template.str = search_str;
92 sym = dtsearch(d, &template);
93 if (sym) {
94 *result = sym->id;
95 return true;
96 }
97 }
98 }
99 return false;
100}
101
102/* caller GUARANTEES that this is a new entry */
103void aginternalmapinsert(Agraph_t * g, int objtype, char *str,
104 IDTYPE id)
105{
106 Dict_t *d_name_to_id, *d_id_to_name;
107
108 IMapEntry_t *ent = gv_alloc(sizeof(IMapEntry_t));
109 ent->id = id;
110 ent->str = agstrdup(g, str);
111
112 if (objtype == AGINEDGE)
113 objtype = AGEDGE;
114 if ((d_name_to_id = g->clos->lookup_by_name[objtype]) == NULL)
115 d_name_to_id = g->clos->lookup_by_name[objtype] =
117 if ((d_id_to_name = g->clos->lookup_by_id[objtype]) == NULL)
118 d_id_to_name = g->clos->lookup_by_id[objtype] =
120 dtinsert(d_name_to_id, ent);
121 dtinsert(d_id_to_name, ent);
122}
123
124static IMapEntry_t *find_isym(Agraph_t * g, int objtype, IDTYPE id)
125{
126 Dict_t *d;
127 IMapEntry_t *isym, itemplate;
128
129 if (objtype == AGINEDGE)
130 objtype = AGEDGE;
131 if ((d = g->clos->lookup_by_id[objtype])) {
132 itemplate.id = id;
133 isym = dtsearch(d, &itemplate);
134 } else
135 isym = NULL;
136 return isym;
137}
138
139char *aginternalmapprint(Agraph_t * g, int objtype, IDTYPE id)
140{
141 IMapEntry_t *isym;
142
143 if ((isym = find_isym(g, objtype, id)))
144 return isym->str;
145 return NULL;
146}
147
148
149int aginternalmapdelete(Agraph_t * g, int objtype, IDTYPE id)
150{
151 IMapEntry_t *isym;
152
153 if (objtype == AGINEDGE)
154 objtype = AGEDGE;
155 if ((isym = find_isym(g, objtype, id))) {
156 dtdelete(g->clos->lookup_by_name[objtype], isym);
157 dtdelete(g->clos->lookup_by_id[objtype], isym);
158 agstrfree(g, isym->str);
159 free(isym);
160 return true;
161 }
162 return false;
163}
164
166{
167 int i;
168 IMapEntry_t *sym, *nxt;
169 Dict_t **d_name;
170
171 Ag_G_global = g;
172 d_name = g->clos->lookup_by_name;
173 for (i = 0; i < 3; i++) {
174 if (d_name[i]) {
175 for (sym = dtfirst(d_name[i]); sym; sym = nxt) {
176 nxt = dtnext(d_name[i], sym);
177 if (sym->str[0] == LOCALNAMEPREFIX)
178 aginternalmapdelete(g, i, sym->id);
179 }
180 }
181 }
182}
183
184static void closeit(Dict_t ** d)
185{
186 int i;
187
188 for (i = 0; i < 3; i++) {
189 if (d[i]) {
190 dtclose(d[i]);
191 d[i] = NULL;
192 }
193 }
194}
195
Memory allocation wrappers that exit on failure.
static void * gv_alloc(size_t size)
Definition alloc.h:47
#define dtsearch(d, o)
Definition cdt.h:183
#define dtinsert(d, o)
Definition cdt.h:185
CDT_API Dtmethod_t * Dttree
Definition dttree.c:308
CDT_API int dtclose(Dt_t *)
Definition dtclose.c:8
#define dtdelete(d, o)
Definition cdt.h:186
#define dtnext(d, o)
Definition cdt.h:180
#define dtfirst(d)
Definition cdt.h:179
cgraph.h additions
Dict_t * agdtopen(Agraph_t *g, Dtdisc_t *disc, Dtmethod_t *method)
Definition utils.c:21
Agraph_t * Ag_G_global
Definition graph.c:24
#define LOCALNAMEPREFIX
Definition cghdr.h:46
void free(void *)
node NULL
Definition grammar.y:163
void aginternalmapclearlocalnames(Agraph_t *g)
Definition imap.c:165
uint64_t IDTYPE
unique per main graph ID
Definition cgraph.h:73
@ AGEDGE
Definition cgraph.h:207
@ AGINEDGE
Definition cgraph.h:207
int agstrfree(Agraph_t *, const char *)
Definition refstr.c:139
char * agstrdup(Agraph_t *, const char *)
returns a pointer to a reference-counted copy of the argument string, creating one if necessary
Definition refstr.c:131
char * agstrbind(Agraph_t *g, const char *)
Definition refstr.c:96
static uint64_t id
Definition gv2gml.c:42
textitem scanner parser str
Definition htmlparse.y:224
void aginternalmapclose(Agraph_t *g)
Definition imap.c:196
int aginternalmapdelete(Agraph_t *g, int objtype, IDTYPE id)
Definition imap.c:149
void aginternalmapinsert(Agraph_t *g, int objtype, char *str, IDTYPE id)
Definition imap.c:103
static int namecmpf(void *arg_p0, void *arg_p1)
Definition imap.c:47
static Dtdisc_t LookupById
Definition imap.c:73
char * aginternalmapprint(Agraph_t *g, int objtype, IDTYPE id)
Definition imap.c:139
bool aginternalmaplookup(Agraph_t *g, int objtype, char *str, IDTYPE *result)
Definition imap.c:82
struct IMapEntry_s IMapEntry_t
static IMapEntry_t * find_isym(Agraph_t *g, int objtype, IDTYPE id)
Definition imap.c:124
static Dtdisc_t LookupByName
Definition imap.c:64
static void closeit(Dict_t **d)
Definition imap.c:184
static int idcmpf(void *arg_p0, void *arg_p1)
Definition imap.c:27
Dict_t * lookup_by_id[3]
Definition cgraph.h:418
Dict_t * lookup_by_name[3]
Definition cgraph.h:417
graph or subgraph
Definition cgraph.h:425
Agclos_t * clos
shared resources
Definition cgraph.h:435
Dtlink_t iddict_link
Definition imap.c:22
IDTYPE id
Definition imap.c:23
Dtlink_t namedict_link
Definition imap.c:21
char * str
Definition imap.c:24
Definition cdt.h:100