1 /*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
11 #include <cgraph/cghdr.h>
16 * reference counted strings.
21 uint64_t refcnt: sizeof(uint64_t) * 8 - 1;
24 char store[1]; /* this is actually a dynamic array */
27 static Dtdisc_t Refstrdisc = {
28 offsetof(refstr_t, s), /* key */
39 static Dict_t *Refdict_default;
42 * Return the string dictionary associated with g.
43 * If necessary, create it.
44 * As a side-effect, set html masks. This assumes 8-bit bytes.
46 static Dict_t *refdict(Agraph_t * g)
51 dictref = &(g->clos->strdict);
53 dictref = &Refdict_default;
54 if (*dictref == NULL) {
55 *dictref = agdtopen(g, &Refstrdisc, Dttree);
60 int agstrclose(Agraph_t * g)
62 return agdtclose(g, refdict(g));
65 static refstr_t *refsymbind(Dict_t * strdict, char *s)
69 r = dtsearch(strdict, &key);
73 static char *refstrbind(Dict_t * strdict, char *s)
76 r = refsymbind(strdict, s);
83 char *agstrbind(Agraph_t * g, char *s)
85 return refstrbind(refdict(g), s);
88 char *agstrdup(Agraph_t * g, char *s)
97 r = refsymbind(strdict, s);
101 sz = sizeof(refstr_t) + strlen(s);
110 dtinsert(strdict, r);
115 char *agstrdup_html(Agraph_t * g, char *s)
123 strdict = refdict(g);
124 r = refsymbind(strdict, s);
128 sz = sizeof(refstr_t) + strlen(s);
137 dtinsert(strdict, r);
142 int agstrfree(Agraph_t * g, char *s)
150 strdict = refdict(g);
151 r = refsymbind(strdict, s);
152 if (r && (r->s == s)) {
154 if (r->refcnt == 0) {
155 agdtdelete(g, strdict, r);
164 * Return true if s is an HTML string.
165 * We assume s points to the datafield store[0] of a refstr.
167 int aghtmlstr(char *s)
173 key = (refstr_t *) (s - offsetof(refstr_t, store[0]));
177 void agmarkhtmlstr(char *s)
183 key = (refstr_t *) (s - offsetof(refstr_t, store[0]));
188 static int refstrprint(Dict_t * dict, void *ptr, void *user)
195 fprintf(stderr, "%s\n", r->s);
199 void agrefstrdump(Agraph_t * g)
202 dtwalk(Refdict_default, refstrprint, 0);