]> sjero.net Git - wget/blobdiff - src/cookies.c
[svn] Simplify iteration over hash table entries.
[wget] / src / cookies.c
index 9320eabbab32b90eca0aaed200a6471f6224190d..075a45d9e5099561b229f6201b9d245f058ab456 100644 (file)
@@ -1421,44 +1421,13 @@ cookie_jar_load (struct cookie_jar *jar, const char *file)
   fclose (fp);
 }
 
-/* Mapper for save_cookies callable by hash_table_map.  VALUE points
-   to the head in a chain of cookies.  The function prints the entire
-   chain.  */
-
-static int
-save_cookies_mapper (void *key, void *value, void *arg)
-{
-  FILE *fp = (FILE *)arg;
-  char *domain = (char *)key;
-  struct cookie *cookie = (struct cookie *)value;
-  for (; cookie; cookie = cookie->next)
-    {
-      if (!cookie->permanent && !opt.keep_session_cookies)
-       continue;
-      if (cookie_expired_p (cookie))
-       continue;
-      if (!cookie->domain_exact)
-       fputc ('.', fp);
-      fputs (domain, fp);
-      if (cookie->port != PORT_ANY)
-       fprintf (fp, ":%d", cookie->port);
-      fprintf (fp, "\t%s\t%s\t%s\t%.0f\t%s\t%s\n",
-              cookie->domain_exact ? "FALSE" : "TRUE",
-              cookie->path, cookie->secure ? "TRUE" : "FALSE",
-              (double)cookie->expiry_time,
-              cookie->attr, cookie->value);
-      if (ferror (fp))
-       return 1;               /* stop mapping */
-    }
-  return 0;
-}
-
 /* Save cookies, in format described above, to FILE. */
 
 void
 cookie_jar_save (struct cookie_jar *jar, const char *file)
 {
   FILE *fp;
+  hash_table_iterator iter;
 
   DEBUGP (("Saving cookies to %s.\n", file));
 
@@ -1476,8 +1445,33 @@ cookie_jar_save (struct cookie_jar *jar, const char *file)
   fprintf (fp, "# Generated by Wget on %s.\n", datetime_str (&cookies_now));
   fputs ("# Edit at your own risk.\n\n", fp);
 
-  hash_table_map (jar->chains, save_cookies_mapper, fp);
-
+  for (hash_table_iterate (jar->chains, &iter);
+       hash_table_iter_next (&iter);
+       )
+    {
+      const char *domain = iter.key;
+      struct cookie *cookie = iter.value;
+      for (; cookie; cookie = cookie->next)
+       {
+         if (!cookie->permanent && !opt.keep_session_cookies)
+           continue;
+         if (cookie_expired_p (cookie))
+           continue;
+         if (!cookie->domain_exact)
+           fputc ('.', fp);
+         fputs (domain, fp);
+         if (cookie->port != PORT_ANY)
+           fprintf (fp, ":%d", cookie->port);
+         fprintf (fp, "\t%s\t%s\t%s\t%.0f\t%s\t%s\n",
+                  cookie->domain_exact ? "FALSE" : "TRUE",
+                  cookie->path, cookie->secure ? "TRUE" : "FALSE",
+                  (double)cookie->expiry_time,
+                  cookie->attr, cookie->value);
+         if (ferror (fp))
+           goto out;
+       }
+    }
+ out:
   if (ferror (fp))
     logprintf (LOG_NOTQUIET, _("Error writing to `%s': %s\n"),
               file, strerror (errno));
@@ -1489,9 +1483,9 @@ cookie_jar_save (struct cookie_jar *jar, const char *file)
 }
 \f
 /* Destroy all the elements in the chain and unhook it from the cookie
-   jar.  This is written in the form of a callback to hash_table_map
-   and used by cookie_jar_delete to delete all the cookies in a
-   jar.  */
+   jar.  This is written in the form of a callback to
+   hash_table_for_each and used by cookie_jar_delete to delete all the
+   cookies in a jar.  */
 
 static int
 nuke_cookie_chain (void *value, void *key, void *arg)
@@ -1521,7 +1515,7 @@ nuke_cookie_chain (void *value, void *key, void *arg)
 void
 cookie_jar_delete (struct cookie_jar *jar)
 {
-  hash_table_map (jar->chains, nuke_cookie_chain, jar);
+  hash_table_for_each (jar->chains, nuke_cookie_chain, jar);
   hash_table_destroy (jar->chains);
   xfree (jar);
 }