Pārlūkot izejas kodu

Derive CSndHandler and CVidHandler from new class CMediaHandler.

Frank Zago 16 gadi atpakaļ
vecāks
revīzija
7bb09341a0
2 mainītis faili ar 92 papildinājumiem un 172 dzēšanām
  1. 77 148
      hch/CSndHandler.cpp
  2. 15 24
      hch/CSndHandler.h

+ 77 - 148
hch/CSndHandler.cpp

@@ -13,7 +13,7 @@
  *
  */
 
-CSndHandler::~CSndHandler()
+CMediaHandler::~CMediaHandler()
 {
 	entries.clear();
 	fimap.clear();
@@ -25,7 +25,7 @@ CSndHandler::~CSndHandler()
 // a simple structure. However, some post treatment would be necessary: file
 // size and offsets are little endian, and filename have a NUL in
 // them. */
-CSndHandler::CSndHandler(std::string fname)
+CMediaHandler::CMediaHandler(std::string fname)
 {
 	try //c-tor of mapped_file_source throws exception on failure
 	{
@@ -36,52 +36,15 @@ CSndHandler::CSndHandler(std::string fname)
 		tlog1 << "Cannot open " << fname << std::endl;
 		throw std::string("Cannot open ")+fname;
 	}
-
-	const unsigned char *data = (const unsigned char *)mfile->data();
-
-	unsigned int numFiles = readNormalNr(&data[0]);
-
-	for (unsigned int i=0; i<numFiles; i++)
-	{
-		Entry entry;
-		const unsigned char *p;
-
-		// Read file name and extension
-		p = &data[4+48*i];
-
-		while(*p) {
-			entry.name += *p;
-			p++;
-		}
-
-		entry.name+='.';
-		p++;
-
-		while(*p)
-		{
-			entry.name += *p;
-			p++;
-		}
-
-		// Read offset and size
-		p = &data[4+48*i+40];
-		entry.offset = readNormalNr(p);
-
-		p += 4;
-		entry.size = readNormalNr(p);
-
-		entries.push_back(entry);
-		fimap[entry.name] = i;
-	}
 }
 
 // Reads a 4 byte integer. Format on file is little endian.
-unsigned int CSndHandler::readNormalNr (const unsigned char *p)
+unsigned int CMediaHandler::readNormalNr (const unsigned char *p)
 {
 	return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
 }
 
-void CSndHandler::extract(int index, std::string dstfile) //saves selected file
+void CMediaHandler::extract(int index, std::string dstfile) //saves selected file
 {
 	std::ofstream out(dstfile.c_str(),std::ios_base::binary);
 	const char *data = mfile->data();
@@ -90,7 +53,7 @@ void CSndHandler::extract(int index, std::string dstfile) //saves selected file
 	out.close();
 }
 
-void CSndHandler::extract(std::string srcfile, std::string dstfile, bool caseSens) //saves selected file
+void CMediaHandler::extract(std::string srcfile, std::string dstfile, bool caseSens) //saves selected file
 {
 	if (caseSens)
 	{
@@ -118,7 +81,7 @@ void CSndHandler::extract(std::string srcfile, std::string dstfile, bool caseSen
 
 #if 0
 // unused and not sure what it's supposed to do
-MemberFile CSndHandler::getFile(std::string name)
+MemberFile CMediaHandler::getFile(std::string name)
 {
 	MemberFile ret;
 	std::transform(name.begin(),name.end(),name.begin(),tolower);
@@ -141,7 +104,7 @@ MemberFile CSndHandler::getFile(std::string name)
 }
 #endif
 
-const char * CSndHandler::extract (int index, int & size)
+const char * CMediaHandler::extract (int index, int & size)
 {
 	size = entries[index].size;
 	const char *data = mfile->data();
@@ -149,7 +112,7 @@ const char * CSndHandler::extract (int index, int & size)
 	return &data[entries[index].offset];
 }
 
-const char * CSndHandler::extract (std::string srcName, int &size)
+const char * CMediaHandler::extract (std::string srcName, int &size)
 {
 	int index;
 	std::map<std::string, int>::iterator fit;
@@ -162,122 +125,88 @@ const char * CSndHandler::extract (std::string srcName, int &size)
 	return NULL;
 }
 
-
-CVidHandler::~CVidHandler()
+CSndHandler::CSndHandler(std::string fname) : CMediaHandler::CMediaHandler(fname)
 {
-	entries.clear();
-	file.close();
-}
-CVidHandler::CVidHandler(std::string fname):CHUNK(65535)
-{
-	file.open(fname.c_str(),std::ios::binary);
-	if (!file.is_open())
-#ifndef __GNUC__
-		throw new std::exception((std::string("Cannot open ")+fname).c_str());
-#else
-		throw new std::exception();
-#endif
-	int nr = readNormalNr(0,4);
-	char tempc;
-	for (int i=0;i<nr;i++)
+	const unsigned char *data = (const unsigned char *)mfile->data();
+
+	unsigned int numFiles = readNormalNr(&data[0]);
+
+	for (unsigned int i=0; i<numFiles; i++)
 	{
 		Entry entry;
-		while(true)
-		{
-			file.read(&tempc,1);
-			if (tempc)
-				entry.name+=tempc;
-			else break;
+		const unsigned char *p;
+
+		// Read file name and extension
+		p = &data[4+48*i];
+
+		while(*p) {
+			entry.name += *p;
+			p++;
 		}
-		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)
+
+		entry.name+='.';
+		p++;
+
+		while(*p)
 		{
-			file.seekg(0,std::ios::end);
-			entry.size = ((int)file.tellg())-entry.offset;
-			file.seekg(0,std::ios::beg);
+			entry.name += *p;
+			p++;
 		}
+
+		// Read offset and size
+		p = &data[4+48*i+40];
+		entry.offset = readNormalNr(p);
+
+		p += 4;
+		entry.size = readNormalNr(p);
+
 		entries.push_back(entry);
+		fimap[entry.name] = i;
 	}
 }
-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
+
+CVidHandler::CVidHandler(std::string fname) : CMediaHandler::CMediaHandler(fname) 
 {
-	if (caseSens)
-	{
-		for (size_t i=0;i<entries.size();++i)
-		{
-			if (entries[i].name==srcfile)
-				extract(i,dstfile);
-		}
-	}
-	else
+	const unsigned char *data = (const unsigned char *)mfile->data();
+
+	unsigned int numFiles = readNormalNr(&data[0]);
+
+	for (unsigned int i=0; i<numFiles; i++)
 	{
-		std::transform(srcfile.begin(),srcfile.end(),srcfile.begin(),tolower);
-		for (size_t i=0;i<entries.size();++i)
-		{
-			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);
-			}
+		Entry entry;
+		const unsigned char *p;
+
+		// Read file name and extension
+		p = &data[4+44*i];
+
+		while(*p) {
+			entry.name += *p;
+			p++;
 		}
-	}
-}
-MemberFile CVidHandler::getFile(std::string name)
-{
-	MemberFile ret;
-	std::transform(name.begin(),name.end(),name.begin(),tolower);
-	for (size_t i=0;i<entries.size();++i)
-	{
-		if (entries[i].name==name)
+
+		entry.name+='.';
+		p++;
+
+		while(*p)
 		{
-			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;
-			}
+			entry.name += *p;
+			p++;
 		}
+
+		// Read offset and size
+		p = &data[4+44*i+40];
+		entry.offset = readNormalNr(p);
+
+		// There is no size, so check where the next file is
+		if (i == numFiles - 1) {
+			entry.size = mfile->size() - entry.offset;
+		} else {
+			p = &data[4+44*(i+1)+40];
+			int next_offset = readNormalNr(p);
+			entry.size = next_offset - entry.offset;
+		}	
+
+		entries.push_back(entry);
+		fimap[entry.name] = i;
 	}
-	throw ret;
-	//return ret;
 }

+ 15 - 24
hch/CSndHandler.h

@@ -29,9 +29,10 @@ struct MemberFile
 	std::ifstream * ifs;
 	int length;
 };
-class CSndHandler
+
+class CMediaHandler
 {
-private:
+protected:
 	struct Entry
 	{
 		std::string name;
@@ -44,36 +45,26 @@ private:
 
 public:
 	std::vector<Entry> entries;
-	std::map<std::string, int> fimap; // map of wav file and index
-	~CSndHandler(); //d-tor
-	CSndHandler(std::string fname); //c-tor
+	std::map<std::string, int> fimap; // map of file and index
+	~CMediaHandler(); //d-tor
+	CMediaHandler(std::string fname); //c-tor
 	void extract(std::string srcfile, std::string dstfile, bool caseSens=true); //saves selected file
 	const char * extract (std::string srcfile, int & size); //return selecte file data, NULL if file doesn't exist
 	void extract(int index, std::string dstfile); //saves selected file
 	MemberFile getFile(std::string name);//nie testowane - sprawdzic
 	const char * extract (int index, int & size); //return selecte file - NIE TESTOWANE
 };
-class CVidHandler
+
+class CSndHandler: public CMediaHandler
 {
-protected:
-	const int CHUNK;
-	struct Entry
-	{
-		std::string name;
-		int size, offset, something;
-	};
-	std::ifstream file;
-	int readNormalNr (int pos, int bytCon);
-	bool opened;
 public:
-	std::vector<Entry> entries;
-	~CVidHandler(); //d-tor
-	CVidHandler(std::string fname); //c-tor
-	std::ifstream & extract(std::string srcfile);
-	void extract(std::string srcfile, std::string dstfile, bool caseSens=true); //saves selected file
-	unsigned char * extract (std::string srcfile, int & size); //return selecte file,
-	void extract(int index, std::string dstfile); //saves selected file
-	MemberFile getFile(std::string name); //nie testowane - sprawdzic
+	CSndHandler(std::string fname);
+};
+
+class CVidHandler: public CMediaHandler
+{
+public:
+	CVidHandler(std::string fname);
 };