* zip.h: Rename zip_get_error to zip_error_get;
        rename zip_file_get_error to zip_file_error_get;
        rename zip_error_str to zip_error_to_str;
        replace various zip_add_* and zip_replace_* functions
        with zip_source_*.  Adapt users.  Update docs.

--HG--
branch : HEAD
diff --git a/lib/zip_file_get_error.c b/lib/zip_file_error_get.c
similarity index 100%
rename from lib/zip_file_get_error.c
rename to lib/zip_file_error_get.c
diff --git a/lib/zip_replace_data.c b/lib/zip_source_data.c
similarity index 83%
rename from lib/zip_replace_data.c
rename to lib/zip_source_data.c
index a0ad7c5..2ed3669 100644
--- a/lib/zip_replace_data.c
+++ b/lib/zip_source_data.c
@@ -1,7 +1,7 @@
 /*
-  $NiH: zip_replace_data.c,v 1.16 2004/06/24 16:26:08 dillo Exp $
+  $NiH: zip_source_data.c,v 1.17 2004/11/17 21:55:13 wiz Exp $
 
-  zip_replace_data.c -- replace file from buffer
+  zip_source_data.c -- create zip data source from buffer
   Copyright (C) 1999, 2003, 2004 Dieter Baron and Thomas Klausner
 
   This file is part of libzip, a library to manipulate ZIP archives.
@@ -52,29 +52,20 @@
 
 
 
-int
-zip_replace_data(struct zip *zf, int idx,
-		 const void *data, off_t len, int freep)
-{
-    if (idx < 0 || idx >= zf->nentry) {
-	_zip_error_set(&zf->error, ZIP_ER_INVAL, 0);
-	return -1;
-    }
-    
-    return _zip_replace_data(zf, idx, NULL, data, len, freep);
-}
-
-
-
-int
-_zip_replace_data(struct zip *zf, int idx, const char *name,
-		  const void *data, off_t len, int freep)
+struct zip_source *
+zip_source_data(struct zip *za, const void *data, off_t len, int freep)
 {
     struct read_data *f;
+    struct zip_source *zs;
+
+    if (len < 0 || (data == NULL && len > 0)) {
+	_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
+	return NULL;
+    }
 
     if ((f=malloc(sizeof(*f))) == NULL) {
-	_zip_error_set(&zf->error, ZIP_ER_MEMORY, 0);
-	return -1;
+	_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
+	return NULL;
     }
 
     f->data = data;
@@ -82,7 +73,12 @@
     f->freep = freep;
     f->mtime = time(NULL);
     
-    return _zip_replace(zf, idx, name, read_data, f);
+    if ((zs=zip_source_function(za, read_data, f)) == NULL) {
+	free(f);
+	return NULL;
+    }
+
+    return zs;
 }
 
 
@@ -144,6 +140,7 @@
 	    if (len < sizeof(int)*2)
 		return -1;
 
+	    e = (int *)data;
 	    e[0] = e[1] = 0;
 	}
 	return sizeof(int)*2;
diff --git a/lib/zip_replace_file.c b/lib/zip_source_file.c
similarity index 73%
rename from lib/zip_replace_file.c
rename to lib/zip_source_file.c
index 4641b7a..52b86bb 100644
--- a/lib/zip_replace_file.c
+++ b/lib/zip_source_file.c
@@ -1,7 +1,7 @@
 /*
-  $NiH: zip_replace_file.c,v 1.13 2004/04/16 09:40:30 dillo Exp $
+  $NiH: zip_source_file.c,v 1.14 2004/11/17 21:55:13 wiz Exp $
 
-  zip_replace_file.c -- replace file from file system
+  zip_source_file.c -- create data source from file
   Copyright (C) 1999, 2003, 2004 Dieter Baron and Thomas Klausner
 
   This file is part of libzip, a library to manipulate ZIP archives.
@@ -43,30 +43,21 @@
 
 
 
-int
-zip_replace_file(struct zip *zf, int idx,
-		 const char *fname, off_t start, off_t len)
+struct zip_source *
+zip_source_file(struct zip *za, const char *fname, off_t start, off_t len)
 {
-    if (idx < 0 || idx >= zf->nentry) {
-	_zip_error_set(&zf->error, ZIP_ER_INVAL, 0);
-	return -1;
-    }
-
-    return _zip_replace_file(zf, idx, NULL, fname, start, len);
-}
-
-
-
-int
-_zip_replace_file(struct zip *zf, int idx, const char *name,
-		  const char *fname, off_t start, off_t len)
-{
+    struct zip_source *zs;
     FILE *fp;
 
     if ((fp=fopen(fname, "rb")) == NULL) {
-	_zip_error_set(&zf->error, ZIP_ER_OPEN, errno);
-	return -1;
+	_zip_error_set(&za->error, ZIP_ER_OPEN, errno);
+	return NULL;
     }
 
-    return _zip_replace_filep(zf, idx, name, fp, start, len);
+    if ((zs=zip_source_filep(za, fp, start, len)) == NULL) {
+	fclose(fp);
+	return NULL;
+    }
+
+    return zs;
 }
diff --git a/lib/zip_replace_filep.c b/lib/zip_source_filep.c
similarity index 86%
rename from lib/zip_replace_filep.c
rename to lib/zip_source_filep.c
index d6ac9d3..8ebfb3a 100644
--- a/lib/zip_replace_filep.c
+++ b/lib/zip_source_filep.c
@@ -1,7 +1,7 @@
 /*
-  $NiH: zip_replace_filep.c,v 1.11 2004/06/24 16:26:08 dillo Exp $
+  $NiH: zip_source_filep.c,v 1.12 2004/11/17 21:55:13 wiz Exp $
 
-  zip_replace_filep.c -- replace file from FILE*
+  zip_source_filep.c -- create data source from FILE *
   Copyright (C) 1999, 2003, 2004 Dieter Baron and Thomas Klausner
 
   This file is part of libzip, a library to manipulate ZIP archives.
@@ -56,36 +56,27 @@
 
 
 
-int
-zip_replace_filep(struct zip *zf, int idx,
-		  FILE *file, off_t start, off_t len)
-{
-    if (idx < 0 || idx >= zf->nentry) {
-	_zip_error_set(&zf->error, ZIP_ER_INVAL, 0);
-	return -1;
-    }
-    
-    return _zip_replace_filep(zf, idx, NULL, file, start, len);
-}
-
-
-
-int
-_zip_replace_filep(struct zip *zf, int idx, const char *name,
-		  FILE *file, off_t start, off_t len)
+struct zip_source *
+zip_source_filep(struct zip *za, FILE *file, off_t start, off_t len)
 {
     struct read_file *f;
+    struct zip_source *zs;
 
     if ((f=(struct read_file *)malloc(sizeof(struct read_file))) == NULL) {
-	_zip_error_set(&zf->error, ZIP_ER_MEMORY, 0);
-	return -1;
+	_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
+	return NULL;
     }
 
     f->f = file;
     f->off = start;
     f->len = (len ? len : -1);
     
-    return _zip_replace(zf, idx, name, read_file, f);
+    if ((zs=zip_source_function(za, read_file, f)) == NULL) {
+	free(f);
+	return NULL;
+    }
+
+    return zs;
 }
 
 
diff --git a/lib/zip_get_error.c b/lib/zip_source_free.c
similarity index 81%
rename from lib/zip_get_error.c
rename to lib/zip_source_free.c
index 4111d9c..7d4f5f8 100644
--- a/lib/zip_get_error.c
+++ b/lib/zip_source_free.c
@@ -1,8 +1,8 @@
 /*
-  $NiH: zip_error_get.c,v 1.1 2003/10/06 02:50:06 dillo Exp $
+  $NiH: zip_source_free.c,v 1.17 2004/11/17 21:55:13 wiz Exp $
 
-  zip_error_get.c -- get zip error
-  Copyright (C) 1999, 2003 Dieter Baron and Thomas Klausner
+  zip_source_free.c -- free zip data source
+  Copyright (C) 1999, 2003, 2004 Dieter Baron and Thomas Klausner
 
   This file is part of libzip, a library to manipulate ZIP archives.
   The authors can be contacted at <nih@giga.or.at>
@@ -35,13 +35,20 @@
 
 
 
+#include <stdlib.h>
+
 #include "zip.h"
 #include "zipint.h"
 
 
 
 void
-zip_error_get(struct zip *za, int *zep, int *sep)
+zip_source_free(struct zip_source *source)
 {
-    _zip_error_get(&za->error, zep, sep);
+    if (source == NULL)
+	return;
+
+    (void)source->f(source->ud, NULL, 0, ZIP_CMD_FREE);
+
+    free(source);
 }
diff --git a/lib/zip_get_error.c b/lib/zip_source_function.c
similarity index 75%
copy from lib/zip_get_error.c
copy to lib/zip_source_function.c
index 4111d9c..083ecfc 100644
--- a/lib/zip_get_error.c
+++ b/lib/zip_source_function.c
@@ -1,8 +1,8 @@
 /*
-  $NiH: zip_error_get.c,v 1.1 2003/10/06 02:50:06 dillo Exp $
+  $NiH: zip_source_function.c,v 1.17 2004/11/17 21:55:13 wiz Exp $
 
-  zip_error_get.c -- get zip error
-  Copyright (C) 1999, 2003 Dieter Baron and Thomas Klausner
+  zip_source_function.c -- create zip data source from callback function
+  Copyright (C) 1999, 2003, 2004 Dieter Baron and Thomas Klausner
 
   This file is part of libzip, a library to manipulate ZIP archives.
   The authors can be contacted at <nih@giga.or.at>
@@ -35,13 +35,25 @@
 
 
 
+#include <stdlib.h>
+
 #include "zip.h"
 #include "zipint.h"
 
 
 
-void
-zip_error_get(struct zip *za, int *zep, int *sep)
+struct zip_source *
+zip_source_function(struct zip *za, zip_read_func zrf, void *ud)
 {
-    _zip_error_get(&za->error, zep, sep);
+    struct zip_source *zs;
+
+    if ((zs=malloc(sizeof(*zs))) == NULL) {
+	_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
+	return NULL;
+    }
+
+    zs->f = zrf;
+    zs->ud = ud;
+    
+    return zs;
 }
diff --git a/lib/zip_replace_zip.c b/lib/zip_source_zip.c
similarity index 75%
rename from lib/zip_replace_zip.c
rename to lib/zip_source_zip.c
index a785408..f3b23cb 100644
--- a/lib/zip_replace_zip.c
+++ b/lib/zip_source_zip.c
@@ -1,7 +1,7 @@
 /*
-  $NiH: zip_replace_zip.c,v 1.23 2004/06/24 16:26:08 dillo Exp $
+  $NiH: zip_source_zip.c,v 1.24 2004/11/17 21:55:13 wiz Exp $
 
-  zip_replace_zip.c -- replace file from zip file
+  zip_source_zip.c -- create data source from zip file
   Copyright (C) 1999, 2003, 2004 Dieter Baron and Thomas Klausner
 
   This file is part of libzip, a library to manipulate ZIP archives.
@@ -51,33 +51,18 @@
 
 
 
-int
-zip_replace_zip(struct zip *zf, int idx,
-		struct zip *srczf, int srcidx, int flags,
-		off_t start, off_t len)
-{
-    if (srcidx < 0 || srcidx >= srczf->nentry) {
-	_zip_error_set(&zf->error, ZIP_ER_INVAL, 0);
-	return -1;
-    }
-
-    return _zip_replace_zip(zf, idx, NULL, srczf, srcidx, flags, start, len);
-}
-
-
-
-int
-_zip_replace_zip(struct zip *zf, int idx, const char *name,
-		 struct zip *srczf, int srcidx, int flags,
-		 off_t start, off_t len)
+struct zip_source *
+zip_source_zip(struct zip *za, struct zip *srcza, int srcidx, int flags,
+	       off_t start, off_t len)
 {
     struct zip_error error;
+    struct zip_source *zs;
     struct read_zip *p;
 
     if ((flags & ZIP_FL_UNCHANGED) == 0
-	&& ZIP_ENTRY_DATA_CHANGED(srczf->entry+srcidx)) {
-	_zip_error_set(&zf->error, ZIP_ER_CHANGED, 0);
-	return -1;
+	&& ZIP_ENTRY_DATA_CHANGED(srcza->entry+srcidx)) {
+	_zip_error_set(&za->error, ZIP_ER_CHANGED, 0);
+	return NULL;
     }
 
     if (len == 0)
@@ -89,19 +74,19 @@
 	flags &= ~ZIP_FL_COMPRESSED;
 
     if ((p=malloc(sizeof(*p))) == NULL) {
-	_zip_error_set(&zf->error, ZIP_ER_MEMORY, 0);
-	return -1;
+	_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
+	return NULL;
     }
 	
-    _zip_error_copy(&error, &srczf->error);
+    _zip_error_copy(&error, &srcza->error);
 	
-    if (zip_stat_index(srczf, srcidx, flags, &p->st) < 0
-	|| (p->zf=zip_fopen_index(srczf, srcidx, flags)) == NULL) {
+    if (zip_stat_index(srcza, srcidx, flags, &p->st) < 0
+	|| (p->zf=zip_fopen_index(srcza, srcidx, flags)) == NULL) {
 	free(p);
-	_zip_error_copy(&zf->error, &srczf->error);
-	_zip_error_copy(&srczf->error, &error);
+	_zip_error_copy(&za->error, &srcza->error);
+	_zip_error_copy(&srcza->error, &error);
 	
-	return -1;
+	return NULL;
     }
     p->off = start;
     p->len = len;
@@ -112,7 +97,12 @@
 	/* XXX: crc */
     }
     
-    return _zip_replace(zf, idx, name, read_zip, p);
+    if ((zs=zip_source_function(za, read_zip, p)) == NULL) {
+	free(p);
+	return NULL;
+    }
+
+    return zs;
 }
 
 
@@ -147,7 +137,7 @@
 	
 
 	if ((i=zip_fread(z->zf, buf, n)) < 0) {
-	    /* XXX: copy error from z->zff */
+	    /* XXX: copy error from z->zf */
 	    return -1;
 	}
 
@@ -175,7 +165,7 @@
 		return -1;
 
 	    e = (int *)data;
-	    zip_file_get_error(z->zf, e, e+1);
+	    zip_file_error_get(z->zf, e, e+1);
 	}
 	return sizeof(int)*2;