From 6e7b8f94bf7fdc087cd1eed604eabed6070dffad Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Wed, 2 Mar 2011 19:22:33 +0100 Subject: Implemented simple caching for transcoded audio data. --- cache.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 cache.cpp (limited to 'cache.cpp') 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 +#include +#include + +#include +#include + +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()) / 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(); +} -- cgit v1.2.3