nix-gh/src/nix-store/xmlgraph.cc
Eelco Dolstra c10c61449f Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.

Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 14:28:26 +01:00

72 lines
1.4 KiB
C++

#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";
}
}