1 #include <msp/core/maputils.h>
2 #include <msp/fs/dir.h>
3 #include <msp/fs/stat.h>
4 #include <msp/fs/utils.h>
5 #include <msp/io/file.h>
6 #include <msp/io/print.h>
7 #include <msp/strings/utils.h>
10 #include "sourcepackage.h"
18 unsigned read_count(IO::Base &in)
20 unsigned char head = in.get();
23 unsigned char tail = in.get();
24 return (head&0x3F)<<8 | tail;
30 string read_string(IO::Base &in)
32 unsigned len = read_count(in);
34 len = in.read(buf, len);
35 return string(buf, len);
38 void write_count(IO::Base &out, unsigned count)
44 out.put(0xC0 | (count>>8));
48 throw invalid_argument("write_count");
51 void write_string(IO::Base &out, const string &str)
53 write_count(out, str.size());
60 Cache::Cache(SourcePackage &p):
62 filename(package.get_temp_directory()/"../cache"),
66 void Cache::set_value(const Target *tgt, const string &k, const string &v)
70 set_values(tgt, k, vl);
73 void Cache::append_value(const Target *tgt, const string &k, const string &v)
75 Key key(tgt->get_name(), k);
76 DataMap::iterator i = data.find(key);
78 i = data.insert(DataMap::value_type(key, ValueList())).first;
79 i->second.push_back(v);
81 package.get_builder().get_logger().log("cache", format("Updated key %s %s+ %s", tgt->get_name(), k, v));
84 void Cache::set_values(const Target *tgt, const string &k, const ValueList &v)
86 data[Key(tgt->get_name(), k)] = v;
88 package.get_builder().get_logger().log("cache", format("Updated key %s %s: %s", tgt->get_name(), k, join(v.begin(), v.end())));
91 const string &Cache::get_value(const Target *tgt, const string &k)
93 const ValueList &values = get_values(tgt, k);
95 throw logic_error("values.empty()");
96 return values.front();
99 const Cache::ValueList &Cache::get_values(const Target *tgt, const string &k)
101 return get_item(data, Key(tgt->get_name(), k));
104 bool Cache::has_key(const Target *tgt, const string &k)
106 return data.count(Key(tgt->get_name(), k));
111 if(FS::Stat st = FS::stat(filename))
113 package.get_builder().get_logger().log("files", format("Reading %s", filename));
114 IO::BufferedFile in(filename.str());
119 key.first = read_string(in);
120 key.second = read_string(in);
121 if(key.first.empty() || key.second.empty())
123 ValueList &values = data[key];
124 for(unsigned count = read_count(in); count; --count)
125 values.push_back(read_string(in));
126 package.get_builder().get_logger().log("cache", format("Loaded key %s %s: %s", key.first, key.second, join(values.begin(), values.end())));
129 mtime = st.get_modify_time();
133 void Cache::save() const
135 if(data.empty() || !changed)
138 FS::Path dir = FS::dirname(filename);
140 FS::mkpath(dir, 0755);
141 package.get_builder().get_logger().log("files", format("Writing %s", filename));
142 IO::BufferedFile out(filename.str(), IO::M_WRITE);
144 for(DataMap::const_iterator i=data.begin(); i!=data.end(); ++i)
146 write_string(out, i->first.first);
147 write_string(out, i->first.second);
148 write_count(out, i->second.size());
149 for(ValueList::const_iterator j=i->second.begin(); j!=i->second.end(); ++j)
150 write_string(out, *j);