[Scummvm-cvs-logs] SF.net SVN: scummvm:[34370] scummvm/trunk

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Sat Sep 6 00:12:47 CEST 2008


Revision: 34370
          http://scummvm.svn.sourceforge.net/scummvm/?rev=34370&view=rev
Author:   fingolfin
Date:     2008-09-05 22:12:46 +0000 (Fri, 05 Sep 2008)

Log Message:
-----------
Added simple ZipArchive class, and changed some GUI code to use it, instead of the ugly C API to the unzip code

Modified Paths:
--------------
    scummvm/trunk/common/unzip.h
    scummvm/trunk/gui/theme.cpp

Modified: scummvm/trunk/common/unzip.h
===================================================================
--- scummvm/trunk/common/unzip.h	2008-09-05 20:53:30 UTC (rev 34369)
+++ scummvm/trunk/common/unzip.h	2008-09-05 22:12:46 UTC (rev 34370)
@@ -65,9 +65,12 @@
 #ifndef _unz_H
 #define _unz_H
 
+#ifdef USE_ZLIB
+
 #include "common/scummsys.h"
+#include "common/archive.h"
+#include "common/stream.h"
 
-#ifdef USE_ZLIB
 
 #ifdef __cplusplus
 extern "C" {
@@ -302,6 +305,49 @@
 }
 #endif
 
-#endif
 
+class ZipArchive : Common::Archive {
+	unzFile _zipFile;
+
+public:
+	ZipArchive(const Common::String &name) {
+		_zipFile = unzOpen(name.c_str());
+	}
+	~ZipArchive() {
+		unzClose(_zipFile);
+	}
+
+	virtual bool hasFile(const Common::String &name) {
+		return (_zipFile && unzLocateFile(_zipFile, name.c_str(), 2) == UNZ_OK);
+	}
+
+	virtual int getAllNames(Common::StringList &list) {
+		// TODO
+		return 0;
+	}
+
+	virtual Common::SeekableReadStream *openFile(const Common::String &name) {
+		if (!_zipFile)
+			return 0;
+	
+		unzLocateFile(_zipFile, name.c_str(), 2);
+
+		unz_file_info fileInfo;
+		unzOpenCurrentFile(_zipFile);
+		unzGetCurrentFileInfo(_zipFile, &fileInfo, NULL, 0, NULL, 0, NULL, 0);
+		byte *buffer = (byte *)calloc(fileInfo.uncompressed_size+1, 1);
+		assert(buffer);
+		unzReadCurrentFile(_zipFile, buffer, fileInfo.uncompressed_size);
+		unzCloseCurrentFile(_zipFile);
+		return new Common::MemoryReadStream(buffer, fileInfo.uncompressed_size+1, true);
+		
+		// FIXME: instead of reading all into a memory stream, we could
+		// instead create a new ZipStream class. But then we have to be
+		// careful to handle the case where the client code opens multiple
+		// files in the archive and tries to use them indepenendtly.
+	}
+};
+
+#endif // USE_ZLIB
+
 #endif /* _unz_H */

Modified: scummvm/trunk/gui/theme.cpp
===================================================================
--- scummvm/trunk/gui/theme.cpp	2008-09-05 20:53:30 UTC (rev 34369)
+++ scummvm/trunk/gui/theme.cpp	2008-09-05 22:12:46 UTC (rev 34370)
@@ -26,6 +26,7 @@
 #include "gui/eval.h"
 #include "common/file.h"
 
+#include "common/archive.h"
 #include "common/unzip.h"
 
 namespace GUI {
@@ -69,24 +70,11 @@
 			return font;
 
 #ifdef USE_ZLIB
-		unzFile zipFile = unzOpen((_stylefile + ".zip").c_str());
-		if (zipFile && unzLocateFile(zipFile, cacheFilename.c_str(), 2) == UNZ_OK) {
-			unz_file_info fileInfo;
-			unzOpenCurrentFile(zipFile);
-			unzGetCurrentFileInfo(zipFile, &fileInfo, NULL, 0, NULL, 0, NULL, 0);
-			uint8 *buffer = new uint8[fileInfo.uncompressed_size+1];
-			assert(buffer);
-			memset(buffer, 0, (fileInfo.uncompressed_size+1)*sizeof(uint8));
-			unzReadCurrentFile(zipFile, buffer, fileInfo.uncompressed_size);
-			unzCloseCurrentFile(zipFile);
-			Common::MemoryReadStream stream(buffer, fileInfo.uncompressed_size+1);
-
-			font = Graphics::NewFont::loadFromCache(stream);
-
-			delete[] buffer;
-			buffer = 0;
+		ZipArchive zipArchive(_stylefile + ".zip");
+		if (zipArchive.hasFile(cacheFilename)) {
+			Common::FilePtr stream(zipArchive.openFile(cacheFilename));
+			font = Graphics::NewFont::loadFromCache(*stream.get());
 		}
-		unzClose(zipFile);
 #endif
 		if (font)
 			return font;
@@ -99,24 +87,11 @@
 
 #ifdef USE_ZLIB
 	if (!font) {
-		unzFile zipFile = unzOpen((_stylefile + ".zip").c_str());
-		if (zipFile && unzLocateFile(zipFile, filename, 2) == UNZ_OK) {
-			unz_file_info fileInfo;
-			unzOpenCurrentFile(zipFile);
-			unzGetCurrentFileInfo(zipFile, &fileInfo, NULL, 0, NULL, 0, NULL, 0);
-			uint8 *buffer = new uint8[fileInfo.uncompressed_size+1];
-			assert(buffer);
-			memset(buffer, 0, (fileInfo.uncompressed_size+1)*sizeof(uint8));
-			unzReadCurrentFile(zipFile, buffer, fileInfo.uncompressed_size);
-			unzCloseCurrentFile(zipFile);
-			Common::MemoryReadStream stream(buffer, fileInfo.uncompressed_size+1);
-
-			font = Graphics::NewFont::loadFont(stream);
-
-			delete[] buffer;
-			buffer = 0;
+		ZipArchive zipArchive(_stylefile + ".zip");
+		if (zipArchive.hasFile(filename)) {
+			Common::FilePtr stream(zipArchive.openFile(filename));
+			font = Graphics::NewFont::loadFont(*stream.get());
 		}
-		unzClose(zipFile);
 	}
 #endif
 
@@ -158,37 +133,20 @@
 	if (ConfMan.hasKey("extrapath"))
 		Common::File::addDefaultDirectoryRecursive(ConfMan.get("extrapath"));
 
-	if (!_configFile.loadFromFile(stylefile + ".ini")) {
+	if (_configFile.loadFromFile(stylefile + ".ini"))
+		return true;
+
 #ifdef USE_ZLIB
-		// Maybe find a nicer solution to this
-		unzFile zipFile = unzOpen((stylefile + ".zip").c_str());
-		if (zipFile && unzLocateFile(zipFile, (stylefile + ".ini").c_str(), 2) == UNZ_OK) {
-			unz_file_info fileInfo;
-			unzOpenCurrentFile(zipFile);
-			unzGetCurrentFileInfo(zipFile, &fileInfo, NULL, 0, NULL, 0, NULL, 0);
-			uint8 *buffer = new uint8[fileInfo.uncompressed_size+1];
-			assert(buffer);
-			memset(buffer, 0, (fileInfo.uncompressed_size+1)*sizeof(uint8));
-			unzReadCurrentFile(zipFile, buffer, fileInfo.uncompressed_size);
-			unzCloseCurrentFile(zipFile);
-			Common::MemoryReadStream stream(buffer, fileInfo.uncompressed_size+1);
-			if (!_configFile.loadFromStream(stream)) {
-				unzClose(zipFile);
-				return false;
-			}
-			delete[] buffer;
-			buffer = 0;
-		} else {
-			unzClose(zipFile);
-			return false;
-		}
-		unzClose(zipFile);
-#else
-		return false;
-#endif
+	// Maybe find a nicer solution to this
+	ZipArchive zipArchive(stylefile + ".zip");
+	if (zipArchive.hasFile(stylefile + ".ini")) {
+		Common::FilePtr stream(zipArchive.openFile(stylefile + ".ini"));
+		if (_configFile.loadFromStream(*stream.get()))
+			return true;
 	}
+#endif
 
-	return true;
+	return false;
 }
 
 bool Theme::themeConfigUseable(const Common::String &stylefile, const Common::String &style, Common::String *cStyle, Common::ConfigFile *cfg) {
@@ -210,30 +168,16 @@
 	if (!file.open(stylefile + ".ini")) {
 #ifdef USE_ZLIB
 		// Maybe find a nicer solution to this
-		unzFile zipFile = unzOpen((stylefile + ".zip").c_str());
-		if (zipFile && unzLocateFile(zipFile, (stylefile + ".ini").c_str(), 2) == UNZ_OK) {
+		ZipArchive zipArchive(stylefile + ".zip");
+		if (zipArchive.hasFile(stylefile + ".ini")) {
 			if (!style.empty() || cStyle || cfg) {
-				unz_file_info fileInfo;
-				unzOpenCurrentFile(zipFile);
-				unzGetCurrentFileInfo(zipFile, &fileInfo, NULL, 0, NULL, 0, NULL, 0);
-				uint8 *buffer = new uint8[fileInfo.uncompressed_size+1];
-				assert(buffer);
-				memset(buffer, 0, (fileInfo.uncompressed_size+1)*sizeof(uint8));
-				unzReadCurrentFile(zipFile, buffer, fileInfo.uncompressed_size);
-				unzCloseCurrentFile(zipFile);
-				Common::MemoryReadStream stream(buffer, fileInfo.uncompressed_size+1);
-				if (!cfg->loadFromStream(stream)) {
-					unzClose(zipFile);
+				Common::FilePtr stream(zipArchive.openFile(stylefile + ".ini"));
+				if (!cfg->loadFromStream(*stream.get()))
 					return false;
-				}
-				delete[] buffer;
-				buffer = 0;
 			}
 		} else {
-			unzClose(zipFile);
 			return false;
 		}
-		unzClose(zipFile);
 #else
 		return false;
 #endif


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list