2008-01-09 19:21:31 +02:00
|
|
|
#include "../stdafx.h"
|
2007-07-07 07:30:36 +03:00
|
|
|
#include "CSndHandler.h"
|
|
|
|
|
|
|
|
|
|
|
|
CSndHandler::~CSndHandler()
|
|
|
|
{
|
|
|
|
entries.clear();
|
2008-03-20 10:51:53 +02:00
|
|
|
fimap.clear();
|
2007-07-07 07:30:36 +03:00
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
CSndHandler::CSndHandler(std::string fname):CHUNK(65535)
|
|
|
|
{
|
|
|
|
file.open(fname.c_str(),std::ios::binary);
|
|
|
|
if (!file.is_open())
|
2008-07-16 21:26:15 +03:00
|
|
|
#ifndef __GNUC__
|
2007-07-07 07:30:36 +03:00
|
|
|
throw new std::exception((std::string("Cannot open ")+fname).c_str());
|
2008-07-16 21:26:15 +03:00
|
|
|
#else
|
|
|
|
throw new std::exception();
|
|
|
|
#endif
|
2007-07-07 07:30:36 +03:00
|
|
|
int nr = readNormalNr(0,4);
|
|
|
|
char tempc;
|
|
|
|
for (int i=0;i<nr;i++)
|
|
|
|
{
|
|
|
|
Entry entry;
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
file.read(&tempc,1);
|
|
|
|
if (tempc)
|
|
|
|
entry.name+=tempc;
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
entry.name+='.';
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
file.read(&tempc,1);
|
|
|
|
if (tempc)
|
|
|
|
entry.name+=tempc;
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
file.seekg(40-entry.name.length()-1,std::ios_base::cur);
|
|
|
|
entry.offset = readNormalNr(-1,4);
|
|
|
|
entry.size = readNormalNr(-1,4);
|
|
|
|
entries.push_back(entry);
|
2008-03-20 10:51:53 +02:00
|
|
|
fimap[entry.name] = i;
|
2007-07-07 07:30:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
int CSndHandler::readNormalNr (int pos, int bytCon)
|
|
|
|
{
|
|
|
|
if (pos>=0)
|
|
|
|
file.seekg(pos,std::ios_base::beg);
|
|
|
|
int ret=0;
|
|
|
|
int amp=1;
|
|
|
|
unsigned char zcz=0;
|
|
|
|
for (int i=0; i<bytCon; i++)
|
|
|
|
{
|
|
|
|
file.read((char*)(&zcz),1);
|
|
|
|
ret+=zcz*amp;
|
|
|
|
amp*=256;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
void CSndHandler::extract(int index, std::string dstfile) //saves selected file
|
|
|
|
{
|
|
|
|
std::ofstream out(dstfile.c_str(),std::ios_base::binary);
|
|
|
|
file.seekg(entries[index].offset,std::ios_base::beg);
|
|
|
|
int toRead=entries[index].size;
|
|
|
|
char * buffer = new char[std::min(CHUNK,entries[index].size)];
|
|
|
|
while (toRead>CHUNK)
|
|
|
|
{
|
|
|
|
file.read(buffer,CHUNK);
|
|
|
|
out.write(buffer,CHUNK);
|
|
|
|
toRead-=CHUNK;
|
|
|
|
}
|
|
|
|
file.read(buffer,toRead);
|
|
|
|
out.write(buffer,toRead);
|
|
|
|
out.close();
|
|
|
|
}
|
|
|
|
void CSndHandler::extract(std::string srcfile, std::string dstfile, bool caseSens) //saves selected file
|
|
|
|
{
|
|
|
|
if (caseSens)
|
|
|
|
{
|
2008-12-21 21:17:35 +02:00
|
|
|
for (size_t i=0;i<entries.size();++i)
|
2007-07-07 07:30:36 +03:00
|
|
|
{
|
|
|
|
if (entries[i].name==srcfile)
|
|
|
|
extract(i,dstfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::transform(srcfile.begin(),srcfile.end(),srcfile.begin(),tolower);
|
2008-12-21 21:17:35 +02:00
|
|
|
for (size_t i=0;i<entries.size();++i)
|
2007-07-07 07:30:36 +03:00
|
|
|
{
|
|
|
|
if (entries[i].name==srcfile)
|
|
|
|
{
|
|
|
|
std::string por = entries[i].name;
|
|
|
|
std::transform(por.begin(),por.end(),por.begin(),tolower);
|
|
|
|
if (por==srcfile)
|
|
|
|
extract(i,dstfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-07-31 08:24:10 +03:00
|
|
|
MemberFile CSndHandler::getFile(std::string name)
|
|
|
|
{
|
2008-07-16 21:26:15 +03:00
|
|
|
MemberFile ret;
|
2007-07-31 08:24:10 +03:00
|
|
|
std::transform(name.begin(),name.end(),name.begin(),tolower);
|
2008-12-21 21:17:35 +02:00
|
|
|
for (size_t i=0;i<entries.size();++i)
|
2007-07-31 08:24:10 +03:00
|
|
|
{
|
|
|
|
if (entries[i].name==name)
|
|
|
|
{
|
|
|
|
std::string por = entries[i].name;
|
|
|
|
std::transform(por.begin(),por.end(),por.begin(),tolower);
|
|
|
|
if (por==name)
|
|
|
|
{
|
|
|
|
ret.length=entries[i].size;
|
|
|
|
file.seekg(entries[i].offset,std::ios_base::beg);
|
|
|
|
ret.ifs=&file;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2007-07-07 07:30:36 +03:00
|
|
|
unsigned char * CSndHandler::extract (int index, int & size)
|
|
|
|
{
|
|
|
|
size = entries[index].size;
|
|
|
|
unsigned char * ret = new unsigned char[size];
|
|
|
|
file.seekg(entries[index].offset,std::ios_base::beg);
|
|
|
|
file.read((char*)ret,entries[index].size);
|
|
|
|
return ret;
|
2007-07-31 08:24:10 +03:00
|
|
|
}
|
|
|
|
|
2008-03-20 10:51:53 +02:00
|
|
|
unsigned char * CSndHandler::extract (std::string srcName, int &size)
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
std::map<std::string, int>::iterator fit;
|
|
|
|
if ((fit = fimap.find(srcName)) != fimap.end())
|
|
|
|
{
|
|
|
|
index = fit->second;
|
|
|
|
return this->extract(index, size);
|
|
|
|
}
|
|
|
|
size = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-07-31 08:24:10 +03:00
|
|
|
|
|
|
|
CVidHandler::~CVidHandler()
|
|
|
|
{
|
|
|
|
entries.clear();
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
CVidHandler::CVidHandler(std::string fname):CHUNK(65535)
|
|
|
|
{
|
|
|
|
file.open(fname.c_str(),std::ios::binary);
|
|
|
|
if (!file.is_open())
|
2008-07-16 21:26:15 +03:00
|
|
|
#ifndef __GNUC__
|
2007-07-31 08:24:10 +03:00
|
|
|
throw new std::exception((std::string("Cannot open ")+fname).c_str());
|
2008-07-16 21:26:15 +03:00
|
|
|
#else
|
|
|
|
throw new std::exception();
|
|
|
|
#endif
|
2007-07-31 08:24:10 +03:00
|
|
|
int nr = readNormalNr(0,4);
|
|
|
|
char tempc;
|
|
|
|
for (int i=0;i<nr;i++)
|
|
|
|
{
|
|
|
|
Entry entry;
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
file.read(&tempc,1);
|
|
|
|
if (tempc)
|
|
|
|
entry.name+=tempc;
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
entry.something=readNormalNr(-1,4);
|
|
|
|
file.seekg(44-entry.name.length()-9,std::ios_base::cur);
|
|
|
|
entry.offset = readNormalNr(-1,4);
|
|
|
|
if (i>0)
|
|
|
|
entries[i-1].size=entry.offset-entries[i-1].offset;
|
|
|
|
if (i==nr-1)
|
|
|
|
{
|
2008-07-16 21:26:15 +03:00
|
|
|
file.seekg(0,std::ios::end);
|
2007-07-31 08:24:10 +03:00
|
|
|
entry.size = ((int)file.tellg())-entry.offset;
|
2008-07-16 21:26:15 +03:00
|
|
|
file.seekg(0,std::ios::beg);
|
2007-07-31 08:24:10 +03:00
|
|
|
}
|
|
|
|
entries.push_back(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int CVidHandler::readNormalNr (int pos, int bytCon)
|
|
|
|
{
|
|
|
|
if (pos>=0)
|
|
|
|
file.seekg(pos,std::ios_base::beg);
|
|
|
|
int ret=0;
|
|
|
|
int amp=1;
|
|
|
|
unsigned char zcz=0;
|
|
|
|
for (int i=0; i<bytCon; i++)
|
|
|
|
{
|
|
|
|
file.read((char*)(&zcz),1);
|
|
|
|
ret+=zcz*amp;
|
|
|
|
amp*=256;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
void CVidHandler::extract(int index, std::string dstfile) //saves selected file
|
|
|
|
{
|
|
|
|
std::ofstream out(dstfile.c_str(),std::ios_base::binary);
|
|
|
|
file.seekg(entries[index].offset,std::ios_base::beg);
|
|
|
|
int toRead=entries[index].size;
|
|
|
|
char * buffer = new char[std::min(CHUNK,entries[index].size)];
|
|
|
|
while (toRead>CHUNK)
|
|
|
|
{
|
|
|
|
file.read(buffer,CHUNK);
|
|
|
|
out.write(buffer,CHUNK);
|
|
|
|
toRead-=CHUNK;
|
|
|
|
}
|
|
|
|
file.read(buffer,toRead);
|
|
|
|
out.write(buffer,toRead);
|
|
|
|
out.close();
|
|
|
|
}
|
|
|
|
void CVidHandler::extract(std::string srcfile, std::string dstfile, bool caseSens) //saves selected file
|
|
|
|
{
|
|
|
|
if (caseSens)
|
|
|
|
{
|
2008-12-21 21:17:35 +02:00
|
|
|
for (size_t i=0;i<entries.size();++i)
|
2007-07-31 08:24:10 +03:00
|
|
|
{
|
|
|
|
if (entries[i].name==srcfile)
|
|
|
|
extract(i,dstfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::transform(srcfile.begin(),srcfile.end(),srcfile.begin(),tolower);
|
2008-12-21 21:17:35 +02:00
|
|
|
for (size_t i=0;i<entries.size();++i)
|
2007-07-31 08:24:10 +03:00
|
|
|
{
|
|
|
|
if (entries[i].name==srcfile)
|
|
|
|
{
|
|
|
|
std::string por = entries[i].name;
|
|
|
|
std::transform(por.begin(),por.end(),por.begin(),tolower);
|
|
|
|
if (por==srcfile)
|
|
|
|
extract(i,dstfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MemberFile CVidHandler::getFile(std::string name)
|
|
|
|
{
|
2008-07-16 21:26:15 +03:00
|
|
|
MemberFile ret;
|
2007-07-31 08:24:10 +03:00
|
|
|
std::transform(name.begin(),name.end(),name.begin(),tolower);
|
2008-12-21 21:17:35 +02:00
|
|
|
for (size_t i=0;i<entries.size();++i)
|
2007-07-31 08:24:10 +03:00
|
|
|
{
|
|
|
|
if (entries[i].name==name)
|
|
|
|
{
|
|
|
|
std::string por = entries[i].name;
|
|
|
|
std::transform(por.begin(),por.end(),por.begin(),tolower);
|
|
|
|
if (por==name)
|
|
|
|
{
|
|
|
|
ret.length=entries[i].size;
|
|
|
|
file.seekg(entries[i].offset,std::ios_base::beg);
|
|
|
|
ret.ifs=&file;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-01-09 19:21:31 +02:00
|
|
|
throw ret;
|
|
|
|
//return ret;
|
2007-07-31 08:24:10 +03:00
|
|
|
}
|