+/* $Id$
+
+This file is part of builder
+Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#include <fstream>
+#include <msp/path/utils.h>
+#include <msp/strings/utils.h>
+#include "builder.h"
+#include "dependencycache.h"
+#include "package.h"
+
+using namespace std;
+using namespace Msp;
+
+DependencyCache::DependencyCache(Package &p):
+ package(p),
+ changed(false)
+{ }
+
+const StringList &DependencyCache::get_deps(const string &tgt) const
+{
+ DepsMap::const_iterator i=deps.find(tgt);
+ if(i==deps.end())
+ {
+ static StringList dummy;
+ return dummy;
+ }
+ return i->second;
+}
+
+void DependencyCache::set_deps(const string &tgt, const StringList &d)
+{
+ deps[tgt]=d;
+ changed=true;
+}
+
+/**
+Saves the depencency cache. If there are no cached dependencies or they
+haven't been changed, does nothing.
+*/
+void DependencyCache::save() const
+{
+ if(deps.empty() || !changed || package.get_builder().get_dry_run())
+ return;
+
+ ofstream out((package.get_source()/".deps").str().c_str());
+ if(!out)
+ return;
+
+ for(DepsMap::const_iterator i=deps.begin(); i!=deps.end(); ++i)
+ {
+ out<<i->first;
+ for(StringList::const_iterator j=i->second.begin(); j!=i->second.end(); ++j)
+ out<<'|'<<*j;
+ out<<'\n';
+ }
+}
+
+void DependencyCache::load()
+{
+ string fn=(package.get_source()/".deps").str();
+ ifstream in(fn.c_str());
+ if(!in)
+ return;
+
+ string line;
+ while(getline(in, line))
+ {
+ vector<string> parts=split(line, '|');
+ deps[parts[0]]=StringList(parts.begin()+1, parts.end());
+ }
+
+ struct stat st;
+ Path::stat(fn, st);
+ mtime=Time::TimeStamp::from_unixtime(st.st_mtime);
+}