diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2011-03-02 22:35:20 +0100 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2011-03-02 22:35:20 +0100 |
commit | 7167b1c4ce914facaa14cd6db8cf7219b622da97 (patch) | |
tree | abc81a74ec51f953b5eec25a086a82287609ddbe /cache.cpp | |
parent | 0c2f90ee5c713fcb3aedb236fcebe7dd6d323ba3 (diff) | |
parent | 6e7b8f94bf7fdc087cd1eed604eabed6070dffad (diff) |
Merge branch 'master' into cache_range
Conflicts:
music.cpp
Diffstat (limited to 'cache.cpp')
-rw-r--r-- | cache.cpp | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/cache.cpp b/cache.cpp new file mode 100644 index 0000000..92e0cfa --- /dev/null +++ b/cache.cpp @@ -0,0 +1,47 @@ +#include "cache.h" +#include "config.h" + +#include <boost/format.hpp> +#include <boost/filesystem/fstream.hpp> +#include <openssl/sha.h> + +#include <iostream> +#include <stdexcept> + +EncodedCache::EncodedCache(fs::path path, Decoder::p decoder, Encoder::p encoder) { + this->decoder = decoder; + this->encoder = encoder; + + // TODO: Differentiate between encoders? + SHA_CTX context; + unsigned char md[SHA_DIGEST_LENGTH]; + SHA_Init(&context); + SHA_Update(&context, (unsigned char*)path.string().c_str(), path.string().length()); + SHA_Final(md, &context); + for(int i = 0; i < SHA_DIGEST_LENGTH; i++) { + hash = hash + (boost::format("%02x") % (int)md[i]).str(); + } +} + +fs::path EncodedCache::get_path() { + return fs::path(config::vm["audist.cache_dir"].as<std::string>()) / hash; +} + +void EncodedCache::create_cache() { + fs::path path = get_path(); + + // TODO: Locking? + fs::ofstream os(path, std::ios::out | std::ios::binary); + // TODO: Make an encoder-to-istream adapter to get rid of this: + + if(!os.is_open()) + throw std::runtime_error("failed to create cache object"); + char data[0x10000]; + std::streamsize size = 1; + while(size) { + size = encoder->read(data, 0x10000); + if(size > 0) + os.write(data, size); + } + os.close(); +} |