Graphviz 14.1.2~dev.20260118.1035
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 "config.h"
16
17#include <cgraph/cghdr.h>
18#include <stdbool.h>
19#include <stdlib.h>
20#include <util/alloc.h>
21
28
29static 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 */
49static 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
67 0, /* object ptr is passed as key */
68 0, /* size (ignored) */
69 offsetof(IMapEntry_t, namedict_link),
70 NULL,
71 NULL,
73};
74
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
84bool 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 */
105void 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] =
119 if ((d_id_to_name = g->clos->lookup_by_id[objtype]) == NULL)
120 d_id_to_name = g->clos->lookup_by_id[objtype] =
122 dtinsert(d_name_to_id, ent);
123 dtinsert(d_id_to_name, ent);
124}
125
126static 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
141char *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
151int 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
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
185static 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
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:184
#define dtinsert(d, o)
Definition cdt.h:186
CDT_API Dtmethod_t * Dttree
Definition dttree.c:310
CDT_API int dtclose(Dt_t *)
Definition dtclose.c:10
#define dtdelete(d, o)
Definition cdt.h:187
#define dtnext(d, o)
Definition cdt.h:181
#define dtfirst(d)
Definition cdt.h:180
cgraph.h additions
Dict_t * agdtopen(Dtdisc_t *disc, Dtmethod_t *method)
Definition utils.c:23
#define LOCALNAMEPREFIX
Definition cghdr.h:46
void free(void *)
node NULL
Definition grammar.y:181
void aginternalmapclearlocalnames(Agraph_t *g)
Definition imap.c:167
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 *, bool is_html)
Definition refstr.c:417
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:401
char * agstrbind(Agraph_t *g, const char *)
Definition refstr.c:341
static uint64_t id
Definition gv2gml.c:42
textitem scanner parser str
Definition htmlparse.y:218
void aginternalmapclose(Agraph_t *g)
Definition imap.c:197
int aginternalmapdelete(Agraph_t *g, int objtype, IDTYPE id)
Definition imap.c:151
void aginternalmapinsert(Agraph_t *g, int objtype, char *str, IDTYPE id)
Definition imap.c:105
static int namecmpf(void *arg_p0, void *arg_p1)
Definition imap.c:49
static Dtdisc_t LookupById
Definition imap.c:75
char * aginternalmapprint(Agraph_t *g, int objtype, IDTYPE id)
Definition imap.c:141
bool aginternalmaplookup(Agraph_t *g, int objtype, char *str, IDTYPE *result)
Definition imap.c:84
struct IMapEntry_s IMapEntry_t
static IMapEntry_t * find_isym(Agraph_t *g, int objtype, IDTYPE id)
Definition imap.c:126
static Dtdisc_t LookupByName
Definition imap.c:66
static void closeit(Dict_t **d)
Definition imap.c:185
static int idcmpf(void *arg_p0, void *arg_p1)
Definition imap.c:29
Dict_t * lookup_by_id[3]
Definition cgraph.h:417
Dict_t * lookup_by_name[3]
Definition cgraph.h:416
graph or subgraph
Definition cgraph.h:424
Agclos_t * clos
shared resources
Definition cgraph.h:434
Dtlink_t iddict_link
Definition imap.c:24
IDTYPE id
Definition imap.c:25
Dtlink_t namedict_link
Definition imap.c:23
char * str
Definition imap.c:26
Definition cdt.h:98