WiiFlow_Lite/source/list/cache.cpp

61 lines
1.3 KiB
C++
Raw Normal View History

2012-01-21 21:57:41 +01:00
#include "cache.hpp"
CCache::CCache(vector<dir_discHdr> &list, string path, CMode mode)
2012-01-21 21:57:41 +01:00
{
filename = path;
//gprintf("Opening DB: %s\n", filename.c_str());
2012-01-21 21:57:41 +01:00
cache = fopen(filename.c_str(), io[mode]);
if(!cache) return;
switch(mode)
{
case LOAD:
LoadAll(list);
break;
case SAVE:
SaveAll(list);
break;
default:
return;
}
}
2012-08-16 16:09:02 +02:00
CCache::~CCache()
2012-01-21 21:57:41 +01:00
{
//gprintf("Closing DB: %s\n", filename.c_str());
if(cache) fclose(cache);
cache = NULL;
}
2012-08-16 16:09:02 +02:00
void CCache::SaveAll(vector<dir_discHdr> list)
2012-01-21 21:57:41 +01:00
{
//gprintf("Updating DB: %s\n", filename.c_str());
if(!cache) return;
2012-08-16 16:09:02 +02:00
fwrite((void *)&list[0], 1, list.size() * sizeof(dir_discHdr), cache);
2012-01-21 21:57:41 +01:00
}
2012-08-16 16:09:02 +02:00
void CCache::LoadAll(vector<dir_discHdr> &list)
2012-01-21 21:57:41 +01:00
{
if(!cache) return;
//gprintf("Loading DB: %s\n", filename.c_str());
2012-08-16 16:09:02 +02:00
dir_discHdr tmp;
2012-01-21 21:57:41 +01:00
fseek(cache, 0, SEEK_END);
u64 fileSize = ftell(cache);
fseek(cache, 0, SEEK_SET);
2012-08-16 16:09:02 +02:00
u32 count = (u32)(fileSize / sizeof(dir_discHdr));
2012-01-21 21:57:41 +01:00
list.reserve(count + list.size());
for(u32 i = 0; i < count; i++)
{
//gprintf("Fetching Item number %u in DB: %s\n", index, filename.c_str());
fseek(cache, i * sizeof(dir_discHdr), SEEK_SET);
fread((void *)&tmp, 1, sizeof(dir_discHdr), cache);
//gprintf("Path %s\n", tmp.path);
2012-01-21 21:57:41 +01:00
list.push_back(tmp);
}
}