← Back to team overview

linuxdcpp-team team mailing list archive

[Branch ~dcplusplus-team/dcplusplus/trunk] Rev 2616: move the gz decompression routine

 

------------------------------------------------------------
revno: 2616
committer: poy <poy@xxxxxxxxxx>
branch nick: trunk
timestamp: Wed 2011-09-28 19:00:17 +0200
message:
  move the gz decompression routine
modified:
  dcpp/GeoIP.cpp
  dcpp/GeoIP.h
  dcpp/ZUtils.cpp
  dcpp/ZUtils.h


--
lp:dcplusplus
https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk

Your team Dcplusplus-team is subscribed to branch lp:dcplusplus.
To unsubscribe from this branch go to https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk/+edit-subscription
=== modified file 'dcpp/GeoIP.cpp'
--- dcpp/GeoIP.cpp	2011-09-23 11:47:15 +0000
+++ dcpp/GeoIP.cpp	2011-09-28 17:00:17 +0000
@@ -22,8 +22,9 @@
 #include "File.h"
 #include "format.h"
 #include "Util.h"
+#include "ZUtils.h"
 
-#include <zlib.h>
+#include <GeoIP.h>
 
 namespace dcpp {
 
@@ -40,31 +41,9 @@
 			return;
 		}
 
-		// decompress the file.
-		try {
-			auto gz = gzopen((path + ".gz").c_str(), "rb");
-			if(!gz) {
-				return;
-			}
-			File dat(path, File::WRITE, File::CREATE);
-
-			const size_t BUF_SIZE = 64 * 1024;
-			ByteVector buf(BUF_SIZE);
-
-			while(true) {
-				auto read = gzread(gz, &buf[0], BUF_SIZE);
-				if(read >= 0) {
-					dat.write(&buf[0], read);
-				}
-				if(read < BUF_SIZE) {
-					break;
-				}
-			}
-
-			gzclose(gz);
-		} catch(const Exception&) {
-			return;
-		}
+		// there is a .gz file but no .dat; decompress the .gz.
+		try { GZ::decompress(path + ".gz", path); }
+		catch(const Exception&) { return; }
 	}
 
 	geo = GeoIP_open(path.c_str(), GEOIP_STANDARD);

=== modified file 'dcpp/GeoIP.h'
--- dcpp/GeoIP.h	2011-09-23 15:08:32 +0000
+++ dcpp/GeoIP.h	2011-09-28 17:00:17 +0000
@@ -19,9 +19,10 @@
 #ifndef DCPLUSPLUS_DCPP_GEOIP_H
 #define DCPLUSPLUS_DCPP_GEOIP_H
 
-#include <GeoIP.h>
 #include <string>
 
+typedef struct GeoIPTag GeoIP;
+
 namespace dcpp {
 
 using std::string;

=== modified file 'dcpp/ZUtils.cpp'
--- dcpp/ZUtils.cpp	2011-04-13 19:16:51 +0000
+++ dcpp/ZUtils.cpp	2011-09-28 17:00:17 +0000
@@ -21,6 +21,7 @@
 
 #include "format.h"
 #include "Exception.h"
+#include "File.h"
 
 namespace dcpp {
 
@@ -129,4 +130,27 @@
 	return err == Z_OK;
 }
 
+void GZ::decompress(const string& source, const string& target) {
+	auto gz = gzopen(source.c_str(), "rb");
+	if(!gz) {
+		throw Exception(_("Error during decompression"));
+	}
+	File f(target, File::WRITE, File::CREATE);
+
+	const size_t BUF_SIZE = 64 * 1024;
+	ByteVector buf(BUF_SIZE);
+
+	while(true) {
+		auto read = gzread(gz, &buf[0], BUF_SIZE);
+		if(read > 0) {
+			f.write(&buf[0], read);
+		}
+		if(read < BUF_SIZE) {
+			break;
+		}
+	}
+
+	gzclose(gz);
+}
+
 } // namespace dcpp

=== modified file 'dcpp/ZUtils.h'
--- dcpp/ZUtils.h	2011-04-15 20:53:17 +0000
+++ dcpp/ZUtils.h	2011-09-28 17:00:17 +0000
@@ -21,11 +21,14 @@
 
 #include <cstddef>
 #include <cstdint>
+#include <string>
 
 #include <zlib.h>
 
 namespace dcpp {
 
+using std::string;
+
 class ZFilter {
 public:
 	/** Compression will automatically be turned off if below this... */
@@ -66,6 +69,11 @@
 	z_stream zs;
 };
 
+class GZ {
+public:
+	static void decompress(const string& source, const string& target);
+};
+
 class CRC32Filter {
 public:
 	CRC32Filter() : crc(crc32(0, NULL, 0)) { }