X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcache.cpp;h=6baacce3f4884dd21bc8ced890471f5889c883c2;hb=d1f9551e05c9d341149eb490e05b1465d3d6b711;hp=087428fc3cf54cf8341deae68dcb4224b6f3613b;hpb=7ac882d9a3a025bbe77f88803f53eff5c6dad750;p=builder.git diff --git a/source/cache.cpp b/source/cache.cpp index 087428f..6baacce 100644 --- a/source/cache.cpp +++ b/source/cache.cpp @@ -1,5 +1,7 @@ #include +#include #include +#include #include #include #include @@ -57,13 +59,12 @@ void write_string(IO::Base &out, const string &str) Cache::Cache(SourcePackage &p): package(p), - filename(package.get_temp_dir()/"../cache"), - changed(false) + filename(package.get_temp_directory()/"../cache") { } void Cache::set_value(const Target *tgt, const string &k, const string &v) { - ValueList vl; + Values vl; vl.push_back(v); set_values(tgt, k, vl); } @@ -71,30 +72,30 @@ void Cache::set_value(const Target *tgt, const string &k, const string &v) void Cache::append_value(const Target *tgt, const string &k, const string &v) { Key key(tgt->get_name(), k); - DataMap::iterator i = data.find(key); + auto i = data.find(key); if(i==data.end()) - i = data.insert(DataMap::value_type(key, ValueList())).first; + i = data.insert({ key, Values() }).first; i->second.push_back(v); changed = true; - package.get_builder().get_logger().log("cache", format("Updated key %s %s+ %s", tgt->get_name(), k, v)); + package.get_builder().get_logger().log("cache", "Updated key %s %s+ %s", tgt->get_name(), k, v); } -void Cache::set_values(const Target *tgt, const string &k, const ValueList &v) +void Cache::set_values(const Target *tgt, const string &k, const Values &v) { data[Key(tgt->get_name(), k)] = v; changed = true; - package.get_builder().get_logger().log("cache", format("Updated key %s %s: %s", tgt->get_name(), k, join(v.begin(), v.end()))); + package.get_builder().get_logger().log("cache", "Updated key %s %s: %s", tgt->get_name(), k, join(v.begin(), v.end())); } const string &Cache::get_value(const Target *tgt, const string &k) { - const ValueList &values = get_values(tgt, k); + const Values &values = get_values(tgt, k); if(values.empty()) throw logic_error("values.empty()"); return values.front(); } -const Cache::ValueList &Cache::get_values(const Target *tgt, const string &k) +const Cache::Values &Cache::get_values(const Target *tgt, const string &k) { return get_item(data, Key(tgt->get_name(), k)); } @@ -108,7 +109,7 @@ void Cache::load() { if(FS::Stat st = FS::stat(filename)) { - package.get_builder().get_logger().log("files", format("Reading %s", filename)); + package.get_builder().get_logger().log("files", "Reading %s", filename); IO::BufferedFile in(filename.str()); while(!in.eof()) @@ -118,10 +119,10 @@ void Cache::load() key.second = read_string(in); if(key.first.empty() || key.second.empty()) break; - ValueList &values = data[key]; + Values &values = data[key]; for(unsigned count = read_count(in); count; --count) values.push_back(read_string(in)); - package.get_builder().get_logger().log("cache", format("Loaded key %s %s: %s", key.first, key.second, join(values.begin(), values.end()))); + package.get_builder().get_logger().log("cache", "Loaded key %s %s: %s", key.first, key.second, join(values.begin(), values.end())); } mtime = st.get_modify_time(); @@ -133,15 +134,20 @@ void Cache::save() const if(data.empty() || !changed) return; - package.get_builder().get_logger().log("files", format("Writing %s", filename)); + FS::Path dir = FS::dirname(filename); + if(!FS::exists(dir)) + FS::mkpath(dir, 0755); + package.get_builder().get_logger().log("files", "Writing %s", filename); IO::BufferedFile out(filename.str(), IO::M_WRITE); - for(DataMap::const_iterator i=data.begin(); i!=data.end(); ++i) + for(const auto &kvp: data) { - write_string(out, i->first.first); - write_string(out, i->first.second); - write_count(out, i->second.size()); - for(ValueList::const_iterator j=i->second.begin(); j!=i->second.end(); ++j) - write_string(out, *j); + write_string(out, kvp.first.first); + write_string(out, kvp.first.second); + write_count(out, kvp.second.size()); + for(const string &v: kvp.second) + write_string(out, v); } + + changed = false; }