4:36
      #ˆ¾‘äÇò 8à_ho³$eŸÑƒh¹ÐÁÍ      #128
  E   %ˆl—Ým_J¥2ÛB‚ šP.ÜmËŒº¦-ÿ�þZ}¦–p22ô�ºë2´çÒ„Î¶çÑ_‹I|¥�+ýÿ´XOÃÏ ?÷     %/* $Id$ $Revision$ */
/* vim:set shiftwidth=4 ts=8: */

/*************************************************************************
 * Copyright (c) 2011 AT&T Intellectual Property 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors: See CVS logs. Details at http://www.graphviz.org/
 *************************************************************************/

#include <string.h>
#include <stdlib.h>
#include "gvc.h"

extern "C" {
extern void gv_string_writer_init(GVC_t *gvc);
extern void gv_channel_writer_init(GVC_t *gvc);
extern void gv_writer_reset(GVC_t *gvc);
}

#define agfindattr(x,s) agattrsym(x,s)
#define agraphattr(g,n,s) agattr(g,AGRAPH,n,s)
#define agnodeattr(g,n,s) agattr(g,AGNODE,n,s)
#define agedgeattr(g,n,s) agattr(g,AGEDGE,n,s)

static char emptystring[] = {'\0'};

static GVC_t *gvc;

static void gv_init(void) {
    /* list of builtins, enable demand loading */
    gvc = gvContextPlugins(lt_preloaded_symbols, DEMAND_LOADING);
}

Agraph_t *graph(char *name)
{
    if (!gvc)
        gv_init();
    return agopen(name, Agundirected, 0);
}

Agraph_t *digraph(char *name)
{
    if (!gvc)
        gv_init();
    return agopen(name, Agdirected, 0);
}

Agraph_t *strictgraph(char *name)
{
    if (!gvc)
        gv_init();
    return agopen(name, Agstrictundirected, 0);
}

Agraph_t *strictdigraph(char *name)
{
    if (!gvc)
        gv_init();
    return agopen(name, Agstrictdirected, 0);
}

Agraph_t *readstring(char *string)
{
    if (!gvc)
        gv_init();
    return agmemread(string);
}

Agraph_t *read(FILE *f)
{
    if (!gvc)
        gv_init();
    return agread(f, NULL);
}

Agraph_t *read(const char *filename)
{
    FILE *f;
    Agraph_t *g;

    f = fopen(filename, "r");
    if (!f)
        return NULL;
    if (!gvc)
        gv_init();
    g = agread(f, NULL);
    fclose(f);
    return g;
}

//-------------------------------------------------
Agraph_t *graph(Agraph_t *g, char *name)
{
    if (!gvc)
        gv_init();
    return agsubg(g, name, 1);
}

Agnode_t *node(Agraph_t *g, char *name)
{
    if (!gvc)
        return NULL;
    return agnode(g, name, 1);
}

Agedge_t *edge(Agraph_t* g, Agnode_t *t, Agnode_t *h)
{
    if (!gvc || !t || !h || !g)
        return NULL;
    // edges from/to the protonode are not permitted
    if (AGTYPE(t) == AGRAPH || AGTYPE(h) == AGRAPH)
	return NULL;
    return agedge(g, t, h, NULL, 1);
}

Agedge_t *edge(Agnode_t *t, Agnode_t *h)
{
    return edge(agraphof(t), t, h);
}

// induce tail if necessary
Agedge_t *edge(char *tname, Agnode_t *h)
{
    return edge(node(agraphof(h), tname), h);
}

// induce head if necessary
Agedge_t *edge(Agnode_t *t, char *hname)
{
    return edge(t, node(agraphof(t), hname));
}

// induce tail/head if necessary
Agedge_t *edge(Agraph_t *g, char *tname, char *hname)
{
    return edge(g, node(g, tname), node(g, hname));
}

//-------------------------------------------------
static char* myagxget(void *obj, Agsym_t *a)
{
    int len;
    char *val, *hs;

    if (!obj || !a)
        return emptystring;
    val = agxget(obj, a);
    if (!val)
        return emptystring;
    if (a->name[0] == 'l' && strcmp(a->name, "label") == 0 && aghtmlstr(val)) {
        len = strlen(val);
        hs = (char*)malloc(len + 3);
        hs[0] = '<';
        strcpy(hs+1, val);
        hs[len+1] = '>';
        hs[len+2] = '\0';
        return hs;
    }
    return val;
}
char *getv(Agraph_t *g, Agsym_t *a)
{
    return myagxget(g, a);
}
char *getv(Agraph_t *g, char *attr)
{
    Agsym_t *a;

    if (!g || !attr)
        return NULL;
    a = agfindattr(agroot(g), attr);
    return myagxget(g, a);
}
static void myagxset(void *obj, Agsym_t *a, char *val)
{
    int len;
    char *hs;

    if (a->name[0] == 'l' && val[0] == '<' && strcmp