File: attribs.c

package info (click to toggle)
graphviz 1.7.16-2
  • links: PTS
  • area: non-free
  • in suites: woody
  • size: 11,124 kB
  • ctags: 12,650
  • sloc: ansic: 131,002; sh: 7,483; makefile: 1,954; tcl: 1,760; yacc: 1,758; perl: 253; awk: 150; lex: 96
file content (289 lines) | stat: -rw-r--r-- 6,481 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
    This software may only be used by you under license from AT&T Corp.
    ("AT&T").  A copy of AT&T's Source Code Agreement is available at
    AT&T's Internet website having the URL:
    <http://www.research.att.com/sw/tools/graphviz/license/source.html>
    If you received this software without first entering into a license
    with AT&T, you have an infringing copy of this software and cannot use
    it without violating AT&T's intellectual property rights.
*/
#pragma prototyped

#define EXTERN
#include "libgraph.h"

#ifdef DMALLOC
#include "dmalloc.h"
#endif

Agdict_t *agdictof(void* obj)
{
	Agdict_t		*d = NULL;

	switch (TAG_OF(obj)) {
		case TAG_GRAPH:	d = ((Agraph_t*)obj)->univ->globattr; break;
		case TAG_NODE:	d = ((Agnode_t*)obj)->graph->univ->nodeattr; break;
		case TAG_EDGE:	d = ((Agedge_t*)obj)->tail->graph->univ->edgeattr; break;
	}
	return d;
}

Agsym_t *agNEWsym(Agdict_t* dict, char* name, char* value)
{
	Agsym_t		*a;
	int			i;

	a			=	NEW(Agsym_t);
	a->name		=	agstrdup(name);
	a->value	=	agstrdup(value);
	a->printed	=	TRUE;
	i = a->index	=	dtsize(dict->dict);
	dict->list	=	ALLOC(i + 2, dict->list, Agsym_t*);
	dict->list[i++] = a;
	dict->list[i++] = NULL;
	dtinsert(dict->dict,a);
	return a;
}

static void obj_init_attr(void* obj, Agsym_t* attr)
{
	int			i;
	Agraph_t	*gobj;	/* generic object */

	gobj = (Agraph_t*) obj;
	i = attr->index;
	gobj->attr = ALLOC(i + 1, gobj->attr, char*);
	gobj->attr[i] = agstrdup(attr->value);
}

static void add_graph_attr(Agraph_t* g, Agsym_t* attr)
{
	Agnode_t		*n;

	if (g->meta_node) {
		for (n = agfstnode(g->meta_node->graph); n;
			n = agnxtnode(g->meta_node->graph,n))
				obj_init_attr(agusergraph(n),attr);
	}
	else obj_init_attr(g,attr);
}

static void add_node_attr(Agraph_t* g, Agsym_t* attr)
{
	Agnode_t		*n;
	Agproto_t		*proto;

	for (n = agfstnode(g); n; n = agnxtnode(g,n)) obj_init_attr(n,attr);
	if (g->meta_node) {
		for (n = agfstnode(g->meta_node->graph); n;
			 n = agnxtnode(g->meta_node->graph,n))
				for (proto = agusergraph(n)->proto; proto; proto = proto->prev)
					obj_init_attr(proto->n,attr);
	}
	else for (proto = g->proto; proto; proto = proto->prev)
		obj_init_attr(proto->n,attr);
}

static void add_edge_attr(Agraph_t* g, Agsym_t* attr)
{
	Agnode_t		*n;
	Agedge_t		*e;
	Agproto_t		*proto;

	for (n = agfstnode(g); n; n = agnxtnode(g,n))
		for (e = agfstout(g,n); e; e = agnxtout(g,e))
			obj_init_attr(e,attr);
	if (g->meta_node) {
		for (n = agfstnode(g->meta_node->graph); n;
			n = agnxtnode(g->meta_node->graph,n))
				for (proto = agusergraph(n)->proto; proto; proto = proto->prev)
					obj_init_attr(proto->e,attr);
	}
	else for (proto = g->proto; proto; proto = proto->prev)
		obj_init_attr(proto->e,attr);
}

static Agsym_t *dcl_attr(void* obj, char* name, char* value)
{
    Agsym_t   *rv;
	
	rv = agfindattr(obj, name);
    if (rv) {
        if (strcmp(rv->value,value)) {
			fprintf(stderr,"%s %s %s\n",name,value,rv->value);
            agerror("%s may not be redefined",name);
        }
		return rv;
    }
    rv = agNEWsym(agdictof(obj),name,value);
    if (rv) {
        switch (TAG_OF(obj)) {
            case TAG_GRAPH: add_graph_attr((Agraph_t*)obj,rv);			break;
            case TAG_NODE : add_node_attr(((Agnode_t*)obj)->graph,rv);	break;
            case TAG_EDGE : add_edge_attr(((Agedge_t*)obj)->head->graph,rv);break;
        }
    }
    return rv;
}

static void initproto(void)
{
	Agsym_t		*a;
	Agraph_t	*g;
	g = AG.proto_g = agopen("ProtoGraph",AGRAPH);
	a = dcl_attr(g->proto->e,KEY_ID,"");
	if (a->index != KEYX) abort();
	a = dcl_attr(g->proto->e,TAIL_ID,"");
	if (a->index != TAILX) abort();
	a->printed = FALSE;
	a = dcl_attr(g->proto->e,HEAD_ID,"");
	if (a->index != HEADX) abort();
	a->printed = FALSE;
}

Agsym_t *agraphattr(Agraph_t* g, char* name, char* value)
{
	if (g == NULL) g = AG.proto_g;
	if (g != g->root) return NULL;
	return dcl_attr(g,name,value);
}

Agsym_t *agnodeattr (Agraph_t* g, char* name, char* value)
{
	if (g == NULL) g = AG.proto_g;
	if (g != g->root) return NULL;
	return dcl_attr(g->proto->n,name,value);
}

Agsym_t * agedgeattr(Agraph_t* g, char* name, char* value)
{
	if (g == NULL) g = AG.proto_g;
	if (g != g->root) return NULL;
	return dcl_attr(g->proto->e,name,value);
}

/* attribute dictionaries */

static void agfreesym(void* ptr)
{
	Agsym_t		 *a;
	a = (Agsym_t*)ptr;
	agstrfree(a->name);
	agstrfree(a->value);
	free(a);
}

void agFREEdict(Agraph_t* g, Agdict_t* dict)
{
	int		i;
	Agsym_t		*a;

	g = g;
	dtclose(dict->dict);
	if (dict->list) {
		i = 0;
		while (a=dict->list[i++])
			agfreesym(a);
		free(dict->list);
	}
	free(dict);
}

Agdict_t * agNEWdict(char* name)
{
	Agdict_t	*dict;
	static Dtdisc_t symdisc = {
		offsetof(Agsym_t,name),		/* key */
		-1,							/* size */
		-1,							/* link */
		(Dtmake_f)0,
		(Dtfree_f)0,
		(Dtcompar_f)0,			/* use strcmp */
		(Dthash_f)0,
		(Dtmemory_f)0,
		(Dtevent_f)0
	};

	dict	= NEW(Agdict_t);
	dict->name = name;
	dict->dict = dtopen(&symdisc,Dttree);
	dict->list = NULL;
	return dict;
}

void agcopydict(Agdict_t* to_dict, Agdict_t* from_dict)
{
	int			i,n;
	Agsym_t		*a,*b;

	n = dtsize(from_dict->dict);
	for (i = 0; i < n; i++) {
		a = from_dict->list[i];
		b = agNEWsym(to_dict,a->name,a->value);
		b->printed = a->printed;
#ifdef DOS
		/* Microsoft C is a thing of wonder. */
fprintf(stderr,"", a->name, a->value);
#endif
	}
}

Agsym_t *agfindattr(void* obj, char* name)
{
	Agsym_t		*rv;
	Agdict_t	*dict	= agdictof(obj);

	rv = (Agsym_t*) dtmatch(dict->dict,name);
	return rv;
}

	/* this is normally called by the aginit() macro */
void aginitlib(int gs, int ns, int es)
{
	if (AG.proto_g == NULL) {
		AG.graph_nbytes = gs;
		AG.node_nbytes = ns;
		AG.edge_nbytes = es;
		AG.init_called = TRUE;
		initproto();
	}
	else
		if ((AG.graph_nbytes!=gs)||(AG.node_nbytes!=ns)||(AG.edge_nbytes!=es))
			fprintf(stderr,"aginit() called multiply with inconsistent args\n");
}

char *agget(void* obj, char* attr)
{
	return agxget(obj,agindex(obj,attr));
}

void agset(void* obj, char* attr, char* value)
{
	agxset(obj,agindex(obj,attr),value);
}

int agindex(void* obj, char* name)
{
	Agsym_t		*a;
	int			rv = -1;
	
	a = agfindattr(obj,name);
	if (a) rv = a->index;
	return rv;
}

char *agxget(void* obj, int index)
{
	if (index >= 0) return ((Agraph_t*)obj)->attr[index];
	return NULL;
}

void agxset(void* obj, int index, char* buf)
{
	char	**p;
	if (index >= 0) {
		p = ((Agraph_t*)obj)->attr;
		agstrfree(p[index]);
		p[index] = agstrdup(buf);
	}
}