2012-01-21 21:57:41 +01:00
|
|
|
#include "cache.hpp"
|
|
|
|
|
2012-08-16 16:09:02 +02:00
|
|
|
CCache::CCache(dir_discHdr &tmp, string path, u32 index, CMode mode) /* Load/Save One */
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
filename = path;
|
|
|
|
//gprintf("Openning DB: %s\n", filename.c_str());
|
|
|
|
|
|
|
|
cache = fopen(filename.c_str(), io[mode]);
|
|
|
|
if(!cache) return;
|
|
|
|
|
|
|
|
switch(mode)
|
|
|
|
{
|
|
|
|
case LOAD:
|
|
|
|
LoadOne(tmp, index);
|
|
|
|
break;
|
|
|
|
case SAVE:
|
|
|
|
SaveOne(tmp, index);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-16 16:09:02 +02:00
|
|
|
CCache::CCache(vector<dir_discHdr> &list, string path , CMode mode) /* Load/Save All */
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
filename = path;
|
2012-01-25 01:11:28 +01:00
|
|
|
//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(vector<dir_discHdr> &list, string path, dir_discHdr tmp, CMode mode) /* Add One */
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
filename = path;
|
|
|
|
//gprintf("Openning DB: %s\n", filename.c_str());
|
|
|
|
|
|
|
|
cache = fopen(filename.c_str(), io[mode]);
|
|
|
|
if(!cache) return;
|
|
|
|
|
|
|
|
switch(mode)
|
|
|
|
{
|
|
|
|
case ADD:
|
|
|
|
AddOne(list, tmp);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-16 16:09:02 +02:00
|
|
|
CCache::CCache(vector<dir_discHdr> &list, string path, u32 index, CMode mode) /* Remove One */
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
filename = path;
|
|
|
|
//gprintf("Openning DB: %s\n", filename.c_str());
|
|
|
|
|
|
|
|
cache = fopen(filename.c_str(), io[mode]);
|
|
|
|
if(!cache) return;
|
|
|
|
|
|
|
|
switch(mode)
|
|
|
|
{
|
|
|
|
case REMOVE:
|
|
|
|
RemoveOne(list, index);
|
|
|
|
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::SaveOne(dir_discHdr tmp, u32 index)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
//gprintf("Updating Item number %u in DB: %s\n", index, filename.c_str());
|
|
|
|
if(!cache) return;
|
2012-08-16 16:09:02 +02:00
|
|
|
fseek(cache, index * sizeof(dir_discHdr), SEEK_SET);
|
|
|
|
fwrite((void *)&tmp, 1, 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++)
|
|
|
|
{
|
|
|
|
LoadOne(tmp, i);
|
|
|
|
list.push_back(tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-16 16:09:02 +02:00
|
|
|
void CCache::LoadOne(dir_discHdr &tmp, u32 index)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
if(!cache) return;
|
|
|
|
|
|
|
|
//gprintf("Fetching Item number %u in DB: %s\n", index, filename.c_str());
|
2012-08-16 16:09:02 +02:00
|
|
|
fseek(cache, index * sizeof(dir_discHdr), SEEK_SET);
|
|
|
|
fread((void *)&tmp, 1, sizeof(dir_discHdr), cache);
|
2012-01-21 21:57:41 +01:00
|
|
|
//gprintf("Path %s\n", tmp.path);
|
|
|
|
}
|
|
|
|
|
2012-08-16 16:09:02 +02:00
|
|
|
void CCache::AddOne(vector<dir_discHdr> &list, dir_discHdr tmp)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
//gprintf("Adding Item number %u in DB: %s\n", list.size()+1, filename.c_str());
|
|
|
|
list.push_back(tmp);
|
|
|
|
|
|
|
|
if(!cache) return;
|
2012-08-16 16:09:02 +02:00
|
|
|
fwrite((void *)&tmp, 1, sizeof(dir_discHdr), cache); // FILE* is opened as "ab+" so its always written to the EOF.
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-08-16 16:09:02 +02:00
|
|
|
void CCache::RemoveOne(vector<dir_discHdr> &list, u32 index)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
//gprintf("Removing Item number %u in DB: %s\n", index, filename.c_str());
|
|
|
|
list.erase(list.begin() + index);
|
|
|
|
SaveAll(list);
|
|
|
|
}
|