]> git.tdb.fi Git - builder.git/blob - source/dependencycache.cpp
Convert all fstreams to IO::Files
[builder.git] / source / dependencycache.cpp
1 /* $Id$
2
3 This file is part of builder
4 Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/fs/stat.h>
9 #include <msp/io/except.h>
10 #include <msp/io/file.h>
11 #include <msp/io/print.h>
12 #include <msp/strings/utils.h>
13 #include "builder.h"
14 #include "dependencycache.h"
15 #include "sourcepackage.h"
16
17 using namespace std;
18 using namespace Msp;
19
20 DependencyCache::DependencyCache(SourcePackage &p):
21         package(p),
22         changed(false)
23 { }
24
25 const StringList &DependencyCache::get_deps(const string &tgt) const
26 {
27         DepsMap::const_iterator i=deps.find(tgt);
28         if(i==deps.end())
29                 throw KeyError("Unknown dependencies", tgt);
30
31         return i->second;
32 }
33
34 void DependencyCache::set_deps(const string &tgt, const StringList &d)
35 {
36         deps[tgt]=d;
37         changed=true;
38 }
39
40 /**
41 Saves the depencency cache.  If there are no cached dependencies or they
42 haven't been changed, does nothing.
43 */
44 void DependencyCache::save() const
45 {
46         if(deps.empty() || !changed || package.get_builder().get_dry_run())
47                 return;
48
49         IO::BufferedFile out((package.get_source()/".deps").str(), IO::M_WRITE);
50
51         for(DepsMap::const_iterator i=deps.begin(); i!=deps.end(); ++i)
52         {
53                 IO::print(out, i->first);
54                 for(StringList::const_iterator j=i->second.begin(); j!=i->second.end(); ++j)
55                         IO::print(out, "|%s", *j);
56                 IO::print(out, "\n");
57         }
58 }
59
60 void DependencyCache::load()
61 {
62         string fn=(package.get_source()/".deps").str();
63
64         try
65         {
66                 IO::BufferedFile in(fn);
67
68                 string line;
69                 while(in.getline(line))
70                 {
71                         vector<string> parts=split(line, '|');
72                         deps[parts[0]]=StringList(parts.begin()+1, parts.end());
73                 }
74
75                 mtime=Time::TimeStamp::from_unixtime(FS::stat(fn).st_mtime);
76         }
77         catch(const IO::FileNotFound &)
78         { }
79 }