3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2013 Jonathan Neuschäfer <j.neuschaefer@gmx.net>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "filecache.h"
23 #include "network/networkprotocol.h"
31 bool FileCache::loadByPath(const std::string &path, std::ostream &os)
33 std::ifstream fis(path.c_str(), std::ios_base::binary);
36 verbosestream<<"FileCache: File not found in cache: "
45 std::streamsize len = fis.gcount();
55 errorstream<<"FileCache: Failed to read file from cache: \""
56 <<path<<"\""<<std::endl;
62 bool FileCache::updateByPath(const std::string &path, const std::string &data)
64 std::ofstream file(path.c_str(), std::ios_base::binary |
65 std::ios_base::trunc);
69 errorstream<<"FileCache: Can't write to file at "
74 file.write(data.c_str(), data.length());
80 bool FileCache::update(const std::string &name, const std::string &data)
82 std::string path = m_dir + DIR_DELIM + name;
83 return updateByPath(path, data);
85 bool FileCache::load(const std::string &name, std::ostream &os)
87 std::string path = m_dir + DIR_DELIM + name;
88 return loadByPath(path, os);