about summary refs log tree commit diff
path: root/src/nix-store/xmlgraph.cc
blob: f88266bbbdedaa25f036dafa3e61c6e4381e8fbe (plain) (blame)
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
#include "xmlgraph.hh"
#include "util.hh"
#include "store-api.hh"

#include <iostream>


using std::cout;

namespace nix {


static inline const string & xmlQuote(const string & s)
{
    // Luckily, store paths shouldn't contain any character that needs to be
    // quoted.
    return s;
}


static string makeEdge(const string & src, const string & dst)
{
    format f = format("  <edge src=\"%1%\" dst=\"%2%\"/>\n")
      % xmlQuote(src) % xmlQuote(dst);
    return f.str();
}


static string makeNode(const string & id)
{
    format f = format("  <node name=\"%1%\"/>\n") % xmlQuote(id);
    return f.str();
}


void printXmlGraph(StoreAPI & store, const PathSet & roots)
{
    PathSet workList(roots);
    PathSet doneSet;

    cout << "<?xml version='1.0' encoding='utf-8'?>\n"
         << "<nix>\n";

    while (!workList.empty()) {
        Path path = *(workList.begin());
        workList.erase(path);

        if (doneSet.find(path) != doneSet.end()) continue;
        doneSet.insert(path);

        cout << makeNode(path);

        PathSet references;
        store.queryReferences(path, references);

        for (PathSet::iterator i = references.begin();
             i != references.end(); ++i)
        {
            if (*i != path) {
                workList.insert(*i);
                cout << makeEdge(*i, path);
            }
        }

    }

    cout << "</nix>\n";
}


}