If only C had templates!  Without function templates, macros
like these two will have to do.

This patch refactors the callers for zlib and libbz2 library
functions to share some code, in preparation for some small
changes that apply to both.

Signed-off-by: Jonathan Nieder <jrnie...@gmail.com>
---
 lib/dpkg/compression.c |  165 +++++++++++++++++++++---------------------------
 1 files changed, 71 insertions(+), 94 deletions(-)

diff --git a/lib/dpkg/compression.c b/lib/dpkg/compression.c
index f7444af..22e1af7 100644
--- a/lib/dpkg/compression.c
+++ b/lib/dpkg/compression.c
@@ -36,6 +36,67 @@ fd_fd_filter(int fd_in, int fd_out,
   ohshite(_("%s: failed to exec '%s %s'"), desc, cmd, args);
 }
 
+#define DECOMPRESS(format, zFile, zdopen, zread, zerror, ERR_ERRNO, \
+                   fd_in, fd_out, desc) do \
+{ \
+  char buffer[4096]; \
+  int actualread; \
+  zFile zfile = zdopen(fd_in, "r"); \
+  \
+  while ((actualread = zread(zfile, buffer, sizeof(buffer))) > 0) { \
+    if (actualread < 0) { \
+      int err = 0; \
+      const char *errmsg = zerror(zfile, &err); \
+      if (err == ERR_ERRNO) { \
+        if (errno == EINTR) \
+          continue; \
+        errmsg = strerror(errno); \
+      } \
+      ohshite(_("%s: internal " format " error: `%s'"), desc, errmsg); \
+    } \
+    write(fd_out, buffer, actualread); \
+  } \
+  exit(0); \
+} while(0)
+
+#define COMPRESS(format, zFile, zdopen, zwrite, zclose, zerror, ERR_ERRNO, \
+                 fd_in, fd_out, compression, desc) do \
+{ \
+  char combuf[6]; \
+  int actualread, actualwrite; \
+  char buffer[4096]; \
+  zFile zfile; \
+  \
+  strncpy(combuf, "w9", sizeof(combuf)); \
+  combuf[1] = compression; \
+  zfile = zdopen(1, combuf); \
+  while ((actualread = read(0, buffer, sizeof(buffer))) > 0) { \
+    if (actualread < 0) { \
+      if (errno == EINTR) \
+        continue; \
+      ohshite(_("%s: internal " format " error: read: `%s'"), \
+              desc, strerror(errno)); \
+    } \
+    actualwrite = zwrite(zfile, buffer, actualread); \
+    if (actualwrite < 0) { \
+      int err = 0; \
+      const char *errmsg = zerror(zfile, &err); \
+      if (err == ERR_ERRNO) { \
+        if (errno == EINTR) \
+          continue; \
+        errmsg = strerror(errno); \
+      } \
+      ohshite(_("%s: internal " format " error: write: `%s'"), \
+              desc, errmsg); \
+    } \
+    if (actualwrite != actualread) \
+      ohshite(_("%s: internal " format " error: read(%i) != write(%i)"), \
+              desc, actualread, actualwrite); \
+  } \
+  zclose(zfile); \
+  exit(0); \
+} while(0)
+
 void decompress_cat(enum compress_type type, int fd_in, int fd_out, char 
*desc, ...) {
   va_list al;
   struct varbuf v = VARBUF_INIT;
@@ -47,47 +108,16 @@ void decompress_cat(enum compress_type type, int fd_in, 
int fd_out, char *desc,
   switch(type) {
     case compress_type_gzip:
 #ifdef WITH_ZLIB
-      {
-        char buffer[4096];
-        int actualread;
-        gzFile gzfile = gzdopen(fd_in, "r");
-        while ((actualread= gzread(gzfile,buffer,sizeof(buffer))) > 0) {
-          if (actualread < 0 ) {
-            int err = 0;
-            const char *errmsg = gzerror(gzfile, &err);
-            if (err == Z_ERRNO) {
-              if (errno == EINTR) continue;
-              errmsg= strerror(errno);
-            }
-          ohshite(_("%s: internal gzip error: `%s'"), v.buf, errmsg);
-          }
-          write(fd_out,buffer,actualread);
-        }
-      }
-      exit(0);
+      DECOMPRESS("gzip", gzFile, gzdopen, gzread, gzerror, Z_ERRNO,
+                 fd_in, fd_out, v.buf);
 #else
       fd_fd_filter(fd_in, fd_out, GZIP, "gzip", "-dc", v.buf);
 #endif
     case compress_type_bzip2:
 #ifdef WITH_BZ2
-      {   
-        char buffer[4096];
-        int actualread;
-        BZFILE *bzfile = BZ2_bzdopen(fd_in, "r");
-        while ((actualread= BZ2_bzread(bzfile,buffer,sizeof(buffer))) > 0) {
-          if (actualread < 0 ) {
-            int err = 0;
-            const char *errmsg = BZ2_bzerror(bzfile, &err);
-            if (err == BZ_IO_ERROR) {
-              if (errno == EINTR) continue;
-              errmsg= strerror(errno);
-            }
-          ohshite(_("%s: internal bzip2 error: `%s'"), v.buf, errmsg);
-          }
-          write(fd_out,buffer,actualread);
-        }
-      }
-      exit(0);
+      DECOMPRESS("bzip2", BZFILE *, BZ2_bzdopen, BZ2_bzread,
+                 BZ2_bzerror, BZ_IO_ERROR,
+                 fd_in, fd_out, v.buf);
 #else
       fd_fd_filter(fd_in, fd_out, BZIP2, "bzip2", "-dc", v.buf);
 #endif
@@ -116,35 +146,8 @@ void compress_cat(enum compress_type type, int fd_in, int 
fd_out, const char *co
   switch(type) {
     case compress_type_gzip:
 #ifdef WITH_ZLIB
-      {
-        char combuf[6];
-        int actualread, actualwrite;
-        char buffer[4096];
-        gzFile gzfile;
-        strncpy(combuf, "w9", sizeof(combuf));
-        combuf[1]= *compression;
-        gzfile = gzdopen(1, combuf);
-        while((actualread = read(0,buffer,sizeof(buffer))) > 0) {
-          if (actualread < 0 ) {
-            if (errno == EINTR) continue;
-            ohshite(_("%s: internal gzip error: read: `%s'"), v.buf, 
strerror(errno));
-          }
-          actualwrite= gzwrite(gzfile,buffer,actualread);
-          if (actualwrite < 0 ) {
-            int err = 0;
-            const char *errmsg = gzerror(gzfile, &err);
-            if (err == Z_ERRNO) {
-              if (errno == EINTR) continue;
-            errmsg= strerror(errno);
-            }
-            ohshite(_("%s: internal gzip error: write: `%s'"), v.buf, errmsg);
-          }
-          if (actualwrite != actualread)
-            ohshite(_("%s: internal gzip error: read(%i) != write(%i)"), 
v.buf, actualread, actualwrite);
-        }
-        gzclose(gzfile);
-        exit(0);
-      }
+      COMPRESS("gzip", gzFile, gzdopen, gzwrite, gzclose, gzerror, Z_ERRNO,
+               fd_in, fd_out, *compression, v.buf);
 #else
       {
         char combuf[6];
@@ -155,35 +158,9 @@ void compress_cat(enum compress_type type, int fd_in, int 
fd_out, const char *co
 #endif
     case compress_type_bzip2:
 #ifdef WITH_BZ2
-      {
-        char combuf[6];
-        int actualread, actualwrite;
-        char buffer[4096];
-        BZFILE *bzfile;
-        strncpy(combuf, "w9", sizeof(combuf));
-        combuf[1]= *compression;
-        bzfile = BZ2_bzdopen(1, combuf);
-        while((actualread = read(0,buffer,sizeof(buffer))) > 0) {
-          if (actualread < 0 ) {
-            if (errno == EINTR) continue;
-            ohshite(_("%s: internal bzip2 error: read: `%s'"), v.buf, 
strerror(errno));
-          }
-          actualwrite= BZ2_bzwrite(bzfile,buffer,actualread);
-          if (actualwrite < 0 ) {
-            int err = 0;
-            const char *errmsg = BZ2_bzerror(bzfile, &err);
-            if (err == BZ_IO_ERROR) {
-              if (errno == EINTR) continue;
-              errmsg= strerror(errno);
-            }
-            ohshite(_("%s: internal bzip2 error: write: `%s'"), v.buf, errmsg);
-          }
-          if (actualwrite != actualread)
-            ohshite(_("%s: internal bzip2 error: read(%i) != write(%i)"), 
v.buf, actualread, actualwrite);
-        }
-        BZ2_bzclose(bzfile);
-        exit(0);
-      }
+      COMPRESS("bzip2", BZFILE *, BZ2_bzdopen, BZ2_bzwrite, BZ2_bzclose,
+               BZ2_bzerror, BZ_IO_ERROR,
+               fd_in, fd_out, *compression, v.buf);
 #else
       {
         char combuf[6];
-- 
1.6.5.rc1.199.g596ec


-- 
To UNSUBSCRIBE, email to debian-dpkg-requ...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org

Reply via email to