--- pdftoepdf-orig.cc	Tue Feb 18 20:05:22 2003
+++ pdftoepdf.cc	Wed Feb 19 23:19:48 2003
@@ -17,7 +17,7 @@
 along with pdfTeX; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
-$Id: //depot/Build/source.development/TeX/texk/web2c/pdftexdir/pdftoepdf.cc#32 $
+$Id: pdftoepdf.cc,v 1.12 2003/02/19 21:43:44 hahe Exp hahe $
 */
 
 #include <stdlib.h>
@@ -44,7 +44,8 @@
 #include "Error.h"
 
 #include "epdf.h"
-
+#include "avl.h"
+#define DEBUG
 /* we avoid reading all necessary kpathsea headers, but we need xstrdup */
 #ifdef __cplusplus
 extern "C" {
@@ -145,7 +146,7 @@
 static GBool isInit = gFalse;
 
 // --------------------------------------------------------------------
-// Maintain list of open embedded PDF files
+// Maintain AVL-tree of open embedded PDF files
 // --------------------------------------------------------------------
 
 struct PdfDocument {
@@ -154,10 +155,16 @@
     XRef *xref;
     InObj *inObjList;
     int occurences;
-    PdfDocument *next;
 };
 
-static PdfDocument *pdfDocuments = 0;
+int compare_str (const void *pa, const void *pb, void *param)
+{
+    const char *a = ((const struct PdfDocument *) pa)->file_name;
+    const char *b = ((const struct PdfDocument *) pb)->file_name;
+    return strcmp (a, b);
+}
+
+static struct avl_table *PdfDocTree = 0;
 
 static XRef *xref = 0;
 
@@ -167,10 +174,18 @@
 
 static PdfDocument *find_add_document(char *file_name)
 {
-    PdfDocument *p = pdfDocuments;
-    while (p && strcmp(p->file_name, file_name) != 0)
-        p = p->next;
-    if (p) {
+    static void **vpp;
+    static void *vp;
+    static struct PdfDocument *p;
+    static struct PdfDocument tmp;
+    if (PdfDocTree == NULL)
+        PdfDocTree = avl_create(compare_str, NULL, NULL);
+    if (PdfDocTree == NULL)
+        pdftex_fail("pdftoepdf.cc: avl_create() PdfDocTree failed");
+    tmp.file_name = file_name; 
+    vp = avl_find(PdfDocTree, &tmp);
+    if (vp != NULL) {
+        p = (struct PdfDocument *) vp;
         xref = p->xref;
         (p->occurences)++;
 #ifdef DEBUG
@@ -191,28 +206,25 @@
         pdftex_fail("xpdf: reading PDF image failed");
         }
     p->inObjList = 0;
-    p->next = pdfDocuments;
-    pdfDocuments = p;
+    vpp = avl_probe(PdfDocTree, p);
+    if (vpp == NULL)
+        pdftex_fail("pdftoepdf.cc: avl_probe() out of memory in insertion");
     return p;
 }
 
 // Deallocate a PdfDocument with all its resources
 static void delete_document(PdfDocument *pdf_doc)
 {
-    PdfDocument **p = &pdfDocuments;
-    while (*p && *p != pdf_doc)
-      p = &((*p)->next);
-    // should not happen:
-    if (!*p) 
-      return;
-    // unlink from list
-    *p = pdf_doc->next;
+    void *vp;
     // free pdf_doc's resources
     InObj *r, *n;
     for (r = pdf_doc->inObjList; r != 0; r = n) {
         n = r->next;
         delete r;
     }
+    vp = avl_delete(PdfDocTree, pdf_doc);
+    if (vp == NULL)
+        pdftex_fail("pdftoepdf.cc: avl_delete() deletion failed");
     xref = pdf_doc->xref;
     delete pdf_doc->doc;
     xfree(pdf_doc->file_name);
@@ -1030,18 +1042,19 @@
     }
 }
 
+void pdf_doc_destroy (void *pa, void *pb)
+{
+    PdfDocument *p = (PdfDocument *) pa;
+    delete_document(p);
+}
+
 // Called when PDF embedding system is finalized.
 // Now deallocate all remaining PdfDocuments.
 void epdf_check_mem()
 {
-    if (isInit) {
-        PdfDocument *p, *n;
-    for (p = pdfDocuments; p; p = n) {
-        n = p->next;
-        delete_document(p);
-    }
+    if (isInit) 
+        avl_destroy (PdfDocTree, pdf_doc_destroy);
     // see above for globalParams
     delete globalParams;
-    }
 }
 // vi:ts=4:tw=79:expandtab:ai:
