summaryrefslogtreecommitdiff
path: root/filecache.cpp
blob: 9a21647fc2d74d14319e010450c5d6febb56c3ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <algorithm>
#include <fstream>
#include <istream>
#include <sys/stat.h>
#include "filecache.h"

namespace vdrlive {

std::time_t FileObject::get_filetime( std::string const& path )
{
	struct stat sbuf;
	if ( stat( path.c_str(), &sbuf ) < 0 )
		return 0; // XXX
	return sbuf.st_ctime;
}

bool FileObject::is_current() const
{
	return m_ctime < get_filetime( m_path );
	/*struct stat statBuf = { 0 };
 * 	if ( ::stat( m_path.c_str(), &statBuf ) < 0 ) {
 * 		}
	if ( m_ctime == 0 )
		return true;
	m_ctime = get_filetime( m_path );
	return timestamp < m_ctime;*/
}

bool FileObject::load()
{
	std::ifstream ifs( m_path.c_str(), std::ios::in | std::ios::binary | std::ios::ate );
	if ( !ifs )
		return false;

	std::streamsize size = ifs.tellg();
	ifs.seekg( 0, std::ios::beg );

	std::vector< char > data( size );
	data.resize( size );
	ifs.read( &data[0], size );
	ifs.close();

	m_ctime = get_filetime( m_path );
	m_data.swap( data );
	return true;
}

FileCache& LiveFileCache()
{
	static FileCache instance( 1000000 );
	return instance;
}

} // namespace vdrlive

using namespace vdrlive;

int main()
{
	FileCache::ptr_type f = LiveFileCache().get("/tmp/live/active.png");
}